zoukankan      html  css  js  c++  java
  • 16.抽象类

    package cn.jbit.epet.override;
    /**
     * 宠物类,狗狗和企鹅的父类
     */
    public abstract class Pet {
        private String name = "无名氏";// 昵称
        private int health = 100;// 健康值
        private int love = 20;// 亲密度
        
        /**
         * 无参构造方法
         */
        public Pet() {
        }
        /**
         * 有参构造方法
         * @param name  昵称
         */
        public Pet(String name) {
            this.name = name;
        }
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getHealth() {
            return health;
        }
    
        public void setHealth(int health) {
            if(health<0||health>100){
                System.out.println("健康值应该在0至100之间,默认值为60。");
                this.health=60;
                return;
            }
            this.health = health;
        }
    
        public int getLove() {
            return love;
        }
    
        public void setLove(int love) {
            if(love<0||love>100){
                System.out.println("亲密度应该在0至100之间,默认值为10。");
                this.love=10;
                return;
            }
            this.love = love;
        }
    
        /**
         * 输出宠物信息
         */
        public void print() {
            System.out.println("宠物的自白:
    我的名字叫" + 
                    this.name + ",我的健康值是" + this.health 
                    + ",我和主人的亲密程度是" + this.love + "。");
        }
        public abstract void toHospital();
    }
    package cn.jbit.epet.override;
    
    /**
     * 狗狗类,宠物的子类。
     */
    public class Dog extends Pet {
        private String strain="吉娃娃";// 品种
        
        public Dog(){}
    
        /**
         * 有参构造方法。
         * @param name   昵称
         * @param strain   品种
         */
        public Dog(String name, String strain) {
            super(name); 
            this.strain = strain;
        }
        
        public void setStrain(String strain) {
            this.strain = strain;
        }
        public String getStrain() {
            return strain;
        }
        
        public void print(){
            super.print();
            System.out.println("我是一只"+this.getStrain()+"犬。");
        }
        
        public void toHospital() {
            this.setHealth(60);
            System.out.println("打针、吃药");
        }
    
    }
    package cn.jbit.epet.override;
    
    public class Master {
        // 给宠物看病
        public void cure(Pet pet) {
            if (pet.getHealth() < 50)
                pet.toHospital();        
        }
    }
    package cn.jbit.epet.override;
    /**
     * 企鹅类,宠物的子类。
     */
    public class Penguin extends Pet {
        private String sex;// 性别
        /**
         * 有参构造方法。
         * @param name 昵称
         * @param sex 性别
         */
        public Penguin(String name, String sex) {
            super(name);
            this.sex = sex;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }    
        
        public void print(){
            super.print();
            System.out.println("我的性别是"+this.getSex()+"。");
        }
        
        public void toHospital() {
            this.setHealth(70);
            System.out.println("吃药、疗养");
        }
    }
    package cn.jbit.epet.override;
    
    public class Test {
        public static void main(String[] args) {
            Pet dog=new Dog();
            dog.setHealth(10);
            dog.print();
            System.out.println("*************************");
            Master master=new Master();
            master.cure(dog);
            dog.print();
        }
    }
  • 相关阅读:
    数据库chapter 4 数据库安全性
    数据库 chapter 2 关系数据库
    数据库 chapter 1 概论
    操作系统 chapter 11 I/O系统
    操作系统 chapter 12 死锁
    操作系统 chapter 7 8 存储模型
    聊一聊移动调试那些事儿
    获取当前日期和农历的js代码
    使用 CSS 媒体查询创建响应式网站
    大前端工具集
  • 原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536494.html
Copyright © 2011-2022 走看看