为什么我的方法只是跳过?

时间:2012-01-29 00:50:53

标签: java interface extends implements

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

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

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

  3. 我知道这些问题在内容方面非常有限,但程序真的很复杂,因为它无法解释所有内容:(

    任何帮助都会得到很大的赞赏:)谢谢!!

    ---更新 - 按要求

     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文件中的一个字符串值

2 个答案:

答案 0 :(得分:1)

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

public void someMethod(){ 
   // method contents
}

要运行它,您必须在该方法的scope内调用someMethod();

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

OtherClass otherClass = new OtherClass();

然后使用

调用另一个方法
otherClass.otherMethod();

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

答案 1 :(得分:1)

如果在类中添加了另一个方法,但是在接口中没有定义该方法,并且在实例化对象时使用了Interface作为类型,则Java运行时将不会意识到该方法存在。 / p>

例如:

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
相关问题