当前位置:网站首页>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 | 可以 | 可以 | 可以 | 可以 |
边栏推荐
猜你喜欢
随机推荐
LeetCode 622 设计循环队列[数组 队列] HERODING的LeetCode之路
牛客题目——滑动窗口的最大值、矩阵最长递增路径、顺时针旋转矩阵、接雨水问题
「 每日一练,快乐水题 」1374. 生成每种字符都是奇数个的字符串
奥特学园ROS笔记--7(289-325节)
4 kmiles join YiSheng group, with more strong ability of digital business, accelerate China's cross-border electricity full domain full growth
2170. 使数组变成交替数组的最少操作数
10 种最佳 IDE 软件 ,你更忠爱哪一个?
网上那么多教人赚钱的方法,但是你实际上是靠什么赚钱的呢?
有效解决MySQL报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES)
ALV report learning summary
你所不知道的C#中的细节
Day35 LeetCode
你是几星测试/开发程序员?技术型选手王大拿......
【 LeetCode 】 1374. Generate each character string is an odd number
golang源码分析之geoip2-golang
OP-5,输入/输出信号范围-一信号处理能力
Digital twins help visualize the construction of smart cities
第七章 噪声
Soft Exam ----- UML Design and Analysis (Part 2)
Implement fashion_minst clothing image classification









![[AnXun cup 2019] easy_web](/img/26/c04bc8b9c65ac75ddd2696b48e1661.png)