-1

我有两个问题 - 我是 Java 初学者,但有一个庞大的 Java 项目 :(

  1. 如果我实现一个类(这是一个具有两个 void 方法的接口)并且我尝试在我的类中编写另一个方法,它会编译但在运行时该方法被跳过?我可能做错了什么?

  2. 我如何从另一个不是主要的类中运行一个类。所以基本上主运行一个类,然后调用另一个类......

我知道这些问题在内容方面非常有限,但程序真的很复杂,无法解释一切:(

任何帮助将不胜感激:) 谢谢!

--- 更新 - 根据要求

 class FriendLists implements Results {
     public void processCommunication (Communication d)  {...}

     public void postProcess() {...}

     //the above two run perfectly

     //I don't know what to do next. 
     //I'm trying to create a link to my other class, to use the values values 
     //but when compiled and running it skips this method (and all other methods except the above two

     public void processCommunication(AggregatedDirect f) {
     //does something from the class I'm trying to run
      man = f.getNumTargets(); //getNumTargets is a value in the AggregatedDirect Class
     }

,

interface Results {
void processCommunication (Communication c) throws SAXException;
    void postProcess();

}

,

  public  class AggregatedDirect extends Communication {
ArrayList<Integer> targets;


public AggregatedDirect(Direct d) {
    super();
    targets = new ArrayList<Integer>();
    this.type = d.getType();
    this.invocSerial = d.getInvocSerial();
    this.serial = d.getSerial();
    this.usage = d.getUsage();
    this.messageType = d.getMessageType();
    this.characterID = d.getCharacterID();
    this.characterStatus = d.getCharacterStatus();
    this.locationID = d.getLocationID();
    targets.add(d.getTargetCharacterID());
    this.targetCharacterID = -1;
    this.targetCharacterStatus = -1;
    this.targetCharacterLocationID = -1;
    this.message = d.getMessage();
    this.time = d.getTime();
    this.annotation = d.getAnnotation();
}

public void addTarget(int targetCharacterID) {
    targets.add(targetCharacterID);
}

public void addTarget(Direct d){
    addTarget(d.getTargetCharacterID());
}

public int getNumTargets() {
    if (targets == null)
        return -1;
    else
        return targets.size();
}
public ArrayList<Integer> getTargets() {


     return targets;
    }
}
  • 在这里描述类Communication
  • 所有通信的抽象超类
  • 子类。
  • 实际上 - 处理 XML 文件并将其分离

    • 直接扩展通信 // 它基本上是 XML 文件中的字符串值之一
4

2 回答 2

1

你必须调用一个方法来运行它,所以假设你有一个方法叫做

public void someMethod(){ 
   // method contents
}

为了运行它,您必须someMethod();从该方法的范围内调用。

要从另一个类调用方法,您通常必须首先创建该类的一个instanceobject。假设该类被调用OtherClass并且该类中有一个名为 的方法otherMethod(),在您当前的类中,您必须创建一个如下类型的对象OtherClass

OtherClass otherClass = new OtherClass();

然后使用调用另一个方法

otherClass.otherMethod();

如果这个答案太简单,请告诉我们。:)

于 2012-01-29T00:57:53.523 回答
1

如果您向您的类添加另一个方法,但该方法未在您的接口中定义,并且您在实例化对象时使用该接口作为类型,那么 Java 运行时将不会知道该方法的存在。

例如:

Class MyClass implements List {

    public void method1() {
        // do stuff
    }

    public void method2() {
        // do other stuff
    }

    public void method3() {  // not defined in Interface
        // do yet other stuff
    }

}


Interface List {

    void method1();

    void method2();

}

现在,如果您的代码中的服务类返回“List”类型的对象,那么您的调用类将不知道子类型:

 List list = getMyList();

 list.method3();  // doesn't know what this is because it's type List not type MyClass
于 2012-01-29T00:59:12.823 回答