Java面向对象抽象类实例

package hah;
 abstract class Person{
    private String name ;
    private int age;
    public Person(String name ,int age ){
        this.name=name;
        this.age =age;
    }
    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 abstract void Want();//把彼此不同的属性声明成抽象方法
}
 class Student extends Person{
     private int  score;//学生的私有属性

    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }         //构造方法的复写,并添加私有属性的参数
    public Student(String name, int age, int score) {
        super(name, age);
        this.score =score;
    }
    public  void Want() {//复写抽象方法
    System.out.println("姓名:"+getName()+"    年龄:"+getAge()+"    成绩:"+getScore());
    }
    }
 class Worker extends Person {
        private int money;
    public Worker(String name, int age,int money) {
        super(name, age);
        this.money=money;
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public  void Want(){
        System.out.println("姓名:"+getName()+"    年龄:"+getAge()+"    成绩:"+getMoney());
    }

 }
public class try01 {

    public static void main(String[] args) {
    //分别进行实例化   
        Student student=new Student("阿飞", 20, 100);
        student.Want();//通过 student调用want方法
        Worker worker=new Worker("阿", 30, 50000);
        worker .Want();//调用want方法
    }

}
输出:------------姓名:阿飞    年龄:20    成绩:100
                 姓名:  阿    年龄:30    成绩:50000
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值