DAY09:作业:使用封装的思想实现代码的健壮性


1:使用封装的思想实现对User,。username,password,age,sex,和age和性别做限制

年龄在0-130是合理的,超过这个范围给出默认值20

类:

    String username;
    int password;
    int age;
    String sex;
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    public int getPassword() {
        return password;
    }


    public void setPassword(int password) {
        this.password = password;
    }



    public int getAge() {
        return age;
    }


    public void setAge(int age) {
        if(age<0||age>130){
            this.age = 20;
            System.out.println("超出范围,默认值20");
        }else{
            this.age = age;
        }
        
    }



    public String getSex() {
        return sex;
    }



    public void setSex(String sex) {
        if(sex.equals("") || sex.equals("")){
            this.sex = sex;
        }else{
            System.out.println("年龄不合理");
        }
        
    }



    public void show(){
        System.out.println("用户名:"+username +"密码:"+password+"年龄:"+age+"性别:"+sex);
    }

调用:

    public static void main(String[] args) {
        User a = new User();
        a.setUsername("abcdef");
        a.setPassword(12345678);
        
        Scanner b = new Scanner(System.in);
        System.out.println("请输入年龄:");
        int c = b.nextInt();
        
        
        Scanner d = new Scanner(System.in);
        System.out.println("请输入性别:");
        String e = d.next();
        
        a.setAge(c);
        a.setSex(e);
        a.show();
        

        
    }

2.

类:

    private String name;
    private int health;
    private int lovely;
    private int pettype;
    private int petsex;
    public void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }
    
    public void setpettype(int pettype){
        this.pettype = pettype;
    }
    public int getpettype(){
        return this.pettype;
    }
    public void setpetsex(int petsex){
        this.petsex = petsex;
    }
    public int getpetsex(){
        return this.petsex;
    }
    
    
    
    public void setHealth(int health){
        if(health<0 || health>100){
            this.health = 60;
            System.out.println("健康值应在0至100之间,默认值为60");
        }else{
        this.health = health;
        }
    }
    
    public int getHealth(){
        return this.health;
    }
    
    
    public void setPet(int lovely){
        if(lovely<0 || lovely>100){
            this.lovely = 60;
            System.out.println("亲密度应在0至100之间,默认值为60");
        }else{
            this.lovely = lovely;
        }
        
    }
    
    public int getPet(){
        return this.lovely;
    }
    
    
    public void show(){
        if (pettype == 1){
        System.out.println("我的名字叫"+ name+",健康值是"+health+",和主人的亲密度是"+lovely+"我的性别是Q仔");
        }else{
            System.out.println("我的名字叫"+ name+",健康值是"+health+",和主人的亲密度是"+lovely+"我的性别是Q妹");
        }
    }

调用:

    public static void main(String[] args) {
        Pet a = new Pet();
        
        Scanner b = new Scanner(System.in);
        System.out.println("请输入要领养的宠物的名字:");
        String h = b.next();
        
        Scanner c = new Scanner(System.in);
        System.out.println("请输入要领养的宠物的宠物类型(1、狗狗 2、企鹅):");
        int i = c.nextInt();
        
        Scanner d = new Scanner(System.in);
        System.out.println("请选择企鹅的性别(1、Q仔 2、Q妹):");
        int j = d.nextInt();
        
        Scanner e = new Scanner(System.in);
        System.out.println("请输入宠物的健康值:");
        int k = e.nextInt();
        
        Scanner f = new Scanner(System.in);
        System.out.println("请输入宠物的亲密度:");
        int l = f.nextInt();
        
        a.setName(h);
        a.setpettype(i);
        a.setpetsex(j);
        a.setHealth(k);
        a.setPet(l);
        a.show();
            
        
    }

 

相关