简体   繁体   English

一个类的调用方法以使用另一个类的实例

[英]Call method of a class to use instance of another class

I'm more or less new to Java. 我或多或少是Java的新手。 My intention is to "export" methods of one class into another class (to have a neat structure of all the methods working with one instance). 我的意图是将一个类的方法“导出”到另一类(以使使用一个实例的所有方法都具有整齐的结构)。 My problem is that I cannot establish the necessary connection between the instance of my currently overloaded class and the additional methods I put into another class. 我的问题是,我无法在当前重载的类的实例与放入另一个类的其他方法之间建立必要的连接。

So far I've initiated an instance of my class Graph and put methods that are just supposed to get values of that instance into Readings . 到目前为止,我已经启动了Graph类的实例,并将应该用来获取该实例值的方法放入Readings Since I could not figure out much I initiated and instance of Readings in Class , created a method in Graph calling a method in Readings . 由于无法确定太多内容,因此在Class启动了Readings实例,并在Graph创建了一个方法,在Readings调用了该方法。 So far, so good (I assume) but the problem is that the method in Readings does not really know how to handle the getters of my Graph -Instance/where to access the getters. 到目前为止,还算不错(我假设),但是问题在于Readings中的方法并不真正知道如何处理我的Graph -Instance /在哪里获取吸气剂。

static Graph graphToBeWorkedOn;

in the Readings class does not really help and all I get is a nullpointer. Readings类中并没有真正的帮助,我得到的只是一个空指针。

Thanks in advance for any advices/help! 在此先感谢您的任何建议/帮助!

Edit: Graph : 编辑: Graph

...
Readings readingsVariable = new Reading();
...

public List<SpecificNode> methodToBeCalled(int numberInput) {

List<SpecificNode> listOfMethod = new ArrayList<SpecificNode>();
listOfMethod = readingsVariable.methodOne(numberInput);
return listOfMethod;
}

Readings : Readings

...
Graph graphToBeWorkedOn;
...

public List<SpecificNode> methodOne(int numberInput) {

List<SpecificNode> listOfMethod = new ArrayList<SpecificNode>();
...
// fails here with a nullPointer:
graphToBeWorkedOn.getSpecificNode(numberInput)
...


return listOfMethod;
}

Initilized a Graph in a test-main and attempting to call 在测试主体中初始化Graph并尝试调用

System.out.println(testGraph.methodOne(2));

From your provided code, you never initialize the Graph object. 从您提供的代码中,您永远不会初始化Graph对象。

You'll need to do 你需要做

public List<SpecificNode> methodOne(int numberInput) {
graphToBeWorkedOn = new Graph(); // Initialize Graph
List<SpecificNode> listOfMethod = new ArrayList<SpecificNode>();
...
// Now you can call the method
graphToBeWorkedOn.getSpecificNode(numberInput)
...


return listOfMethod;
}

Simple solution: use a Readings constructor that takes a Graph object as parameter and use it to initialize your graphToBeWorkedOn field: 一个简单的解决方案:使用一个以Graph对象作为参数的Readings构造函数,并使用它来初始化graphToBeWorkedOn字段:

in Graph: 在图表中:

readingsVariable = new Reading(this);

in Readings: 在阅读中:

Readings(Graph graphToBeWorkedOn) {
    this.graphToBeWorkedOn = graphToBeWorkedOn;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过另一个 class 的方法调用实例变量的 class - How to call a class of instance variables through a method of another class 如何从Android中的另一个类调用预定义类中的实例方法 - How to call instance method in predefined class from another class in android 尝试调用另一个类中的方法以在 IF 语句中使用 - Trying to call a method in another class to use in a IF statement 如何在另一个实例的自身方法中使用类的实例? - How to use instance of a class in its own method for another instance? 在另一个类中调用一个方法 - Call a method in another class 如何在硒的另一个类中使用一个类的实例 - How to use an instance of a class in an another class in selenium 如何实现OOP来从一个类调用一个实例方法以存储在另一个类中另一个实例方法的数组中? - How can OOP be implemented to call an instance method from one class to be stored in an array in another instance method in a different class? 如何调用另一个类的方法? - How to call method of the another class? 如何使用 ArrayList 从 Java 中的另一个类调用方法? - How to use ArrayList to call a method from another class in Java? 如何使用按钮从另一个类调用方法? - How to use a button to call a method from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM