java面向对象的继承Person

(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());
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值