JAVA语法学习多态数组/多态参数
大纲
- 多态数组基础
- 多态参数
具体案例
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;
}
}
}//主函数
结束