JAVA语法学习多态数组/多态参数

JAVA语法学习多态数组/多态参数

大纲

  1. 多态数组基础
  2. 多态参数

具体案例

1.代码实例了解多态数组
在这里插入图片描述

package Teach;
//父类
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 Person() {
    }
    public String say(){
        return name + age;
    }
}
//===============================================
package Teach;
//子类teacher
public class Teacher extends Person {
    private double salary;

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

    public Teacher(double salary) {
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

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

    @Override
    public String say() {
        return super.say() + salary;
    }
}
//===================================================
package Teach;
//子类stuedent
public class Student extends Person{
    private double Score;

    public Student(String name, int age, double score) {
        super(name, age);
        Score = score;
    }

    public double getScore() {
        return Score;
    }

    public void setScore(double score) {
        Score = score;
    }

    @Override
    public String say() {
        return super.say() + Score;
    }
}
//==================================================
package Teach;
//主方法
public class Test {
    public static void main(String[] args) {
        Person people [] = new Person[5];
        people [0]= new Person("张三",33);
        people [1]= new Student("李四",18,100);
        people [2]= new Student("王五",19,99);
        people [3]= new Teacher("齐夏",22,10000000);
        people [4]= new Teacher("乔家劲",32,9999999);
        for (int i = 0; i < people.length ; i++) {
            System.out.println(people[i].say());
        }
    }
}

下面进行对多态数组优化,通过判断,来实现执行不同的类型的方法

package Teach;

public class Test {
    public static void main(String[] args) {
        Person people [] = new Person[5];
        people [0]= new Person("张三",33);
        people [1]= new Student("李四",18,100);
        people [2]= new Student("王五",19,99);
        people [3]= new Teacher("齐夏",22,10000000);
        people [4]= new Teacher("乔家劲",32,9999999);
        for (int i = 0; i < people.length ; i++) {
           if (people[i] instanceof Student){
               System.out.println(((Student)people[i]).study());
           } else if (people[i] instanceof Teacher) {
               System.out.println(((Teacher) people[i]).teach());
           } else if (people[i] != null) {
               System.out.println("just a person");
           }
        }
    }
}

总的来说就是利用instanceof来比较判断
2.多态参数
就是用父类的形参,传入子类的实参
3.代码实例
要求在这里插入图片描述
我做的升级版顺便复习

package Employeer;

public class Employee{
    private String name;
    private double mouthSalary;
    public double getAnnual(){
        return 3 * mouthSalary * 3.5;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMouthSalary() {
        return mouthSalary;
    }

    public void setMouthSalary(double mouthSalary) {
        this.mouthSalary = mouthSalary;
    }

    public Employee(String name, double mouthSalary) {
        this.name = name;
        this.mouthSalary = mouthSalary;
    }

    public Employee() {
    }
}//父类
//===============================================
package Employeer;

public class normalEmployee extends Employee{
    public String work(){
        return "007";
    }

    public normalEmployee(String name, double mouthSalary) {
        super(name, mouthSalary);
    }

    public normalEmployee() {
    }

    @Override
    public double getAnnual() {
        return super.getAnnual();
    }

}//子类normalEmplyee
//=====================================================
package Employeer;

public class manager extends Employee{
    private double bonus;

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    public manager(String name, double mouthSalary, double bonus) {
        super(name, mouthSalary);
        this.bonus = bonus;
    }

    public manager(double bonus) {
        this.bonus = bonus;
    }
    public String manage(){
        return "PUA";
    }

    @Override
    public double getAnnual() {
        return super.getAnnual() + getBonus();
    }
}//子类manager
//========================================================
package Employeer;

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入有几个人算工资");
        int number = myScanner.nextInt();
        Employee people[] = new Employee[number];
        for (int i = 0; i < people.length; i++) {
            System.out.println("姓名");
            String name = myScanner.next();
            System.out.println("请输入月薪");
            int monthSalary = myScanner.nextInt();
            System.out.println("你是普通员工还是经理");
            String choose = myScanner.next();
            if (choose.equals("普通员工")) {
                people[i] = new normalEmployee(name, monthSalary);
            } else if (choose.equals("经理")) {
                System.out.println("请输入你的奖金");
                double bonus = myScanner.nextDouble();
                people[i] = new manager(name, monthSalary, bonus);
            }
        }
        test tool = new test();
        for (int i = 0; i < people.length; i++) {
            if (people[i] instanceof normalEmployee) {
                System.out.println(people[i].getName());
                tool.testWork(people[i]);
                System.out.println("年薪是" + tool.showEmpAnnual(people[i]));
            } else if (people[i] instanceof manager) {
                System.out.println(people[i].getName());
                System.out.println("奖金是" + ((manager) people[i]).getBonus());
                tool.testWork(people[i]);
                System.out.println("年薪是" + tool.showEmpAnnual(people[i]));
            }
        }
    }

    public void testWork(Employee person) {
        if (person instanceof normalEmployee) {
            System.out.println("工作状态是" + ((normalEmployee) person).work());
        } else if (person instanceof manager) {
            System.out.println("工作状态是" + ((manager) person).manage());
        }
    }

    public double showEmpAnnual(Employee person) {
        if (person instanceof normalEmployee) {
            return person.getAnnual();
        } else if (person instanceof manager) {
            return person.getAnnual();
        } else {
            return 0;
        }
    }
}//主函数

结束

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挽天技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值