Define classes and create objects (Java)

Define a class Person, define the name and age attributes, and define a parameterized constructor to initialize the name and age . Create two objects of this class in the test class, with names and ages of lili, 19 and lucy, 20 respectively , and print out the names and ages of the two objects on the screen.

Input format:
No input for this question

Output sample:
The corresponding output is given here. For example:
this person is lili,her age is 19
this person is lucy,her age is 20

public class Main  {
    
    
    public static void main(String[] args) {
    
    
        Person p1=new Person("lili", 19);
        Person p2=new Person("lucy", 20);
        System.out.println("this person is "+p1.name+",her age is "+p1.age);
        System.out.println("this person is "+p2.name+",her age is "+p2.age);
    }
}
class Person {
    
    
    String name;
    int age;

    public Person(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }
}

Guess you like

Origin blog.csdn.net/weixin_51430516/article/details/115120215