当前位置:网站首页>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 | 可以 | 可以 | 可以 | 可以 |
边栏推荐
- STP生成树协议
- 笑话:如果你在河边等待得足够久,你会看到你的敌人的尸体漂过,是怎么翻译出来的?
- Parse common methods in the Collection interface that are overridden by subclasses
- 云平台简介
- Shell: conditional statements
- golang源码分析之geoip2-golang
- ImageNet下载及处理
- J9 digital theory: the Internet across chain bridge has what effect?
- Which thread pool does Async use?
- 浅议.NET遗留应用改造
猜你喜欢
随机推荐
php 单引号 双引号 -> => return echo
.NET如何快速比较两个byte数组是否相等
STP生成树协议
TodoList案例
Common tools and test methods for interface testing (Introduction)
基于 outline 实现头像剪裁以及预览
信息学奥赛一本通(1256:献给阿尔吉侬的花束)
传感器工作原理
postgresql autovaccum自动清理
golang 源码分析:uber-go/ratelimit
信息系统项目管理师必背核心考点(五十八)变更管理的主要角色
Leetcode刷题——字符串相加相关题目(415. 字符串相加、面试题 02.05. 链表求和、2. 两数相加)
李沐动手学深度学习V2-bert和代码实现
Triacetin是什么化学材料
.NET性能优化-你应该为集合类型设置初始大小
OP analysis and design
SQL Server数据类型转换函数cast()和convert()详解
J9 Digital Currency Theory: Identifying Web3's New Scarcity: Open Source Developers
【手撕AHB-APB Bridge】~ AMBA总线 之 APB
Lvm逻辑卷