为什么我的方法只是跳过?
我有两个问题 - 我是 Java 初学者,但有一个巨大的 java 项目:(
-
如果我实现一个类(这是一个具有两个 void 方法的接口)并且我尝试在我的类中编写另一个方法,它可以编译,但在运行时会跳过该方法?我可能做错了什么?
-
如何从不是主类的另一个类运行一个类,所以基本上主类运行一个类,然后调用另一个类...
我知道这些问题的内容非常有限,但该程序确实很复杂,无法解释所有内容:(
任何帮助都会非常感谢:) 谢谢!!
--- 按要求更新
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 文件中的字符串值之一
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您向类添加另一个方法,但该方法未在接口中定义,并且您在实例化对象时使用接口作为类型,那么 Java 运行时将不会知道该方法的存在。
例如:
现在,如果代码中的服务类返回“List”类型的对象,那么您的调用类将不知道子类型:
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:
Now, if a service class in your code returns an object of type "List", then your calling class won't know the subtype:
您必须调用一个方法来运行它,所以假设您有一个名为
为了运行它,您必须从该方法的 范围。
要从另一个类调用方法,您通常必须首先创建该类的
实例
或对象
。假设该类名为OtherClass
,该类中有一个名为otherMethod()
的方法,在当前类中,您必须创建一个类型的对象OtherClass
像这样:然后使用调用其他方法
如果这个答案太简单,请告诉我们。 :)
You have to call a method to run it, so let's say you have a method called
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
orobject
of that class first. Let's say the class is calledOtherClass
and there's a method in that class calledotherMethod()
, in your current class you'll have to create an object of typeOtherClass
like this:then call the other method using
If this answer is too simple let us know. :)