zoukankan      html  css  js  c++  java
  • java面向对象抽象类应用

    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 Sthdent extends Person {
        private int score;
    
        public Sthdent(String name, int age, int score) {
            super(name, age);
            this.score = score;
        }
    
        public int getScore() {
            return score;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
        @Override
        public void want() {
            System.out.println(getName() + " 年龄:" + getAge()+" 想要" + getScore()+"分");
        }
    }
    
    class Work extends Person {
        private int money;
    
        public Work(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;
        }
        @Override
        public void want() {
            System.out.println(getName() + " 年龄:" + getAge()+" 想要" + getMoney()+"元");
        }
    }
    
    
    public class AbsDemo {
        public static void main(String agrs[]) {
           Sthdent sthdent =  new Sthdent("张三",20,100);
           sthdent.want();
            Work work = new Work("李四",42,50000);
            work.want();
    
        }
    }
  • 相关阅读:
    3185 队列练习 1 3186 队列练习 2
    1063 合并果子
    堆排序
    奇怪的电梯
    3411 洪水
    2010 求后序遍历
    1729 单词查找树
    3137 栈练习1
    2821 天使之城
    括弧匹配检验(check.cpp)
  • 原文地址:https://www.cnblogs.com/sflik/p/4543055.html
Copyright © 2011-2022 走看看