java的继承封装多态_19java的封装继承多态(下)

本文深入探讨了Java中的方法重写,强调了没有继承关系不能称为方法重写,并通过实例展示了静态与非静态方法在重写中的区别。文章还解释了方法重写的基本规则,包括重写条件、方法名和参数列表的匹配以及访问权限的要求。接着,文章引入了多态的概念,阐述了动态编译和引用类型的可扩展性,并通过代码实例演示了多态的应用。最后,文章讨论了`instanceof`关键字的作用,以及类之间的强制类型转换原则。
摘要由CSDN通过智能技术生成

方法重写

注意:方法的重写是针对继承来说的,没有继承关系不叫方法的重写。

下面先给一个重写的错误写法:

package oop.OopDemo;

import oop.OopDemo.Demo02_extends.Person;

import oop.OopDemo.Demo02_extends.Student;

public class Application {

public static void main(String[] args) {

Student student = new Student();

student.print();

Person person = new Student();//父类的引用(对象)指向子类

person.print();

}

}

package oop.OopDemo.Demo02_extends;

public class Student extends Person {

public Student() {

System.out.println("Student is running!");

}

public static void print() {

System.out.println("I'm Student class!");

}

}

package oop.OopDemo.Demo02_extends;

public class Person {

public Person() {//无参构造器

System.out.println("Person is running!");

}

public static void print() {

System.out.println("I'm Person class!");

}

}

运行结果:

1238dc30749b5ff1296f37c9ab0c688e.png

观察以上的运行结果,首先是运行了Student类的无参构造器,因为首先new了一个Student类的对象,这是正常的,然后调用子类的方法print(),然后又使用父类Person实例化一个子类Student的对象person,运行的也是Student类的无参构造器,这个也能理解,new的谁就调用谁的无参构造器。使用person这个对象调用print()方法,发现调用的是父类的print()方法,记住这个现象,等下作对比。

现在我们把子类和父类的print()方法的修饰符static都去掉,发现两个print()方法前面都出现了这个图标,而且点击还能相互跳转,此时才是真正的方法的重写,如下图:

7507fd61040047f7bf7cf0073e05fb64.png

运行结果:

9b9085b75b91e7a196b25714c9fb2110.png

分析以上结果发现:对于继承关系下的方法的重写,静态方法和非静态方法的区别很大!(不要随便用Static修饰符),非静态方法才是方法的重写。重写之后,不管是使用Student还是使用Person对Student进行初始化,使用初始化对象调用重写的方法,调用的都是子类的方法。子类重写父类的方法,就执行子类的方法,子类如果没有重写父类的方法,就调用父类的方法。

注意:经测试,重写的方法名(修饰符)必须相同,并不能扩大(留作以后再考究)

总结:重写必须有继承关系,子类重写父类的方法!(父类的引用(对象)指向子类)

方法名必须相同

参数列表必须相同

修饰符:范围可以扩大但是不能缩小。范围大小排序:public > protected > default > private

抛出的异常:范围可以缩小但是不能扩大。Exception > ClassNotFoundException

重写只有方法的重写,没有属性的重写

为什么要重写方法?

父类的方法,子类不一定需要,或者不一定满足。

重写方法的快捷键:Alt+Insert 选择Override Methods...(只能把光标放在子类中才能正确生成重写方法)

多态

动态编译,引用类型(对象)的可扩展性,继承关系下,方法可能重写也可能不重写,如果父类和子类拥有相同的方法(被子类重写),不管是直接使用子类进行实例化,还是使用父类的引用类型指向子类进行实例化,其对象调用被重写的方法都是执行子类的方法,如果是父类特有或子类特有,那么分别执行自己的方法,程序在运行之前(程序)不知道执行的过程,但是作为开发者肯定要知道运行过程。

7a3aa7693a379b9f0c61e0b5a1dc0fae.png

关于这里的多态,我觉得理解继承和方法的重写,多态就差不多理解了 ,下面还是用程序说明一下吧:

package oop.OopDemo;

import oop.OopDemo.Polymorphic.Person;

import oop.OopDemo.Polymorphic.Student;

import oop.OopDemo.Polymorphic.Teacher;

public class Application {

public static void main(String[] args) {

Person person = new Person();

animalShout(person);

Person person1 = new Student();

animalShout(person1);

Student student = new Student();

animalShout(student);

Teacher teacher = new Teacher();

animalShout(teacher);

}

public static void animalShout(Person person) {

person.speech();

}

}

package oop.OopDemo.Polymorphic;

public class Person {

public String name;

public Person() {//无参构造器

}

public void speech() {

System.out.println("Person is speeching!");

}

}

package oop.OopDemo.Polymorphic;

public class Student extends Person {

public Student() {

}

//多态

public void speech() {

System.out.println("Student is speeching!");

}

}

package oop.OopDemo.Polymorphic;

public class Teacher extends Person{

//多态

public void speech() {

System.out.println("Teacher is speeching!");

}

}

输出结果:

dc2cf45d1b911daa5f22d142cab278d6.png

Person person = new Student();

这句话是父类的引用类型指向子类,这个对象person还是属于Person类,对象能执行的方法主要看左边的类型,跟右边的类型关系不大(不是没有关系)。

我们看到子类重写了父类的方法,然后使用对象student和person调用被重写的方法run(),执行的是子类的run()方法;子类可以执行父类的方法,但是父类不能执行子类的方法。

多态的注意事项:

多态是方法的多态,没有属性的多态

父类和子类的强制类型转换必须合法,eg:不能把Student转成String。类型转换异常:ClassCastException!

