封装

(补充 this关键字):

 package oop.demon01.demon03;
 ​
 /*
    封装的意义:
        1. 提高程序的安全性,保护代码
        2. 隐藏代码的实现细节
        3. 统一接口
        4. 系统的可维护性增加了
    快捷键:alt + insert    
  */
 ​
 public class Application {
    public static void main(String[] args) {
        Student s1= new Student();
 //       s1.name; private不可以直接调用(报错)----属性私有
        s1.setName("wentaotao");
        System.out.println(s1.getName());
 ​
        s1.setAge(999);//不合法的
        System.out.println(s1.getAge());
 ​
    }
 }
 --------------------------------------------------------------------------------------------------------
 package oop.demon01.demon03;
 ​
 //类
 public class Student {
 ​
    //属性私有(属性私有时用private)
    private String name;//名字     之前是public
    private int id;//学号
    private char sex;//性别
    private int age;//年龄
 ​
    //提供一些可以操作这个属性的方法!
    //提供一些public1 的 get、set方法
 ​
    //get 获得这个数据
    public String getName() {
        return this.name;
    }
 ​
    //set给这个数据设置值
    public void setName(String name) {
        this.name = name;
    }
 ​
    //alt + insert
 ​
    public int getId() {
        return id;
    }
 ​
    public void setId(int id) {
        this.id = id;
    }
 ​
    public char getSex() {
        return sex;
    }
 ​
    public void setSex(char sex) {
        this.sex = sex;
    }
 ​
    public int getAge() {
        return age;
    }
 ​
    public void setAge(int age) {
        if(age>120 || age<0){//不合法
            this.age=3;
        }else{
            this.age = age;
        }
    }
 ​
    //学习()
 ​
    //睡觉()
 }

学习内容源自视频:b站狂神说Java

最新文章

  1. 使用CocoaPods过程中 Unable to find a specification for
  2. Evolution项目(1)
  3. Javascript 优化项目代码技巧之语言基础(一)
  4. 使用HTML来生产Android界面
  5. BZOJ4525——[Usaco2016 Jan]Angry Cows
  6. jquery源码学习之queue方法
  7. ajax 清除缓存
  8. UIButton设置了UIControlStateSelected和UIControlStateHighlighted状态的图片点击会闪烁的解决方案
  9. android知乎小圆圈刷新效果
  10. poj 3648 2-SAT问题
  11. ArrayList源码解析
  12. UCOS 中的中断处理
  13. 10个带源码的充满活力的Web设计教程
  14. cURL模拟网页登陆
  15. 腾讯 AlloyCrop 1.0 发布
  16. python正则表达式手记
  17. Python一些方法的用法集锦
  18. vi 配置
  19. Unity3D-RayMarch-几何图元0
  20. python中join()函数的使用方法

热门文章

  1. 关于Kubernetes(简称K8S)的开启及基本使用,基于Docker Desktop &amp; WSL2
  2. python导入模块--案例
  3. SpringCloud:Eureka 配置心跳机制
  4. MyEclipse中,编写properties文件,输入中文显示乱码
  5. Windows 上连接蓝牙耳机
  6. 使用Hugo框架搭建博客的过程 - 页面模板
  7. 为什么0x100是256个字节、0x400是1KB、0x800是2KB、0x1000是4KB?
  8. 灵魂画手的零基础python教程1:关于Python学习的误区、python的优缺点、前景
  9. 「AGC023D」 Go Home
  10. 5.Java流程控制