好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java实例讲解多态数组的使用

多态概述

多态概念:所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量到底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个类中实现的方法,必须在由程序运行期间才能决定。因为在程序运行时才确定具体的类,这样,不用修改源程序代码,就可以让引用变量绑定到各种不同的类实现上,从而导致该引用变量调用的具体方法随之改变,即不修改程序代码就可以改变程序运行时所绑定的具体代码,让程序可以选择多个运行状态,这就是多态性。

多态存在的三个必要条件

1.要有继承关系的存在(实现接口也是一种继承关系)

2.要有方法的重写

3.要有父类引用指向子类对象

注:

1. 运行方法看运行类型,属性看编译类型

2.在方法中调用属性,无特殊情况为本类属性

3.若类型中没有调用的方法,遵循向上查询

1.向上转型

1.对象的向上转型,其实就是多态写法:

  格式: 父类名称 对象名 = new  子类名称();

  含义:右侧创建了一个子类对象,把它当作父类来看待使用。

Person person=new student();

          创建了一个学生对象,当作人看待,没有问题。
          此时的编译类型为person,运行类型为学生;

注意事项:向上转型一定是安全的。从小范围转向了大范围,从小范围的猫,向上转换成为更大范围的动物

2.向下转型

2.对象的向下转型,其实就是一个还原的动作:

   格式: 子类名称 对象名 =(子类名称) 父类对象;

   含义:将子类还原,还原成为本来的子类对象

Cat cat =(Cat) animal;//本来是猫,向上转型成为动物,还原回来成为本来的猫 注意事项:

   a.必须保证对象本来创建的时候就是猫,才能向下转型成为猫

   b.如果对象创建的时候不是猫,现在非要向下转型成为猫,就会报错

多态数组

建立一个polyArr包

Arrtest.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

package com.polyArr;

public class Arrtest {

     public static void main(String[] args) {

         //父类的引用可以指向子类的对象

         Person []p= new Person[ 5 ];

         p[ 0 ]= new Person( "aaa" , 15 );

         p[ 1 ]= new Student( "asas" , 16 , 55 );

         p[ 2 ]= new Student( "ccc" , 17 , 66 );

         p[ 3 ]= new Teacher( "nnn" , 18 , 2000 );

         p[ 4 ]= new Teacher( "mmm" , 19 , 6666 );

         for ( int i= 0 ;i< p.length;i++)

         {

             //动态绑定机制

             //person[i]编译类型是Person,运行类型是根据实际情况判断的

             System.out.println(p[i].say());

         }

     }

}

Person.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.polyArr;

public class Person {

     private String name;

     private int age;

     public Person(String name, int age) {

         this .name = name;

         this .age = age;

     }

     public String getName() {

         return name;

     }

     public void setName(String name) {

         this .name = name;

     }

     public int getAge() {

         return age;

     }

     public void setAge( int age) {

         this .age = age;

     }

     public String say(){

         return "name=" +name+ " age=" +age;

     }

}

Student.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

package com.polyArr;

public class Student extends Person{

     private double score;

 

     public Student(String name, int age, double score) {

         super (name, age);

         this .score = score;

     }

     public double getScore() {

         return score;

     }

     public void setScore( double score) {

         this .score = score;

     }

     public String say(){

         return super .say()+ " score=" +score;

     }

}

Teacher.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.polyArr;

public class Teacher extends Person {

     private int salary;

     public Teacher(String name, int age, int salary) {

         super (name, age);

         this .salary = salary;

     }

     public int getSalary() {

         return salary;

     }

     public void setSalary( int salary) {

         this .salary = salary;

     }

     public String say(){

         return super .say()+ " salary=" +salary;

     }

}

运行结果

多态数组+向下转型

instanceof关键字

运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。

用法: result = object instanceof class

result为boolean类型

参数:

Result:布尔类型。

Object:必选项。任意对象表达式。

Class:必选项。任意已定义的对象类。

说明:

如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object

不是指定类的一个实例,或者 object 是 null,则返回 false。

但是instanceof在Java的编译状态和运行状态是有区别的;

注:

在编译状态中,class可以是object对象的父类,自身类,子类。在这三种情况下Java编译时不会报错。

在运行转态中,class可以是object对象的父类,自身类,不能是子类。在前两种情况下result的结果为true,最后一种为false。但是class为子类时编译不会报错。运行结果为false。

建立一个polyArr包

Arrtest.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package com.polyArr;

public class Arrtest {

     public static void main(String[] args) {

         //父类的引用可以指向子类的对象

         Person []p= new Person[ 5 ];

         p[ 0 ]= new Person( "aaa" , 15 );

         p[ 1 ]= new Student( "asas" , 16 , 55 );

         p[ 2 ]= new Student( "ccc" , 17 , 66 );

         p[ 3 ]= new Teacher( "nnn" , 18 , 2000 );

         p[ 4 ]= new Teacher( "mmm" , 19 , 6666 );

         for ( int i= 0 ;i< p.length;i++)

         {

             //动态绑定机制

             //person[i]编译类型是Person,运行类型是根据实际情况判断的

             System.out.println(p[i].say());

             if (p[i] instanceof Student)

             {

                 Student t1=(Student) p[i];

                 System.out.println(t1.learn());

             }

             else if (p[i] instanceof Teacher)

             {

                 Teacher t1=(Teacher) p[i];

                 System.out.println(t1.teach());

             }

         }

     }

}

Person.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.polyArr;

public class Person {

     private String name;

     private int age;

     public Person(String name, int age) {

         this .name = name;

         this .age = age;

     }

     public String getName() {

         return name;

     }

     public void setName(String name) {

         this .name = name;

     }

     public int getAge() {

         return age;

     }

     public void setAge( int age) {

         this .age = age;

     }

     public String say(){

         return "name=" +name+ " age=" +age;

     }

}

Student.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package com.polyArr;

public class Student extends Person{

     private double score;

     public Student(String name, int age, double score) {

         super (name, age);

         this .score = score;

     }

     public double getScore() {

         return score;

     }

     public void setScore( double score) {

         this .score = score;

     }

     public String say(){

         return super .say()+ " score=" +score;

     }

     public String learn(){

         return getName()+ "正在听讲" ;

     }

  }

Teacher.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package com.polyArr;

public class Teacher extends Person {

     private int salary;

     public Teacher(String name, int age, int salary) {

         super (name, age);

         this .salary = salary;

     }

     public int getSalary() {

         return salary;

     }

     public void setSalary( int salary) {

         this .salary = salary;

     }

     public String say(){

         return super .say()+ " salary=" +salary;

     }

     public String teach(){

         return getName()+ "正在上课" ;

     }

}

运行结果

到此这篇关于Java实例讲解多态数组的使用的文章就介绍到这了,更多相关Java多态数组内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://zal321.blog.csdn.net/article/details/124266477

查看更多关于Java实例讲解多态数组的使用的详细内容...

  阅读:23次