Java学习第七天
- 继承
继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模!
extends的意思是“拓展”。子类是父类的拓展。
Java中类只有单继承,没有多继承!
继承是类和类之间的一种关系,除此之外,类和类之间的关系还有依赖、组合、聚合等。
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
子类和父类之间,从意义上讲具有“is a”的关系。
package com.lixiaochuan.oop.Demo05;
//父类
//在Java中,所有的类,都默认直接或者间接继承Object
public class Person {
private int money = 10_0000_0000;
public void say(){
System.out.println("说了一句话!");
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
}
/*
package com.lixiaochuan.oop;
import com.lixiaochuan.oop.Demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.say();
}
}
package com.lixiaochuan.oop.Demo05;
//学生 is 人:派生类 子类
//子类继承了父类,就会拥有父类的全部方法!
public class Student extends Person {
//Ctrl + H 打开继承树
}
package com.lixiaochuan.oop.Demo05;
public class Teacher extends Person{
}
*/
- super
私有的无法被继承!
对于无参构造:先父,后子!
super注意点
1. super 调用父类的构造方法,必须在构造方法的第一个
2. super 必须只能出现在子类的方法或者构造方法中!
3. super 和 this 不能同时调用构造方法!
vs this
1. 代表的对象不同
this:本身调用着这个对象
super:代表父类对象的引用
2. 前提
this:没有继承也可以使用
super:只能在继承条件下才可以使用
3. 构造方法
this():本类的构造
super():父类的构造
- 方法重写
b是A new出来的对象,因此调用了A的方法
因为静态方法是类的方法,而非静态是对象的方法
有static时,b调用了B类的方法,因为b是用B类定义的
无static时,b调用的是对象的方法,而b是用A类new的
重写:需要有继承关系,子类重写父类的方法!
1. 方法名必须相同
2. 参数列表必须相同
3. 修饰符:范围可以扩大但不能缩小:public>Protected>Default>private
4. 抛出的异常:范围,可以被缩小,但不能扩大:ClassNotFoundException – >Exception(大)
重写,子类的方法和父类必须一直:方法体不同!
为什么需要重写
1. 父类的功能,子类不一定需要,或则不一定满足!
Alt + Insert:override
- 多态
多态注意事项:
-
多态是方法的多态,属性没有多态;
-
父类和子类,有联系 类型转换异常! ClassCastException!
-
存在条件:继承关系,方法需要重写,父类引用指向子类对象! father f1 = new Son();
不能被重写的方法:
- static方法,属于类,它不属于实例
- final 常量;
- private 方法;
package com.lixiaochuan.oop;
import com.lixiaochuan.oop.Demo06.Person;
import com.lixiaochuan.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类
//Student 能调用的方法都是自己的或者继承父类的!!
Student s1 = new Student();
//Person 父亲型,可以指向子类,但是不能调用子类独有的方法!!
Person s2 = new Student();
Object s3 = new Student();
//对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!!
s2.run();//子类重写了父类的方法,执行子类的方法!
s1.run();
}
}
- instanceof 和类型转换
instanceof:
package com.lixiaochuan.oop;
import com.lixiaochuan.oop.Demo05.Teacher;
import com.lixiaochuan.oop.Demo06.Person;
import com.lixiaochuan.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//Object > String
//Object > Person > Teacher
//Object > Person > Student
Object object = new Student();
System.out.println(object instanceof Student);
System.out.println(object instanceof Person);
System.out.println(object instanceof Object);
System.out.println(object instanceof Teacher);
System.out.println(object instanceof String);
System.out.println("========================");
Person person = new Student();
System.out.println(person instanceof Student);
System.out.println(person instanceof Person);
System.out.println(person instanceof Object);
System.out.println(person instanceof Teacher);
System.out.println(person instanceof String);
}
}
类型转换:
package com.lixiaochuan.oop;
import com.lixiaochuan.oop.Demo05.Teacher;
import com.lixiaochuan.oop.Demo06.Person;
import com.lixiaochuan.oop.Demo06.Student;
public class Application {
public static void main(String[] args) {
//类型之间的转化: 父 子
//高 低
//子类转换为父类,可能丢失自己本来的一些方法!
Person obj = new Student();
//student 将这个对象转换为Student类型,我们就可以使用Student类型的方法了
obj.run();
((Student)obj).go();
}
}
/*
1. 父类引用指向子类的对象
2. 把子类转换为父类,向上转型;
3. 把父类转换为子类,向下转型;(强制转换)
4. 方便方法的调用,减少重复的代码!
*/
今天的内容要好好消化!!