简体   繁体   English

来自另一个类的Java println方法

[英]Java println method from another class

I'm having a bit of a problem with a method from another class 我对另一个类的方法有问题

The method i want to is: 我要的方法是:

/**
 * 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);
}

I would like to have this information printed along side: 我想在旁边打印此信息:

/**
 * 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();
}

I've declared the "Owner" class in the "Forest" field as: 我在“森林”字段中将“所有者”类声明为:

public Owner forestOwner;

But I can't seem to get it to print out the Owner information with the Forest information. 但是我似乎无法用林信息打印出所有者信息。 they work fine each on there own. 他们每个人都能很好地工作。

This makes no sense 这没有道理

System.out.println.forestOwner.printOwnerInfo();

I guess you want to print from the object forestOwner the method printOwnerInfo() so:- 我猜你想从对象forestOwner打印方法printOwnerInfo()所以:-

forestOwner.printOwnerInfo();

Which will call the method on the object and the Println s are handled in there 它将在对象上调用方法,并在Println处理Println

This call doesn't make sense: 该调用没有意义:

System.out.println.forestOwner.printOwnerInfo();

This would only start to be sensible if printOwnerInfo() returned a String (which you're then passing into your System.out.println call), and if you corrected the chain of calls. 仅当printOwnerInfo()返回一个String (然后将其传递到System.out.println调用中)并且更正了调用链时, printOwnerInfo()开始变得明智。 So, either replace that line with this: 因此,用以下代码替换该行:

forestOwner.printOwnerInfo()

... or have that owner method return a String instead and call it like so: ...或者让该所有者方法返回一个String ,然后像这样调用它:

System.out.println(forestOwner.printOwnerInfo());

I think you need to do something like the following in the printForestInfo method: 我认为您需要在printForestInfo方法中执行以下操作:

forestOwner.printOwnerInfo();

... as your code prints to the console from within that method. ...当您的代码从该方法中打印到控制台时。 The 'out' field of the System class is declared static and public, thus it is "available" globally. System类的'out'字段声明为static和public,因此在全局范围内“可用”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM