用Java实现:(1)定义一个Person类 成员变量:name,age 成员方法:1)构造方法,一个或多个;

2024-11-17 22:19:10
推荐回答(1个)
回答1:

public class Person {
private String name;
private int age;

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

public void sayHello() {
System.out.println("Hello!my name is"+this.name );
}

public void sayHello(Person p) {
System.out.println("Hello!您好:"+p.name );

}

public static void main(String[] args) {

Person p1 = new Person("张三");
Person p2 = new Person("李四");
p1.sayHello();
p2.sayHello(p2);

}
}