[英]Calling a method from another class in Java?
I have two classes below and I want to call the SetPlace method in my main program. 我下面有两个类,我想在主程序中调用SetPlace方法。
public class Place
{
//variables
//constructors
//methods
private String place; //only accessible within the class
public Place()
{
place = null; // strings initialised as null as it doesn;t refer to anything yet -1 for int
}
public void SetPlace()
{
Place p1 = new Place();
p1.place="Dungeon";
}
}
public class Main {
public static void main (String []args)
{
Place p1 = new Place();
}
}
How do I pass the SetPlace method to the Main Method and print out "Dungeon". 如何将SetPlace方法传递给Main方法并打印出“地牢”。
Any assistance is welcomed! 欢迎任何帮助!
You're going to have to create an instance of that class like: Place place = new Place()
and then place.SetPlace()
. 您将必须创建该类的实例,例如:
Place place = new Place()
然后place.SetPlace()
。
Or you can make that function static
so the it can be called within main without having to create a new instance of it. 或者,您可以将该函数设为
static
以便可以在main中调用它而不必创建该函数的新实例。 Note that setters probably shouldn't be static, but if you want to access a function from another class, this is how to do it. 注意,setter可能不应该是静态的,但是如果您想从另一个类访问函数,这就是这样做的方法。
You just need to write the calling statement in your main method like this: 您只需要在您的main方法中编写调用语句,如下所示:
public static void main (String []args)
{
Place p1 = new Place();
p1.SetPlace();// added here
}
But you may have made two mistakes: writing the SetPlace
in a wrong way, and lacking of "print" statement. 但是您可能犯了两个错误:以错误的方式编写
SetPlace
,以及缺少“打印”语句。
a setter usually looks like this: setter通常看起来像这样:
SetPlace(String place){
this.place = place; // instead of creating a new instance of Place
}
and you can use System.out if you want to print something onto console: 如果要将某些内容打印到控制台上,可以使用System.out :
// write this method in class Place
String getPlace(){
return place;
}
...
// call it in Main
System.out.println(p1.getPlace());
It doesn't work like that. 它不是那样工作的。 You can't pass the method as a parameter but refence to your objects/instances.
您不能将方法作为参数传递,而只能引用您的对象/实例。 In your case since you already created an instance you can just call
在您的情况下,由于您已经创建了一个实例,您只需调用
p1.SetPlace();
Also if you want to print it to console you should write a toString() method in your Place class then call it with a System.out call like following 另外,如果您想将其打印到控制台,则应在Place类中编写toString()方法,然后使用System.out调用进行调用,如下所示
public String toString()
{
return place;
}
in main method 主要方法
System.out.println(p1);
If you want to get the value of The String place in order to print it, you'll also need to create a getPlace() method that returns place. 如果要获取The String place的值以进行打印,则还需要创建一个getPlace()方法来返回place。
And in the main method you do 而在主要方法中
p1.SetPlace();
System.out.println(p1.getPlace());
Create setter method for place. 创建放置方法。
Note :- as per Java guidelines ...method name must start with small character and must follow camel case. 注意:-按照Java准则...方法名称必须以小写字母开头,并且必须遵循驼峰式大小写。
public void setPlace(String place)
{
this.place=place;
}
Create getter method for place and to get value call this method using reference variable of Place class. 创建place的getter方法并获取值,使用Place类的引用变量调用此方法。
public String getPlace()
{
return place;
}
Now from Main class call setter method. 现在从Main类调用setter方法。
Place p1 = new Place();
p1.setPlace("New York");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.