栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java多态

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java多态

多态
  • 多态
    • instanceof
    • 类型之间的转换

多态
  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式
  • 一个对象的实际类型是确定的,但指向对象的引用类型有很多
  • 多态存在的条件:
    • 有继承关系
    • 子类重写父类方法
    • 父类引用指向子类对象
  • 注意:多态是方法的多态,不是属性的多态

代码示例:

父类Person

package com.ts.Demo03;

public class Person {
    public void run(){
        System.out.println("run");
    }

}

子类Student

package com.ts.Demo03;

public class Student extends Person{

}

主方法类

package com.ts;


import com.ts.Demo03.Person;
import com.ts.Demo03.Student;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        //new Student();
        
        //可以指向的引用类型就不确定了:父类的引用指向子类
        Person s1 = new Student();
        Student s2 = new Student();
        Object s3 = new Student();
        s1.run();
        s2.run();
    }
}


运行结果:

2.当在上面的子类Student中进行run()的重写,会得到以下结果:

package com.ts.Demo03;

public class Student extends Person{
    public void run(){
        System.out.println("son");
    }
}

注意:Student能调用的都是自己的或者继承父类的

​ Person是父类,可以指向子类,但不能调用子类独有的方法

多态的注意事项:

  1. 多态是方法的多态
  2. 父类和子类有联系(类型转换异常:ClassCastException)
  3. 存在的条件:
    • 继承关系
    • 方法需要重写
    • 父类引用指向子类对象(father f1 = new Son())
  4. 哪些方法不能被重写:
  • static方法,属于类,不属于实例
  • final 常量
  • private方法
instanceof

判断两个类之间是否存在父子关系

package com.ts;


import com.ts.Demo03.Person;
import com.ts.Demo03.Student;
import com.ts.Demo03.Teacher;

public class Application {
    public static void main(String[] args) {

        //Object>String
        //Object>Person>Student
        //Object>Person>Teacher
        Object obj = new Student();
        System.out.println(obj instanceof Student);//true
        System.out.println(obj instanceof Object);//true
        System.out.println(obj instanceof Person);//true
        System.out.println(obj instanceof Teacher);//false
        System.out.println(obj instanceof String);//false

        System.out.println("==============================");
        Person person = new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);//编译报错,Person类与String类无关。

        System.out.println("================================");
        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Object);//true
        System.out.println(student instanceof Person);//true
        //System.out.println(student instanceof Teacher);//编译报错
        //System.out.println(student instanceof String);//编译报错


        //总结:先判断两个类之间是否有关系,如果无关,编译报错,若有关,进行new出的对象所属的类进行比较,如果是父子类为true,否则为false。
        //eg:Person person = new Student();
        //System.out.println(person instanceof Teacher);//Person类与Teacher是父子类,有关系,然而person对象所属Student类与Teacher无关(不是父子类),所以输出false。
        //System.out.println(person instanceof String);//Person类与String类毫无关系,所以无法编译
    }
}


总结:先判断两个类之间是否有关系,如果无关,编译报错,若有关,进行new出的对象所属的类进行比较,如果是父子类为true,否则为false。

eg:Person person = new Student();
System.out.println(person instanceof Teacher);

Person类与Teacher是父子类,有关系,然而person对象所属Student类与Teacher无关(不是父子类),所以输出false。

System.out.println(person instanceof String);

Person类与String类毫无关系,所以无法编译

类型之间的转换

强制转换:高------->低,父--------->子

Person person = new Student();
Student student = (Student) person;

这样父类就可以用子类的方法了。

注意:

  1. 父类引用指向子类对象
  2. 子类转化为父类,向上转型,不需要强制转换,可能丢失一些方法
  3. 父类转化为子类,向下转型,需要强制转换
  4. 方便方法调用,减少重复代码,简洁
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号