如何从 Java 中的另一个类调用整个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38522078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call an entire class from another class in Java?
提问by Goldengirl
I am a beginner in Java. I have two packages in my current project. Each of the packages have two classes called the "packageClassOne" and "packageClassTwo".
我是 Java 的初学者。我当前的项目中有两个包。每个包都有两个类,称为“packageClassOne”和“packageClassTwo”。
The packageClassTwo has a constructor and other public methods.
packageClassTwo 有一个构造函数和其他公共方法。
I want to call the PackageClassTwo from an if statment located in the PackageClassOne. My code looks something like this
我想从位于 PackageClassOne 中的 if 语句调用 PackageClassTwo。我的代码看起来像这样
packageClassOne:
包类一:
public class packageClassOne {
public void selectComponent(boolen) {
if(/* check condition*) {
packageClassTwo value = new packageClassTwo();
}
}
}
packageClassTwo:
包类二:
public class packageClassTwo {
public packageClassTwo(String name){ //Constructor
int length = name.length();
}
public String CreateWord(){
/*functionality ofthis method*/
}
public String CreateSentence(){
/*functionality ofthis method*/
}
}
The problem is that everytime I call the packageClassTwo from my packageClassOne it tries to call the constructor instead of calling the class itself. I want to call the entire packageClassTwo instead of just the constructor.
问题是每次我从 packageClassOne 调用 packageClassTwo 时,它都会尝试调用构造函数而不是调用类本身。我想调用整个 packageClassTwo 而不仅仅是构造函数。
Can somebody help me please? Thank you in advance for your help
有人可以帮我吗?预先感谢您的帮助
回答by milez
Since Java is an object oriented language, you have to have a mindset of dealing with instances that are realizations of the classes you defined. These are the objects.
由于 Java 是一种面向对象的语言,因此您必须具有处理实例的心态,这些实例是您定义的类的实现。这些是对象。
So if you want to call a method from packageClassTwo
class, you first create an object of packageClassTwo
. You seem to be trying to do just this. Once you have the object, you can call its methods. For example
所以如果你想从packageClassTwo
类中调用一个方法,你首先要创建一个packageClassTwo
. 你似乎正试图做到这一点。一旦你有了对象,你就可以调用它的方法。例如
//Instantiate an object by calling the constructor
packageClassTwo object = new packageClassTwo(string);
//Now call its methods
String val = object.CreateWord()
There is no such thing as "calling a class". You call methods of objects of a class.
没有“调用类”这样的东西。您调用类的对象的方法。
Occasionally, there might be a well founded need to call methods of a class without initializing objects. Look into static methods and classesfor further reading.
有时,可能需要在不初始化对象的情况下调用类的方法。查看静态方法和类以进一步阅读。
回答by noscreenname
If you want to call all methods of packageClassTwo
you have to do it explicitly
如果你想调用你的所有方法,packageClassTwo
你必须明确地做
packageClassTwo pct = new packageClassTwo("");
pct.CreateWord();
pct.CreateSentence();
If you allways want the 2 methods to be called when you create a new packageClassTwo object, than you can just add the calls to the constructor
如果您总是希望在创建新的 packageClassTwo 对象时调用这 2 个方法,那么您只需将调用添加到构造函数
public packageClassTwo(String name) {
int length = name.length();
pct.CreateWord();
pct.CreateSentence();
}
Edit:
编辑:
Note that in the second case, if you end up only calling the 2 methods from inside the constructor, it is better to make them private.
请注意,在第二种情况下,如果您最终只从构造函数内部调用这 2 个方法,最好将它们设为私有。
As a sidenote, it is a general convention in java to have class names start with a upper case letter : PackageClassTwo
not packageClassTwo
, and method names to start with lower case createWord
not CreateWord
. This wll make your code more readable.
作为旁注,Java 中的一般约定是类名以大写字母 : PackageClassTwo
not开头packageClassTwo
,方法名以小写createWord
not开头CreateWord
。这将使您的代码更具可读性。
回答by wero
I don't think your code will run without compiling errors.because you did not declare the constructor packageClassTwo()
.
我认为您的代码不会在没有编译错误的情况下运行。因为您没有声明构造函数packageClassTwo()
。
回答by gygabyte
If you want to call all the methods from the packageClassTwo, call them from the packageClassTwo constructor
如果要调用 packageClassTwo 中的所有方法,请从 packageClassTwo 构造函数中调用它们
public packageClassTwo(String name)
{
int length = name.length();
CreateWorld();
CreateSentence();
}