package day21; // 父类 父类有两个子类 class Person{ //定义三个私有属性 之后需要get/set方法进行调用和设置 // 1 usage private String name; private int age; private char gender; public Person(){} //构造方法﹑实例对象初始化 public Person(String name, int age,char gender) { super(); // this.name是对象的name;等号右边的传过来的参数 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 char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } //获取对象的所有信息 public String getInfo(){ return"姓名:" + name + ",年龄:" + age + ",性别:" + gender; } } // 学生子类 class Student extends Person{ private double score; public Student(){}//空构造参数 //有参数的构造参数 public Student(String name, int age,char gender, double score){ setName (name) ; setAge(age); setGender(gender); this.score = score; } public void setScore(double score) { this.score = score; } public double getScore(){ return score; } public String getInfo(){ return super.getInfo() + ",分数:" + score; } } // 老师子类 class Teacher extends Person { private int salary; public Teacher(){} public Teacher(String name, int age,char gender, int salary){ setName(name); setAge(age); setGender(gender);this.salary = salary; } public void setSalary(int salary) {this.salary = salary;} public int getSalary(){return salary; } public String getInfo(){ return super.getInfo() + ",薪资: " + salary; } } public class TestPerson { public static void main(String[ ] args) { Person p = new Person( "周浩", 38, '女' ); System.out.println(p.getInfo());//获取并打印信息 Student s = new Student( "罗杰" , 39,'男', 98.5); System.out.println(s.getInfo()); Teacher t = new Teacher( "张雪峰", 40, '男' , 11000); System.out.println(t.getInfo()); } }