简体   繁体   中英

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();

Which will call the method on the object and the Println s are handled in there

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. So, either replace that line with this:

forestOwner.printOwnerInfo()

... or have that owner method return a String instead and call it like so:

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

I think you need to do something like the following in the printForestInfo method:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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