最近跑回学校两趟,然后外婆外公移山,时间都比较零碎。同时还要解决毕业论文答辩,相关材料的准备;
但同时也有小期待小盼头啦,毕业照,又是爷青结。加油!
1 方法重写
- 静态方法和非静态方法区别很大!
- 重写都是方法的重写,和属性无关
静态方法static:方法的调用只和左边,即定义的数据类型有关
public class B {
public static void test(){
System.out.println("B>=test()");
}
}
public class A extends B{
public static void test(){
System.out.println("A>=test()");
}
}
public class Application {
public static void main(String[] args) {
A a = new A();
a.test();//A>=test()
B b = new A();//父类的引用指向了子类
b.test();//B>=test()
}
}
重写只跟非静态方法有关
快捷键 alt + insert
得到:
注意点:父类的static的方法在下面的这个窗口不显示
//Override 重写
@Override //注解:有功能的注释!
public void test() {
super.test();
}
非静态:重写
子类重写了父类的方法,执行子类的方法
public class B {
public void test(){
System.out.println("B>=test()");
}
}
public class A extends B{
//Override 重写
@Override //注解:有功能的注释!
public void test() {
System.out.println("A>=test()");
}
}
public class Application {
public static void main(String[] args) {
A a = new A();
a.test();
B b = new A();//结果A>=test() , 子类重写了父类的方法,执行子类的方法
b.test();
}
}
重写小结:
-
需要有继承关系,子类重写父类的方法!
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大但不能缩小: public> protected>default>private
- 抛出的异常:范围可以被缩小,但不能扩大;ClassNotFoundException --> Exception(范围更大,不可以) ;
-
重写的子类的方法和父类必须一致,方法体不同
-
为什么需要重写:
父类的功能,子类不一定需要,或者不一定满足!所以在子类中才需要重新写。 -
重写快捷键 Alt + Insert : override
2 多态
-
动态编译:类型,可扩展性变强
-
即同一方法可以根据发送对象的不同而采用多种不同的行为方式
-
一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多(父类,有关系的类)
-
多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
-
注意:多态是方法的多态,属性没有多态
一个对象的实际类型是确定的,但是可以指向这个对象的引用类型可以是任意父类的。
比如:new一个子类Student,这时会默认new了一个父类Person,所以可以引用父类Person
所以有 父类的引用指向子类:Person p = new Student();
对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
Student 能调用的方法都是自己的或者继承父类的
Person 父类型,可以指向子类,但是不能调用子类独有的方法;子类重写了父类的方法,执行子类的方法
public class Person {
public void run(){
System.out.println("father");
}
}
public class Student extends Person{
@Override
public void run() {
System.out.println("son");
}
public void eat(){
System.out.println("eat");
}
}
public class Application {
public static void main(String[] args) {
Student s1 = new Student();
Person s2 = new Student();
Object s3 = new Student();
s1.run();//son
s2.run();//son
s1.eat();//eat
((Student)s2).eat();//eat ,强制转换
}
}
多态注意事项:
- 多态是方法的多态,属性没有多态
- 父类和子类,有联系;不然类型转换异常!:ClassCastException!
- 存在的条件:继承关系,方法需要重写,父类引用指向子类对象 Father f1 = new Son();
以下情况不能重写,也就不能实现多态:
①static方法,属于类。不属于实例
②final常量,在常量池
③priva方法
instanceof
System.out.println(X instanceof Y);
能不能编译通过,取决于X的引用类型与Y之间是否存在父子关系,否则编译报错
结果true还是false,X所指向的实际类型是否Y的子类型。
//Object>Person>Teacher
//Object>Person>Student
//Object>String
//System.out.println(X instanceof Y);
Object object = new Student();
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Teacher);//false object所指向的实际类型为student类,不是Teacher的子类型
System.out.println(object instanceof String);//false object所指向的实际类型为student类,不是Teacher的子类型
Person person = new Student();
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Teacher);//false object所指向的实际类型为student类,不是Teacher的子类型
//System.out.println(person instanceof String); //报错,不存在父子关系
Student student = new Student();
System.out.println(student instanceof Object);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Student);//true
//System.out.println(student instanceof Teacher); //报错,不存在父子关系
//System.out.println(student instanceof String); //报错,不存在父子关系
类型转换
类型之间的转换:父类----->子类
//子类转换为父类,可能丢失自己本来的一些方法!
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
public class Application {
public static void main(String[] args) {
Person person = new Student();//student一开始从低的Student类转化成高的Person类
/*
Student student = new Student();
Person person = student;
*/
//person.go;报错
//将这个对象转换为Student类,就可以使用Student类的方法了
((Student)person).go(); //go
}
}
小结:
- 父类引用指向子类的对象
- 把子类转换为父类,向上转型
- 把父类转换成子类,向下转型;强制转换
- 方便方法调用,减少重复的代码,简洁
核心思想:抽象
还有抽象类,接口