2019年1月16日,面向对象-->封装

java文件分享地址:

链接:https://pan.baidu.com/s/18YothzcjSwJw1vfnhOF5eg 
提取码:abim 
复制这段内容后打开百度网盘手机App,操作更方便哦

作业:运动封装的方法

 创建企鹅类:

package com.LianXi;

public class Penguin {
    private String name;
    private int love;
    private int health;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getLove() {
        
        return love;
    }
    public void setLove(int love) {
        if(love>100||love<0) {
            System.out.println("亲密度输入有误,取默认值!");
            love=60;
        }
        this.love = love;
    }
    public int getHealth() {
        return health;
    }
    public void setHealth(int health) {
        if(health<0||health>100) {
            System.out.println("健康值输入有误,去默认值!");
            health =60;
        }
        this.health = health;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    public void showInfo() {
        System.out.println("企鹅的自白:");
        System.out.println("我的名字是:"+name+",健康值是:"+health+",和主人的亲密度是:"+love+",我的性别是:"+sex+"。");
        
        
    }
    

}

创建狗狗类:

package com.LianXi;

public class Dog {
    private String name;
    private int love;
    private int health;
    private String sex;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getLove() {
        return love;
    }
    public void setLove(int love) {
        if(love<0||love>100) {
            System.out.println("亲密度输入有误,取默认值!");
            love = 60;
        }
        this.love = love;
    }
    public int getHealth() {
        return health;
    }
    public void setHealth(int health) {
        if(health<0||health>100) {
            System.out.println("健康值输入有误,取默认值!");
            health = 60;
            
        }
        this.health = health;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    public void showInfo() {
        System.out.println("狗狗的自白:");
        System.out.println("我的名字是:"+name+",健康值是:"+health+",和主人的亲密度是:"+love+",我的性别是:"+sex+"。");
        
        
    }
    

}

main方法测试类:

package com.LianXi;

import java.util.Scanner;

public class PetTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("欢迎您来到宠物店!");
        System.out.print("\n请输入要领养的宠物名字:");
        String name = input.next();
        System.out.print("请选择要领养的宠物类型:(1、狗狗;2、企鹅)");
        int chose1 = input.nextInt();
        String pet = null;
        if(chose1 == 1) {
            pet ="狗狗";
        }else if(chose1 == 2){
            pet = "企鹅";
        }
        System.out.print("请选择"+pet+"的性别:(1、Q仔;2、Q妹)");
        String sex=null;
        int chose2 = input.nextInt();
        if(chose2==1) {
            sex = "Q仔";
        }else if(chose2==2) {
            sex = "Q妹";
        }else {
            System.out.println("输入有误!");
        }
        System.out.print("请输入"+pet+"的健康值(0~100之间):");
        int health = input.nextInt();
        System.out.print("请输入"+pet+"的亲密度(0~100之间):");
        int love = input.nextInt();
        
        switch(chose1) {
        case 1:
            Dog dog = new Dog();
            dog.setName(name);
            dog.setSex(sex);
            dog.setHealth(health);
            dog.setLove(love);
            dog.showInfo();
            break;
        case 2:
            Penguin p = new Penguin();
            p.setName(name);
            p.setSex(sex);
            p.setHealth(health);
            p.setLove(love);
            p.showInfo();
            break;
        default:
            System.out.println("您输入有误!");
        
        }
        
        
    }

}

输出结果为:

欢迎您来到宠物店!
请输入要领养的宠物名字:豆豆
请选择要领养的宠物类型:(1、狗狗;2、企鹅)1
请选择狗狗的性别:(1、Q仔;2、Q妹)2
请输入狗狗的健康值(0~100之间):121
请输入狗狗的亲密度(0~100之间):87
健康值输入有误,取默认值!
狗狗的自白:
我的名字是:豆豆,健康值是:60,和主人的亲密度是:87,我的性别是:Q妹。

笔记:

封装:
如何封装:
第一步,私有化属性
第二步,创建属性的方法--set/get;
第三步,在set/get方法中,设置传参或者取值的条件;

好处:
便于使用者正确使用系统,防止错误修改属性;
有助于系统之间的松耦合,提高系统独立性;
提高软件的可重用性;
降低了构建大型系统的风险;

猜你喜欢

转载自www.cnblogs.com/bozhu-liu/p/10279291.html