封装的实现
封装的概念
- 将类的某些信息隐藏在类内部,不允许外部程序直接访问
- 通过该类提供的方法来实现对隐藏信息的操作和访问
- 隐藏对象的信息
- 留出访问的接口
封装的特点
- 只能通过规定的方法访问数据
- 隐藏类的实现细节,方便修改和实现
封装的实现
cat.java
#Alt + Shift + S => Generate Getters and Setters:生成类的set 和 get方法
package com.demo;
/**
* @author 宠物猫类
*
*/
public class Cat {
private String name;
private int age;
private double weight;
private String species;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
}
catTest.java
package com.demo;
public class CatTest {
public static void main(String[] args) {
// 对象实例化
Cat cat1 = new Cat();
cat1.setName("kitty");
cat1.setAge(2);
cat1.setWeight(2.5);
cat1.setSpecies("中华田园猫");
System.out.println(cat1.getName());
System.out.println(cat1.getAge());
System.out.println(cat1.getWeight());
System.out.println(cat1.getSpecies());
}
}
使用包进行类管理
作用:管理Java文件,解决同名文件冲突问题
创建包
- 一个包里不能存在同名类
- 包的命名规则:域名倒序 + 模块 + 功能 #域名全部小写
- 定义包的语句,必须放在Java源文件中的第一行
- 建议每个包内存储信息功能单一,即单一职责原则
导入包
- 建议采用”import 包名.类名”的方式加载,提高效率
- 加载类的顺序跟import导入语句的位置无关
- “ import 包名.* “ 只能访问指定包名下的类,无法访问子包下的类
导入所有的类
package com.imooc; // 定义包
import com.demo.*; // 导入com.demo下所有的类
public class HelloImooc {
public static void main(String[] args) {
Cat cat1 = new Cat();
CatTest cat2 = new CatTest();
}
}
导入指定的类
package com.imooc; // 定义包
import com.demo.Cat; // 导入com.demo下指定的类
import com.demo.CatTest; // 导入com.demo下指定的类
public class HelloImooc {
public static void main(String[] args) {
Cat cat1 = new Cat();
CatTest cat2 = new CatTest();
}
}
导入不同包中同名的类
package com.imooc; // 定义包
import com.demo.*; // 导入com.demo下所有的类
import com.dev.*; // 导入com.dev下所的类
public class HelloImooc {
public static void main(String[] args) {
com.demo.Cat cat1 = new com.demo.Cat();
com.dev.Cat cat2 = new com.dev.Cat();
}
}
static关键字
静态属性和静态方法
- 类对象共享
- 类加载时产生,销毁时释放,生命周期长,即所有的类对象销毁时释放
- static + 属性,称之为类属性 / 静态属性
- static + 方法,称之为类方法 / 静态方法
- 推荐调用静态属性/方法的方式是,类名.静态成员
- 静态方法中不能直接访问非静态成员,只能直接调用静态成员
静态代码块和构造代码块
- 静态代码块:类加载时调用,优于构造代码块执行
- 构造代码块:创建对象时调用,优于构造方法执行
- 在类中可以有多个静态代码块和构造代码块
- 无论产生多少类实例,静态代码块只执行一次
Cat.java
package com.demo;
/**
* @author 宠物猫类
*
*/
public class Cat {
private String name;
private int age;
private double weight;
private String species;
public Cat(){
System.out.println("demo - cat");
}
static{
System.out.println("我是静态代码块1");
}
static{
System.out.println("我是静态代码块2");
}
{
System.out.println("我是构造代码块1");
}
{
System.out.println("我是构造代码块2");
}
public String getName() {
return name;
}
...省略...
}
CatTest.java
package com.demo;
public class CatTest {
public CatTest(){
System.out.println("demo - CatTest");
}
public static void main(String[] args) {
// 对象实例化
Cat cat1 = new Cat();
Cat cat2 = new Cat();
}
}
输出结果:
我是静态代码块1
我是静态代码块2
我是构造代码块1
我是构造代码块2
demo - cat
我是构造代码块1
我是构造代码块2
demo - cat
实例:
model - Subject.java
package com.imooc.model;
/**
* 专业类
*
* @author 57681
*
*/
public class Subject {
// 成员属性:专业名称,专业编号,学制年限
private String subjectName;
private String subjectNo;
private int subjectLife;
private Student[] myStudents;
private int studentNum;
// 无参构造方法
public Subject() {
}
// 带参构造方法,实现对属性专业名称,专业编号,学制年限的赋值
public Subject(String subjectName, String subjectNo, int subjectLife) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
}
// 带参构造方法,实现对属性的全部赋值
public Subject(String subjectName, String subjectNo, int subjectLife, Student[] myStudents) {
this.setSubjectName(subjectName);
this.setSubjectNo(subjectNo);
this.setSubjectLife(subjectLife);
this.setMyStudents(myStudents);
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getSubjectNo() {
return subjectNo;
}
public void setSubjectNo(String subjectNo) {
this.subjectNo = subjectNo;
}
public int getSubjectLife() {
return subjectLife;
}
// 设置学制年限,限制必须大于0
public void setSubjectLife(int subjectLife) {
if (subjectLife > 0) {
this.subjectLife = subjectLife;
}
}
/**
* 获取选修专业的学生信息,如果保存学生信息的数组未被初始化,则初始化长度为200
*
* @return
*/
public Student[] getMyStudents() {
if (myStudents == null) {
myStudents = new Student[200];
}
return myStudents;
}
public void setMyStudents(Student[] myStudents) {
this.myStudents = myStudents;
}
public int getStudentNum() {
return studentNum;
}
public String info() {
return "专业名称:" + this.getSubjectName() + "\n专业编号:" + this.getSubjectNo() + "\n学制年限:" + this.getSubjectLife();
}
public void addStudent(Student stu) {
// 1.将学生保存到数组
// 2.将学生个数保存到studengNum
for (int i = 0; i < this.getMyStudents().length; i++) {
if (this.getMyStudents()[i] == null) {
stu.setStudentSubject(this);
this.getMyStudents()[i] = stu;
this.studentNum = i + 1;
return;
}
}
}
}
model - Student.java
package com.imooc.model;
/**
* 学生类
*
* @author 57681
*
*/
public class Student {
// 成员属性:学号,姓名,性别,年龄
private String studentNo;
private String studentName;
private String studentSex;
private int studentAge;
private Subject studentSubject;
// 无参构造方法
public Student() {
}
// 带参构造方法,实现对属性学号,姓名,性别,年龄的赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
}
// 带参构造方法,实现对属性的全部赋值
public Student(String studentNo, String studentName, String studentSex, int studentAge, Subject studentSubject) {
this.setStudentNo(studentNo);
this.setStudentName(studentName);
this.setStudentSex(studentSex);
this.setStudentAge(studentAge);
this.setStudentSubject(studentSubject);
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
if (studentSex == "男" || studentSex == "女") {
this.studentSex = studentSex;
} else {
this.studentSex = "男";
}
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
if (studentAge < 10 || studentAge > 100) {
this.studentAge = 18;
} else {
this.studentAge = studentAge;
}
}
/**
* 获取专业对象,如果没有实例化,先实例化后再返回
*
* @return 专业对象信息
*/
public Subject getStudentSubject() {
if (studentSubject == null) {
studentSubject = new Subject();
}
return studentSubject;
}
public void setStudentSubject(Subject studentSubject) {
this.studentSubject = studentSubject;
}
/**
* 学生介绍的方法
*/
public String info() {
return "学号:" + this.getStudentNo() + "\n姓名:" + this.getStudentName() + "\n性别:" + this.getStudentSex() + "\n年龄:"
+ this.getStudentAge() + "\n专业名称:" + this.getStudentSubject().getSubjectName() + "\n专业编号:"
+ this.getStudentSubject().getSubjectNo() + "\n学制年限:" + this.getStudentSubject().getSubjectLife();
}
}
test - SchoolTest.java
package com.imooc.test;
import com.imooc.model.*;
public class SchoolTest {
public static void main(String[] args) {
Subject sub1 = new Subject("计算机科学与技术", "J0001", 4);
Student stu1 = new Student("S01", "张三", "男", 20);
Student stu2 = new Student("S02", "李四", "女", 19);
Student stu3 = new Student("S03", "王五", "男", 21);
sub1.addStudent(stu1);
sub1.addStudent(stu2);
sub1.addStudent(stu3);
System.out.println(sub1.getStudentNum());
}
}