Java学习Day20
封装
程序设计要求:高内聚,低耦合
高内聚:类的内部数据操作细节自己组织,不允许外部干涉;
低耦合:仅暴露少量的方法给外部使用;
封装要求:属性私有,set/get
封装的意义:
- 提高程序的安全性 ,保护数据
- 隐藏代码的实现细节
- 统一接口
- 增加系统的可维护性
public class Student {
private String name; //名字
private int id; // 学号
private char sex; // 性别
private int age; //年龄
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;//输入不合法,年龄设置为3
else this.age = age;//输入合法,年龄正常
}
}
public static void main(String[] args) {
Student student = new Student();
student.setName("Jack");
System.out.println(student.getName());
student.setAge(999);//不合法
System.out.println(student.getAge());//3
student.setAge(21);//合法
System.out.println(student.getAge());//21
}
继承
继承的本质是对某一批类的抽象 ,从而实现对现实世界更好的建模。
关键词:extends
extends的意思是扩展,子类是对父类的扩展。
修饰符:public>protected>default(不写)>private
子类继承了父类,就会拥有父类的所有公有方法(私有方法和属性无法被继承)!
在类中按Ctrl+H查看继承情况
Java中,所有的类都直接或间接继承Object类;java只能单继承,不能多继承。
//父类
public class Person{
private int money = 100_0000;//私有属性
public void say(){
System.out.println("说了一句话");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
//子类Student继承父类Person
public class Student extends Person{
}
//测试类
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
// student.money; 父类私有属性无法直接获取
student.setMoney(100);
System.out.println(student.getMoney());
}
}
super注意点:
- super调用父类的构造方法,必须在构造方法的第一行
- super必须只能出现在子类的方法或者构造方法中
- super和this不能在构造方法中同时调用
super和this的区别:
- 代表的对象不同
- this:本身调用者这个对象
- super:代表父类对象的调用
- 使用前提
- this:没有继承也可以使用
- super:只能在继承条件下才可以使用
- 构造方法
- this():本类的构造
- super():父类的构造
//父类
public class Person{
protected String name = "Zeta";
public Person() {
System.out.println("Person无参构造");
}
public void print(){
System.out.println("Person");
}
}
//子类Student继承父类Person
public class Student extends Person{
private String name = "Jack";
public Student() {
super();//隐式调用,如果父类没有无参构造,则会报错
System.out.println("Student无参构造");
}
public void print(){
System.out.println("Student");
}
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
public void test1(){
print();
this.print();
super.print();
}
}
//测试类
public class Application {
public static void main(String[] args) {
Student student = new Student();
// student.test("zj"); 输出:zj,Jack,Zeta
// student.test1(); 输出:Student,Student,Person
}
}
方法重写
重写:需要有继承关系为前提,子类重写父类的方法!
-
方法名必须相同
-
参数列表必须相同
-
修饰符范围可以扩大但不能缩小 例:父类的protected可以在子类中变成public
-
抛出异常的范围可以被缩小,但不能扩大 例:ClassNotFoundException扩大成Exeption
-
重写的方法名必须相同,内容不同。
-
为什么需要重写:父类的功能,子类不一定需要,或者不一定满足!
快捷键:Alt+Insert - -> 选择override
这两个图标表示重写与被重写
静态方法不可以被重写,非静态方法可以被重写
//父类
public class B {
public static void test(){//未被重写
System.out.println("B.test");
}
public void print(){//被重写
System.out.println("B.print");
}
}
//子类,重写是方法的重写,与属性无关!
public class A extends B{
public static void test(){//不是重写
System.out.println("A.test");
}
public void print(){//重写
System.out.println("A.print");
}
}
//测试类
public class Application {
public static void main(String[] args) {
A a = new A();
a.test();
a.print();
B b = new A();
b.test();//静态方法,调用B类的静态方法
b.print();//非静态方法,调用A实例化对象的方法
}
}