-1

2 つの質問があります。私は Java の初心者ですが、巨大な Java プロジェクトを持っています :(

  1. クラス (2 つの 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 ファイルを処理して分離します

    • Direct は通信を拡張します // 基本的に XML ファイル内の文字列値の 1 つです
4

2 に答える 2

1

それを実行するにはメソッドを呼び出す必要があるので、と呼ばれるメソッドがあるとしましょう

public void someMethod(){ 
   // method contents
}

それを実行するにはsomeMethod();、そのメソッドのスコープ内から呼び出す必要があります。

別のクラスからメソッドを呼び出すには、通常、最初にそのクラスのinstanceまたはを作成する必要があります。objectクラスが呼び出され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 に答える