当前位置:网站首页>Packages and packages, access modifiers
Packages and packages, access modifiers
2022-08-02 23:34:00 【fourteen seventeen】
什么是封装
面向对象三大特征之一 -----------封装
封装的概念
将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
把尽可能多的东西藏起来,对外提供便捷的接口
如何实现封装
1 修改属性的可见性(访问权限修饰符) 设置为private 防止错误的修改(Only in this class can be changed)
// 定义属性
private String name;
private int health;
private int love;2 创建公有的getter 和setter 方法 Belong to the class to read and write
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
3 在get / setter 方法中加入属性控制语句 To judge the legitimacy of the attribute
public int getHealth() {
return health;
}
public void setHealth(int health) {
// 调用此方法 来给 health对象赋值 Before the assignment for cominghealth进行判断
if (health < 0 || health > 100) {
System.out.println("Health value you input is wrong,默认60");
this.health = 60;
return;// The end of the code further operation
}
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
if (love < 0 || love > 100) {
System.out.println("你的输入有误 默认80");
return;
}
this.love = love;
}封装的好处
便于使用者正确使用系统,防止错误修改属性
Help to loose coupling between system,提高系统独立性
提高软件的可重用性
降低了构建大型系统的风险
包(package)
Windows The tree file system
文档分门别类,Easy to find and manage
Use a directory file conflicts with the same problem

包(package)的作用
允许类组成较小的单元(类似文件夹),Easy to find and use the corresponding file
To prevent naming conflicts to distinguish classes of the same name
有助于实施访问权限控制
如何创建包
package cn.bdqn.demo02;
public class Penguin {}作为java源代码的第一条语句
用package声明包,以分号结尾
包的命名规范
包名由小写字母组成,Not to dot the beginning or the end
package mypackage;
包名之前最好加上唯一的前缀,Often use organization Inversion of the network domain name
package net.javagroud.mypackage;
包名后续部分依不同机构内部的规范不同而不同
package net.javagroud.powerproject(部门名.项目名);
JDKProvide basic package
java.lang 虚拟机自动导入
java.util 提供一些实用类
java.io 输入、输出
如何导包
为了使用不在同一包中的类,需要在java程序中使用importKeywords to import the package
import 包名.类名;
import java.util.*; //导入java.util包中所有类
import cn.jtest.classandobject.School; //导入指定包中指定类
使用包的注意事项
A class reference at the same time from two different packages of the same class
Must pass the full name of the class to distinguish
每个包都是独立的,Top-level package does not contain child package class
package和import的顺序是固定的
package必须位于第一行(Ignore the comment lines)
只允许有一个package语句
其次是import
接着是类的声明
访问权限控制
类成员的访问修饰
作用域 修饰符 | 同一个类中 | 同一个包中 | 子类中 | 任何地方 |
private | 可以 | 不可以 | 不可以 | 不可以 |
默认修饰符 | 可以 | 可以 | 不可以 | 不可以 |
protected | 可以 | 可以 | 可以 | 不可以 |
public | 可以 | 可以 | 可以 | 可以 |
边栏推荐
- 【SLAM】DM-VIO(ros版)安装和论文解读
- .NET如何快速比较两个byte数组是否相等
- ShardingSphere-proxy +PostgreSQL implements read-write separation (static strategy)
- Silver circ: letter with material life insurance products should be by the insurance company is responsible for the management
- OP-5,输入/输出信号范围-一信号处理能力
- Golang source code analysis: time/rate
- js Fetch返回数据res.json()报错问题
- Digital twins help visualize the construction of smart cities
- 引用类型 ,值类型 ,小坑。
- Lvm逻辑卷
猜你喜欢

框架设计:PC 端单页多页框架如何设计与落地

第七章 噪声

Leetcode刷题——单调栈问题(739每日温度问题、496下一个更大元素I、503下一个更大元素 II)

接口测试常用工具及测试方法(入门篇)

Digital twins help visualize the construction of smart cities

callback prototype __proto__

美国爱荷华州立大学| Improving Distantly Supervised Relation Extraction by Natural Language Inference(通过自然语言推理改进远程监督关系提取)

Flutter with internationalized adapter automatically generated

Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
![LeetCode 622 设计循环队列[数组 队列] HERODING的LeetCode之路](/img/dc/ba5a0c3f6a58283d79c5916376e49e.png)
LeetCode 622 设计循环队列[数组 队列] HERODING的LeetCode之路
随机推荐
即时通讯开发移动端网络短连接的优化手段
对话亚洲高校首个博士论文奖-裘捷中丨KDD2022
基本语法(三)
Five data structures of Redis and their corresponding usage scenarios
iframe------------frame-
J9 digital theory: the Internet across chain bridge has what effect?
TPAMI2022 | TransCL: based on the study the compression of the Transformer, more flexible and more powerful
封装和包、访问修饰权限
你所不知道的C#中的细节
特拉维夫大学 | Efficient Long-Text Understanding with Short-Text Models(使用短文本模型进行高效的长文本理解)
磁盘分区的知识
接口测试常用工具及测试方法(入门篇)
信息学奥赛一本通(1258:【例9.2】数字金字塔)
GNN教程:图神经网络基础知识!
golang源码分析之geoip2-golang
数字孪生助力智慧城市可视化建设
callback prototype __proto__
APP自动化uiautomator2获取toast
网上那么多教人赚钱的方法,但是你实际上是靠什么赚钱的呢?
「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线