(1)声明父类:Person类
* - 包含属性:姓名,年龄,性别,属性私有化,
* - 包含get/set方法
* - 包含getInfo()方法:例如:姓名:张三,年龄:23,性别:男
(2)声明子类:Student类,继承Person类
* - 新增属性:score成绩,属性私有化,
* - 包含get/set方法
* - 重写getInfo()方法:例如:姓名:张三,年龄:23,性别:男,成绩:89
(3)声明子类:Teacher类,继承Person类
* - 新增属性:salary薪资,属性私有化,
* - 包含get/set方法
* - 重写getInfo()方法:例如:姓名:张三,年龄:23,性别:男,薪资:10000
(4)在测试类的main方法中创建三个类的对象,并调用相应的方法测试
public class Person {
private String name;
private int age;
private String gender;
public Person() {
}
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
//getMsg()方法
public String getInfo(){
return "姓名:"+name+" ,年龄:"+age+" ,性别:"+gender;
}
}
public class Student extends Person{
private double score;
public Student() {
}
public Student(String name, int age, String gender, double score) {
super(name, age, gender);
this.score = score;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String getInfo() {
return super.getInfo()+",成绩:"+score;
}
}
public class Teacher extends Person {
private double salary;
public Teacher() {
}
public Teacher(String name, int age, String gender, double salary) {
super(name, age, gender);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String getInfo() {
return super.getInfo() + ",薪资:" + salary;
}
}
public class TestPerson {
public static void main(String[] args) {
Person p = new Person("张三", 23, "男");
Student s = new Student("张三", 23, "男", 89);
Teacher t = new Teacher("张三", 23, "男", 10000);
System.out.println(p.getInfo());
System.out.println(s.getInfo());
System.out.println(t.getInfo());
}
}