Java面向对象07——封装
2024-09-06 16:58:13
封装
(补充 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
最新文章
- 使用CocoaPods过程中 Unable to find a specification for
- Evolution项目(1)
- Javascript 优化项目代码技巧之语言基础(一)
- 使用HTML来生产Android界面
- BZOJ4525——[Usaco2016 Jan]Angry Cows
- jquery源码学习之queue方法
- ajax 清除缓存
- UIButton设置了UIControlStateSelected和UIControlStateHighlighted状态的图片点击会闪烁的解决方案
- android知乎小圆圈刷新效果
- poj 3648 2-SAT问题
- ArrayList源码解析
- UCOS 中的中断处理
- 10个带源码的充满活力的Web设计教程
- cURL模拟网页登陆
- 腾讯 AlloyCrop 1.0 发布
- python正则表达式手记
- Python一些方法的用法集锦
- vi 配置
- Unity3D-RayMarch-几何图元0
- python中join()函数的使用方法
热门文章
- 关于Kubernetes(简称K8S)的开启及基本使用,基于Docker Desktop &; WSL2
- python导入模块--案例
- SpringCloud:Eureka 配置心跳机制
- MyEclipse中,编写properties文件,输入中文显示乱码
- Windows 上连接蓝牙耳机
- 使用Hugo框架搭建博客的过程 - 页面模板
- 为什么0x100是256个字节、0x400是1KB、0x800是2KB、0x1000是4KB?
- 灵魂画手的零基础python教程1:关于Python学习的误区、python的优缺点、前景
- 「AGC023D」 Go Home
- 5.Java流程控制