封装


封装:
封装是指隐藏对象的属性和实现细节,根据需要对外提供公共访问方式,迫使用户去使用这个方式去访问数据,使得代码更好维护.
作用:
提供程序的方便性,维护性,安全性.
操作:
私有属性,公有方法
私有属性: 使用private 修饰属性. private 修饰的代码,只能在当前类中被访问.
公有方法: 根据需要,提供属性的setter和getter方法.
setter方法:
1.setter方法用于设置对象的指定属性.
2.格式: public void set属性名(参数类型 参数名){} 比如: setName
备注: 官方建议setter方法的格式为set属性名,但可以自行定义方法名.
getter方式:
1.getter方法用于获得对象的指定属性的值
2.格式: public 返回值 get属性名(){}

public class Person {
    private String name;
    private char sex;
    private int age;
    private String address;

    public Person(String name, char sex, int age, String address) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.address = address;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
public class Test {
    public static void main(String[] args) {
        //创建对象
        User u1 = new User();
        //操作属性:
        /*u1.name="小黄";
        u1.sex='中';
        u1.age=10000;
        u1.address="火星";*/
        u1.setName("小黄");
        u1.setSex('中');
        u1.setAge(10000);
        u1.setAddress("火星");

        System.out.println("姓名:"+u1.getName()+" 性别:"+u1.getSex()+" 年龄:"+u1.getAge()+" 地址:"+u1.getAddress());
        System.out.println("姓名:"+u1.getName()+" 性别:"+u1.getSex()+" 年龄:"+u1.getAge()+" 地址:"+u1.getAddress());
        u1.setName("小绿");
        System.out.println("姓名:"+u1.getName()+" 性别:"+u1.getSex()+" 年龄:"+u1.getAge()+" 地址:"+u1.getAddress());
        System.out.println("姓名:"+u1.getName()+" 性别:"+u1.getSex()+" 年龄:"+u1.getAge()+" 地址:"+u1.getAddress());
    }
}
public class Test2 {
    public static void main(String[] args) {
        User u1 = new User();
        u1.setName("小孙");
        System.out.println(u1.getName());

        User u2=new User("小刘",'男',23,"上海");
        System.out.println(u2.getName());
    }
}
public class User {

    //公有方法
    public void setName(String name){
        //参数值,赋给属性
        this.name=name;
        //name=name1;
    }
    public String getName(){
        //return this.name;
        return name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        if(sex=='男'||sex=='女'){
            this.sex = sex;
        }else{
            this.sex = '男';
        }
    }

    public int getAge() {

        return age;
    }

    public void setAge(int age) {
        if(age>0 && age<=100){
            this.age = age;
        }else{
            this.age =1;
        }
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public User() {
        System.out.println("User 无参构造");
    }
    //私有属性: 只能在当前类中访问和操作
    private String name;
    private char sex;
    private int age;
    private String address;
    public User(String name, char sex, int age, String address) {
        System.out.println("User 有参构造");
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.address = address;
    }
    public User(String name, char sex, int age) {
        System.out.println("User 有参构造");
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}

 

上一篇:16.Java 参数传值机制/package包机制/import详解/静态导入详解


下一篇:2021-10-26