存在条件:有继承关系,方法需要重写,父类引用指向子类对象 Father person = new Son();

不能被重写的方法:

static方法,属于类,不属于实例,不能被重写;

被final 修饰的方法不能被重写,在常量池里面,不能被更改;

被private修饰的方法是私有的,不能被重写。

instanceof关键字

判断两个类之间有没有父子关系

直接用代码解释:

package oop.OopDemo;

import oop.OopDemo.Polymorphic.Person;

import oop.OopDemo.Polymorphic.Student;

import oop.OopDemo.Polymorphic.Teacher;

public class Application {

public static void main(String[] args) {

// Object > Person > Student

// Object > Person > Teacher

// Object > String

Object object = new Student();

System.out.println(object instanceof Student);//true只要有长辈关系都算true

System.out.println(object instanceof Person);//true

System.out.println(object instanceof Object);//true

System.out.println(object instanceof Teacher);//false Teacher类和Student类属于兄弟关系,false

System.out.println(object instanceof String);//false 关系更远,false

System.out.println("==================");

Person person = new Student();

System.out.println(person instanceof Student);//true只要有长辈关系都算true

System.out.println(person instanceof Person);//true

System.out.println(person instanceof Object);//true

System.out.println(person instanceof Teacher);//false

// System.out.println(person instanceof String);//编译错误

System.out.println("==================");

Person person1 = new Person();

System.out.println(person1 instanceof Student);//false

System.out.println(person1 instanceof Teacher);//false

System.out.println(person1 instanceof Person);//true

System.out.println(person1 instanceof Object);//true

System.out.println("==================");

Student student = new Student();

System.out.println(student instanceof Student);//true只要有长辈关系都算true

System.out.println(student instanceof Person);//true

System.out.println(student instanceof Object);//true

// System.out.println(student instanceof Teacher);//编译错误

// System.out.println(student instanceof String);//编译错误

}

}

分析:

第一部分Object object = new Student();,使用父类Object的引用指向子类Student,也就是说object是指向Student类的,关键字instanceof是判断有没有长辈关系(包括父子和祖孙),如:

object instanceof Student: object 是指向Student类的,自己跟自己肯定是true

object instanceof Person: object 是指向Student类的,Student类是Person类的子类,true

object instanceof Object: object 是指向Student类的,Student类是Object类的子类,true

object instanceof Teacher: object 是指向Student类的,Student类与Teacher类没有长辈(父子)关系,是兄弟关系,false

object instanceof String: object 是指向Student类的,与String类没有关系,false

第二部分Person person1 = new Person();,实例化一个Person对象,

person1 instanceof Student:person1 指向Person类,Person类是Student类的父类,false

person1 instanceof Student:person1 指向Person类,Person类是Teacher类的父类,false

person1 instanceof Person: person1 指向Person类,自己跟自己肯定是true

person1 instanceof Object:person1 指向Person类,Person类是Object类的父类,true

至此,应该理清思路了

在进行实例化的时候,不管是直接实例化一个类的对象,还是使用父类的引用类型指向子类进行实例化,都要抓住一个关键点:对象是指向哪个类,就用哪个类参与instanceof的判断

使用关键字instanceof进行判断时,格式上必须要求对象在作,类在右,否则会报错

对于上面出现的一些编译错误,我还不清楚是什么原因。

强制类型转换(类)

类的强制类型转换跟基本数据类型的强制类型转换的原则是相同的,都是高转低,需强制;低转高,可直接。

下面直接看程序实例:

package oop.OopDemo;

import oop.OopDemo.Polymorphic.Person;

import oop.OopDemo.Polymorphic.Student;

public class Application {

public static void main(String[] args) {

//父类与子类之间的类型转换 父转子

Person student = new Student();//student的类型是Person类型,student是指向Student类的

((Student) student).run();//父转子:高转低,需强制类型转换(老鼠进洞)

Student student1=(Student)student;

student1.run();

//子转父

Student student2 = new Student();

Person person=student2;//子转父:低转高,可直接转换,不用强制类型

person.sleep();

// person.run();//丢失了父类本来的方法,不能使用父类run()

((Person)student2).sleep();

}

}

package oop.OopDemo.Polymorphic;

public class Person {

public void sleep() {

System.out.println("Person is sleeping!");

}

}

package oop.OopDemo.Polymorphic;

public class Student extends Person {

public void run() {

System.out.println("Student is running!");

}

}

运行结果:

92afcf8de0ba6d6753cd8ce7006f1ca1.png

总结:

需要注意的是:语句Person student = new Student(); 对象student的类型是Person类型,student是指向Student类的。对象student的类型是父类Person,现在想调用子类的run()方法,注意这是高优先级(父类)转低优先级(子类),需要强制类型转换,代码中给了两种方式均是可行的;

语句Student student2 = new Student();实例化了一个类型为Student 的对象student2 ,但是此时我想用Student类(子类) 的对象student2调用父类Person的sleep()方法,就需要把对象student2的类型从Student类(子类)转换成Person类(父类),就是低优先级(子类)转高优先级(父类),可以直接转换。

关于什么时候需要强制转换,什么时候可以在直接转换的记忆方法,我觉得可以这样记忆:使用老鼠进洞来记忆。高优先级是大老鼠,低优先级是小老鼠,大老鼠要进小老鼠的洞进不去,那就强制塞进去;而小老鼠进大老鼠的洞可以直接进去。

类的类型转换的意义:方便方法的调用,减少重复的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值