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

发布于 2025-01-01 02:50:41 字数 2428 浏览 5 评论 0 原文

我有两个问题 - 我是 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 文件并将其分离

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

I have two questions - I'm a beginner in Java but have a huge java project :(

  1. If I implement a class (which is an interface that has two void methods) and I try to write another method in my class, it compiles but at run time the method is skipped? What could I be doing wrong?

  2. How do I run a class from another class that is not the main. So basically the main runs a class which then calls another class...

I know these questions are pretty limited in terms of content but the program is really complexed at its just impossible to explain everything :(

Any help would be greatly Appreciated :) Thanks!!

--- updated - as requested

 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;
    }
}
  • Describe class Communication here.
  • Abstract superclass of all the communication
  • subclasses.
  • in effect - processes an XML file and separates it

    • Direct extends communications // it basically one of the String Values in the XML file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2025-01-08 02:50:41

如果您向类添加另一个方法,但该方法未在接口中定义,并且您在实例化对象时使用接口作为类型,那么 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

If you add another method to your class, but that method is not defined in your interface, and you're using the Interface as the type when instantiating the object, then the Java runtime will not be aware that method exists.

For instance:

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();

}

Now, if a service class in your code returns an object of type "List", then your calling class won't know the subtype:

 List list = getMyList();

 list.method3();  // doesn't know what this is because it's type List not type MyClass
撩动你心 2025-01-08 02:50:41

您必须调用一个方法来运行它,所以假设您有一个名为

public void someMethod(){ 
   // method contents
}

为了运行它,您必须从该方法的 范围

要从另一个类调用方法,您通常必须首先创建该类的实例对象。假设该类名为 OtherClass,该类中有一个名为 otherMethod() 的方法,在当前类中,您必须创建一个 类型的对象OtherClass 像这样:

OtherClass otherClass = new OtherClass();

然后使用调用其他方法

otherClass.otherMethod();

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

You have to call a method to run it, so let's say you have a method called

public void someMethod(){ 
   // method contents
}

in order to run it you'll have to call someMethod(); from within that method's scope.

To call a method from another class you generally have to create an instance or object of that class first. Let's say the class is called OtherClass and there's a method in that class called otherMethod(), in your current class you'll have to create an object of type OtherClass like this:

OtherClass otherClass = new OtherClass();

then call the other method using

otherClass.otherMethod();

If this answer is too simple let us know. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文