2022.10.8 继承属性七

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());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值