自学JAVA-封装

封装

1.封装的概念
    隐藏对象的属性和方法实现细节,仅对外公开用于操作内部数据的接口,
    控制程序中属性的读和修改的访问级别,
    比如你买了电脑,你会使用它,但你不知道它是怎么运行的
    
2.封装的好处
    隐藏实现细节,使用的人只需要知道传入什么参数,会产生什么效果,返回什么数据
    对属性进行操作时,可以验证传入数据的合理性
    把属性私有,定义公开的get、set方法来验证数据合理性
    
3.练习
    定义Account类要求具有属性:姓名name(长度为2-4,初始化后不可改变)、
    余额balance(必须大于20)、密码password(必须是六位)。如果不满足,则给出提示信息
    并给默认值(自己定)
    
package com.czw.app;

public class Cat {
    private String name=null;
    private int age=0;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if(name.length()>6){
            System.out.println("名称过长");
        }else {
            this.name = name;
        }
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}



import com.czw.app.Cat;

public class Hello {
    public static void main(String[] args) {

        Cat cat = new Cat();
        System.out.println(cat.getName());
        cat.setName("lbwnb");
        System.out.println(cat.getName());



    }
}


练习
    定义Account类要求具有属性:姓名name(长度为2-4,初始化后不可改变)、
    余额balance(必须大于20)、密码password(必须是六位)。如果不满足,则给出提示信息
    并给默认值(自己定)
    
    
import com.czw.app.Cat;

public class Hello {
    public static void main(String[] args) {

        Account account = new Account();
        account.setName("czw");
        account.setPassword("123456");
        account.setBalance(100);
        System.out.println(account.getName());
        System.out.println(account.getPassword());
        System.out.println(account.getBalance());


    }
}


public class Account {
    private String name = null;
    private double balance = 0;
    private String password = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name == null) {
            System.out.println("名称不能设置为null");
            return;
        }
        if (this.name != null) {
            System.out.println("名称只能修改一次");
        } else {
            if (name.length() < 2 || name.length() > 4) {
                System.out.println("名称长度需要2-4位");
            } else {
                this.name = name;
            }
        }
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        if (balance <= 20) {
            System.out.println("余额必须大于20");
        } else {
            this.balance = balance;
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (password == null) {
            System.out.println("密码不能设置为null");
            return;
        }
        if (password.length() == 6) {
            this.password = password;
        } else {
            System.out.println("密码必须为6位");
        }
    }
}

欢迎各位读者加入我们的交流群,进入公众号(Python三剑客)可获取加群方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值