Java基础 4.26

1.访问修饰符细节

java 复制代码
package com.logic.modifier;

public class A {
    public int n1 = 100;
    protected int n2 = 200;
    int n3 = 300;
    private int n4 = 400;
    public void m1() {
        //在同一个类中 可以访问public protected 默认 private 修饰属性和方法
        System.out.println(n1 + " " + n2 + " " + n3 + " " + n4 + "\t");
    }
    protected void m2() {}
    void m3() {}
    private void m4() {}
    public void hi() {
        //在同一类中 可以访问public protected 默认 private 修饰属性和方法
        m1();
        m2();
        m3();
        m4();
    }

}
java 复制代码
package com.logic.modifier;

public class B {
    public void foo() {
        A a = new A();
        //在同一个包下,可以访问public protected 和 默认修饰符属性或方法 不能访问private 属性
        System.out.println(a.n1 + " " + a.n2 + " " + a.n3);
        a.m1();
        a.m2();
        a.m3();
    }
}
java 复制代码
package com.logic.modifier;

public class Test {
    public static void main(String[] args) {
        A a = new A();
        a.m1();
        B b = new B();
        b.foo();
    }
}
java 复制代码
package com.logic.pkg;

import com.logic.modifier.A;

public class Test {
    public static void main(String[] args) {
        A a = new A();
        //在不同包下 可以访问public 修饰符的属性或方法
        //但是不能访问 protected 默认 private修饰的属性或方法
        System.out.println(a.n1);
        a.m1();
    }
}

2.封装

封装介绍

  • 封装就是把抽象出的数据(属性)和对数据的操作(方法)封装在一起,数据被保护在内部,程序的其他部分只有通过被授权的操作(方法),才能对数据进行操作
  • 对电视的操作就是典型的封装(用户不用知道怎么开机的 只用按下开机键)

封装的理解和好处

隐藏实现细节 方法 <-- 调用

可以对数据进行验证 保证安全合理

封装的实现步骤(三步)

  • 将属性进行私有化 private(不能直接修改属性)
  • 提供一个公共的(public)set方法 用于对属性判断并赋值
java 复制代码
public void setXxx(类型 参数名) {
    //加入数据验证的业务逻辑
    属性 = 参数名;
}
  • 提供一个公共的(public)get方法 用于获取属性的值
java 复制代码
public 数据类型 getXxx() {
    //权限判断, Xxx某个属性
    return xxx;
}

封装快速入门

java 复制代码
package com.logic.encap;

import jdk.nashorn.internal.ir.CallNode;

public class Encapsulation01 {
    public static void main(String[] args) {
        //如果要使用快捷键alt + r 需要先配置主类
        //第一次 我们使用鼠标点击形式运算程序 后面直接可用快捷键
        Person person = new Person();
        person.setAge(20);
        person.setName("John");
        person.setSalary(10000);
        System.out.println(person.info());
    }
}
/*
不能随便查看人的年龄 工资等隐私 并对设置的年龄进行合理的验证 年龄合理就设置 否则给默认
年龄 年龄必须在1-120 工资不能直接查看 name的长度在2-6字符之间
 */

class Person {
    public String name;//名字公开
    private double salary;//薪水保密
    private int age;//年龄保密
    //自己写setXxx 和 getXxx 太慢 使用快捷键 alt + insert
    //根据要求完善代码
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        //加入对数据的校验 相当于增加业务逻辑
        if (age >= 1 && age <= 120) {
            this.age = age;
        } else {
            System.out.println("你设置的年龄需要在1-120之间");
            this.age = 18;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.length() >= 2 && name.length() <= 6) {
            this.name = name;
        } else {
            System.out.println("名字长度不符合要求,要在2-6个字符之间,将默认无名");
            this.name = "无名";
        }
    }

    public double getSalary(int password) {
        if (password == 123456) {
            return salary;
        } else {
            System.out.println("你输入的密码不正确");
            return -1;
        }
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    //写一个方法 返回属性信息
    public String info() {
        return "Name: " + name + ", Age: " + age + ", Salary: " + salary;
    }
}

将构造器与封装结合

java 复制代码
public Person(String name, double salary, int age) {
//        this.name = name;
//        this.salary = salary;
//        this.age = age;
        setSalary(salary);
        setAge(age);
        setName(name);
//这样就能调用之前设置的初始化方法,无法绕过验证
    }

封装练习

java 复制代码
package com.logic.encap;

import java.util.Scanner;

/*
创建程序 在其中定义两个类 Account 和 AccountTest
1.Account类要求具有属性 姓名(长度为2-4位)余额(>20)
密码(必须是6位) 如果不满足 给出提示信息并给默认值
2.通过set方法给Account的属性赋值
3.在AccountTest中测试
 */
public class Account {
    private String name;
    private double balance;
    private String password;

    public Account() {
    }

    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("余额必须大于20,默认20");
            this.balance = 20;
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (password.length() == 6) {
            this.password = password;
        } else {
            System.out.println("密码必须是6位,默认000000");
            this.password = "000000";
        }
    }

    public void info() {
        //添加一个输入密码的步骤,加入权限检测环节
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入密码:");
        String infoPassword = sc.next();
        if (password.equals(infoPassword)) {
            System.out.println("name = " + name + " balance = " + balance + " password = " + password);
        } else {
            System.out.println("密码不正确,请重新输入!");
        }
    }
}
java 复制代码
package com.logic.encap;

public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account();
        account.setBalance(50);
        account.setName("jack");
        account.setPassword("123456");
        account.info();
    }
}
相关推荐
人类群星闪耀时5 分钟前
5G赋能远程医疗:从愿景到现实的技术变革
开发语言·5g·php
ℳ₯㎕ddzོꦿ࿐7 分钟前
Java集成Zxing和OpenCV实现二维码生成与识别工具类
java·opencv
雪落山庄12 分钟前
LeetCode100题
java·开发语言·数据结构
码熔burning32 分钟前
【MQ篇】RabbitMQ之发布订阅模式!
java·分布式·rabbitmq·mq
XiaoLeisj40 分钟前
【设计模式】深入解析代理模式(委托模式):代理模式思想、静态模式和动态模式定义与区别、静态代理模式代码实现
java·spring boot·后端·spring·设计模式·代理模式·委托模式
FAREWELL000751 小时前
C#进阶学习(十四)反射的概念以及关键类Type
开发语言·学习·c#·反射·type
李少兄1 小时前
解决Spring Boot版本冲突导致的`NoSuchFieldError`
java·spring boot·后端
NicOym1 小时前
C++ 为什么建议类模板定义在头文件中,而不定义在源文件中
开发语言·c++
种时光的人1 小时前
2025蓝桥省赛c++B组第二场题解
开发语言·c++·算法
pwzs1 小时前
常见的 Spring Boot 注解汇总
java·spring boot·后端·spring