面向对象编程三大特征(封装、继承和多态)

1基本介绍

面向对象编程有三大特征:封装、继承和多态。

2 封装介绍

1663424485765

3 封装的理解和好处

1663424610139

4 封装的实现步骤 (三步)

1663424901200

5 案例

不能随便查看人的年龄,工资等隐私,并对设置的年龄进行合理的验证。年龄合理就设置,否则给默认;年龄, 必须在 1-120, 年龄, 工资不能直接查看 , name的长度在 2-6字符 之间。

public class Encapsulation01 {

    public static void main(String[] args) {
        //如果要使用快捷键alt+r, 需要先配置主类
        //第一次,我们使用鼠标点击形式运算程序,后面就可以用
        Person person = new Person();
        person.setName("小明");
        person.setAge(30);
        person.setSalary(30000);
        System.out.println(person.info());
        System.out.println(person.getSalary());

        //如果我们自己使用构造器指定属性
        Person smith = new Person("smith", 80, 50000);
        System.out.println("====smith的信息======");
        System.out.println(smith.info());


    }
}

class Person {
    public  String name; //名字公开
    private int age; //age 私有化
    private double salary; //..

    public void say(int n,String name) {

    }
    //构造器 alt+insert
    public Person() {
    }
    //有三个属性的构造器
    public Person(String name, int age, double salary) {
//        this.name = name;
//        this.age = age;
//        this.salary = salary;
        //我们可以将set方法写在构造器中,这样仍然可以验证
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    //自己写setXxx 和 getXxx 太慢,我们使用快捷键
    //然后根据要求来完善我们的代码.
    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        //判断
        if(age >= 1 && age <= 120) {//如果是合理范围
            this.age = age;
        } else {
            System.out.println("你设置年龄不对,需要在 (1-120), 给默认年龄18 ");
            this.age = 18;//给一个默认年龄
        }
    }

    public double getSalary() {
        //可以这里增加对当前对象的权限判断
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    //写一个方法,返回属性信息
    public String info() {
        return "信息为 name=" + name  + " age=" + age + " 薪水=" + salary;
    }
}

6 例题

1663426605709

package com.yt.modifier;

public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account("李四",1000,"1234567");
//        account.setName("张三");
//        account.setBalance(120);
//        account.setPassword(1234567);

//        System.out.println("name=" + account.getName());
//        System.out.println("balance=" + account.getBalance());
//        System.out.println("password=" + account.getPassword());
        account.showInfo();
    }

}

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("输入的名字有误,给默认值无名氏");
            this.name = "无名氏";
        }
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        if (balance > 20) {
            this.balance = balance;
        } else {
            System.out.println("输入的余额不对,至少要大于20");
            this.balance = 20.0;
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if (password.length() > 6) {
            this.password = password;
        } else {
            System.out.println("密码位数需要大于六位数");
            this.password = "000000";
        }
    }

    //显示账号信息
    public void showInfo(){
        System.out.println("账号信息 name=" + name + " 余额=" + balance +
                    " 密码=" + password);
    }
}
posted @ 2022-09-19 20:02  半路_出家ren  阅读(83)  评论(0编辑  收藏  举报
返回顶端