写一个人类,有成员用java写属性:姓名、年龄、性别,有两个成员方法
时间: 2024-05-19 19:13:28 浏览: 130
java代码-使用java解决在Person类的基础上,添加一个带参数的构造函数,参数分别为姓名,性别,年龄的源代码
public class Person {
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void introduce() {
System.out.println("My name is " + name + ", I am " + age + " years old, and I am " + gender + ".");
}
public void haveBirthday() {
age++;
System.out.println("Happy birthday to me! I am now " + age + " years old.");
}
}
阅读全文