[英]Java println method from another class
我对另一个类的方法有问题
我要的方法是:
/**
* Prints out information about the owner
*/
public void printOwnerInfo()
{
System.out.println("Information about the owner of the forest");
System.out.println("Name: " + name);
System.out.println("Adresse: " + adresse);
System.out.println("Phone Number: " + phone);
}
我想在旁边打印此信息:
/**
* Prints out information about the forest
*/
public void printForestInfo()
{
System.out.println("Information about forest: " + name);
System.out.println("Location: " + location);
System.out.println("Square meters: " + squareMeters);
System.out.println("Price per square meter: " + price + " euro");
System.out.println("Price for the forest: " + (squareMeters * price) + " euro");
System.out.println.forestOwner.printOwnerInfo();
}
我在“森林”字段中将“所有者”类声明为:
public Owner forestOwner;
但是我似乎无法用林信息打印出所有者信息。 他们每个人都能很好地工作。
这没有道理
System.out.println.forestOwner.printOwnerInfo();
我猜你想从对象forestOwner
打印方法printOwnerInfo()
所以:-
forestOwner.printOwnerInfo();
它将在对象上调用方法,并在Println
处理Println
。
该调用没有意义:
System.out.println.forestOwner.printOwnerInfo();
仅当printOwnerInfo()
返回一个String
(然后将其传递到System.out.println
调用中)并且更正了调用链时, printOwnerInfo()
开始变得明智。 因此,用以下代码替换该行:
forestOwner.printOwnerInfo()
...或者让该所有者方法返回一个String
,然后像这样调用它:
System.out.println(forestOwner.printOwnerInfo());
我认为您需要在printForestInfo方法中执行以下操作:
forestOwner.printOwnerInfo();
...当您的代码从该方法中打印到控制台时。 System类的'out'字段声明为static和public,因此在全局范围内“可用”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.