S
S
Sergey Sergey2021-04-28 23:36:58
Java
Sergey Sergey, 2021-04-28 23:36:58

Is it possible in this code to create another class with a method that would call the print() method to return values ​​and how to write it?

class Person {
    int age;
String name;
int height;
void print() {
System.out.println("Человек: "  +  "\nвозраст = "  + age + " лет " 
 +  " \nимя " + name + " \nрост " + height + " см ");
}
}

   class PersonTest {
            public static void main(String[] args){
    Person p1 = new Person();
    Person p2 = new Person();
        p1.age = 36;
        p1.name = "Jack";
        p1.height = 170;
        p1.print();
        p2.age = 25;
        p2.name = "Poll";
        p2.height = 150;
        p2.print();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Deiwan, 2021-04-29
@Sergei1111

Make the person static class, preferably refactor the construction to this.name=name; and so on,
the method is void, so it cannot return anything. (Remove the void) and try using constructors (much more convenient this way)

O
Orkhan, 2021-04-28
Hasanly @azerphoenix

Your print() method does not return a result (void). Accordingly, no matter how much you call the print () method in another method, it will not return the result.
Another thing is that you can overload the print method to return a value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question