首页 > 其他分享 >多态数组

多态数组

时间:2023-06-07 10:48:16浏览次数:29  
标签:name Person age 多态 persons score 数组 public

多态数组的应用1

  • 多态数组
    数组的定义类型为父类类型,里面保存的实际元素类型为子类类型

继承结构如下:

创建1个Person对象,2个Student对象和2个Teacher对象,统一放在数组中,并调用say方法

父类Person:

package hspedu.poly_.polyarr_;

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    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 + "\t" + age;
    }
}

子类Student:

package hspedu.poly_.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;
    }

    @Override
    public String say() {
        return super.say() + " score=" + score;
    }
}

子类Teacher:

package hspedu.poly_.polyarr_;

public class Teacher extends Person{
    private double salary;

    public Teacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String say() {
        return super.say() + " salary=" + salary;
    }
}

测试类PolyArray:

package hspedu.poly_.polyarr_;

public class PolyArray {
    public static void main(String[] args) {
        Person[] persons = new Person[5];
        persons[0] = new Person("jack",20);
        persons[1] = new Student("jack",18,100);
        persons[2] = new Student("smith",19,30.1);
        persons[3] = new Teacher("scott",30,20000);
        persons[4] = new Teacher("king",50,25000);

        //循环遍历多态数组,调用say()
        for (int i = 0;i < persons.length;i++){
            System.out.println(persons[i].say());
        }
    }
}

jack 20
jack 18 score=100.0
smith 19 score=30.1
scott 30 salary=20000.0
king 50 salary=25000.0

Process finished with exit code 0

多态数组的应用2

在for 循环中调用子类的特有方法:

package hspedu.poly_.polyarr_;

public class PolyArray {
    public static void main(String[] args) {
        Person[] persons = new Person[5];
        persons[0] = new Person("jack",20);
        persons[1] = new Student("marry",18,100);
        persons[2] = new Student("smith",19,30.1);
        persons[3] = new Teacher("scott",30,20000);
        persons[4] = new Teacher("king",50,25000);

        //循环遍历多态数组,调用say()
        for (int i = 0;i < persons.length;i++){
            System.out.println(persons[i].say());
            //persons[i].study();	//编译类型是Person,找不到子类特有方法,无法通过编译
            //persons[i].teach();
            if (persons[i] instanceof Student){ //判断person[i] 的运行类型是不是Student
                Student student = (Student) persons[i];	//强转引用的类型为子类类型,并将引用复制给另一个新的子类类型引用
                student.study();	//使用新的引用去调用子类特有方法
            }else if (persons[i] instanceof Teacher){//判断person[i] 的运行类型是不是Teacher
                Teacher teacher = (Teacher) persons[i];
                teacher.teach();
            }else if (persons[i] instanceof Person){
//                System.out.println("你的类型有误,请自行检查!");
            }else
                System.out.println("你的类型有误,请自行检查!");
        }
    }
}

标签:name,Person,age,多态,persons,score,数组,public
From: https://www.cnblogs.com/Hello-world-noname/p/17462628.html

相关文章

  • 数组和元祖数据类型
    使用案例来讲解TS的数组类型和元祖类型数组类型方式一要求:定义一个数组这个数组只能存储数值类型的数据letvalArr:Array<Number>//定义一个数组类型存储数值valArr=[1,2,3]//正常valArr=["1","2",3];//会报错console.log(valArr);方式二字符串数组......
  • 215. 数组中的第K个最大元素
    给定整数数组nums和整数k,请返回数组中第k个最大的元素。请注意,你需要找的是数组排序后的第k个最大的元素,而不是第k个不同的元素。你必须设计并实现时间复杂度为O(n)的算法解决此问题。 示例1:输入:[3,2,1,5,6,4],k=2输出:5示例 2:输入:[3,2,3,1,2,4,5,5......
  • 60 数组内交换头尾
    packagecom.fqs.test;importjava.util.Arrays;publicclasshello{publicstaticvoidmain(String[]args){//交换数组头尾交换//交换前12345//交换后54321int[]arr={1,2,3,4,5};inttemp=arr[0];for(int......
  • 57 动态初始化数组;求数组中的最大值
    packagecom.fqs.test;importjava.util.Arrays;publicclasshello{publicstaticvoidmain(String[]args){//动态初始化数组int[]arr=newint[3];for(inti=0;i<arr.length;i++){System.out.println(arr[i]);......
  • 56 数组遍历求和;找能被3整除的数
    packagecom.fqs.test;publicclasshello{publicstaticvoidmain(String[]args){//定义一个数组,存储12345求和int[]arr={1,2,3,4,5};intsum=0;for(inti=0;i<arr.length;i++){sum=sum+arr[i];}......
  • 55 打印数组
    packagecom.fqs.test;importjava.util.Arrays;importjava.util.Random;importjava.util.Scanner;publicclasshello{publicstaticvoidmain(String[]args){//需求程序自动生成一个1到100之间的随机数字A,键盘输入数B猜数字int[]arr={......
  • 合并数组与非合并数组 -- SystemVerilog
    合并型数组(packed):合并型数组可以实现连续的存储,赋值时不需要用 ’{}。 数组中,数据排列为{ b_pack[2], b_pack[1],b_pack[0]},其中每个b_pack为8个bit;bit是二值逻辑,每位bit只占据1位。故24位(8bit*3)只占据一个word(一般一个word为32bit)的存储空间。 非合并型数组......
  • 数组
        ......
  • 数组名和指针区别(转)
    指针和数组名的共同特点是都是用来指代一个地址的。不同的是:1、指针是需要占用内存空间来存储地址的;数组名则更像是一个立即数或者常数。你可以修改指针指向的内容,但你绝对无法改变数组名的指向。2、数组和指针对于sizeof来说是不同的,指针变量占用的空间通常等于当前CPU的最大......
  • 一个整数数组里面,除了两个数之外,其他的数字都出现了两次,写一个程序找出这两个数...
    一个整数数组里面,除了两个数之外,其他的数字都出现了两次,写一个程序找出这两个数,要求算法的时间复杂度为O(n). n为数组的长度。  程序代码如下: //取二进制中首个为1的位置intfindFirstOne(intvalue){intpos=0;while((value&1)!=1){value=value>>1;pos++;......