Java学习 --- 面向对象三大特征之封装

目录

一、访问修饰符

 二、封装

三、参考案例

一、访问修饰符

Java提供了四种访问控制修饰符号,用于控制方法和属性(成员变量)的访问权限(范围)。

1、公开的(public):对外公开。

2、受保护(protected):对子类和同一包中的类公开

3、默认:没有修饰符号,向同一个包的类公开

4、私有(private):只有类本身可以访问,不对外公开

 二、封装

把抽象出的数据[属性]和对数据的操作[方法]封装在一起,数据被保护在内部,程序的其它部分只有通过被授权的操作[方法],才能对数据进行操作。

封装的好处:1、隐藏实现细节  2、可以对数据进行验证,保证安全合理。

封装的实现步骤:1、将属性进行私有化(private) 2、提供一个公共的(public)set方法,用于对属性判断并赋值  3、提供一个公共的(public)get方法,用于获取属性的值。

示例代码:

public class Encapsulation01 {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("小明");
        person.setAge(1200);
        person.setSalary(5600);
        person.info();
    }
}
class Person{
    public String name;
    private int age;
    private double salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length()>= 2 && name.length() <= 6){
            this.name = name;
        }else {
            System.out.println("你的名字过长或过短");
        }

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        //加入判断逻辑
        if (age >= 1 && age <= 120){
            this.age = age;
        }else {
            System.out.println("请输入正确的年龄");
        }

    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    //提供一个方法对外展示综合信息
    public void info(){
        System.out.println("姓名:"+name+"\t年龄:"+age+"\t工资"+salary);
    }
}

构造器:在给属性赋值时能通过构造器绕过

解决办法:

public Person(String name, int age, double salary) {
        //在构造器中使用set方法
        setName(name);
        setAge(age);
        setSalary(salary);
    }

三、参考案例

创建程序,在其中定义两个类:Account和AccountTest类体会Java的封装性。

1. Account类要求具有属性:姓名(长度为2位3位或4位)、余额(必须>20)、密码(必须是六位),如果不满足,则给出提示信息,并给默认值。

2.通过setXxx的方法给Account的属性赋值。

3.在AccountTest中测试。

Account类

public class Account {
    private String name;
    private double balance;
    private String password;

    public Account() {
    }
    //在构造器中调用set方法,防止跳过属性赋值检查
    public Account(String name, double balance, String password) {
        setName(name);
        setBalance(balance);
        setPassword(password);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length()>= 2 && name.length() <= 4){
            this.name = name;
        } else {
            System.out.println("请输入2~4长度的名字");
            this.name = "张三";
        }

    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        if (balance > 20){
            this.balance = balance;
        } else {
            System.out.println("余额不足");
        }

    }

    public String getPassword() {

        return password;
    }

    public void setPassword(String password) {
        if (password.length() == 6) {
            this.password = password;
        }else {
            System.out.println("密码长度为六位");
            this.password = "000000";
        }


    }
}

AccountTest类

public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account("老王", 100, "666666");
        System.out.println(account.getName());
        System.out.println(account.getBalance());
        System.out.println(account.getPassword());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸭鸭老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值