Java多态

1.多态概述
多态是继封装、继承之后,面向对象的第三大特性。

多态现实意义理解:

现实事物经常会体现出多种形态,如学生,学生是人的一种,则一个具体的同学张三既是学生也是人,即出现两种形态。

Java作为面向对象的语言,同样可以描述一个事物的多种形态。如Student类继承了Person类,一个Student的对象便既是Student,又是Person。

3.多态体现为父类引用变量可以指向子类对象。

4.前提条件:必须有子父类关系。

注意:在使用多态后的父类引用变量调用方法时,会调用子类重写后的方法。

5.多态的定义与使用格式

定义格式:父类类型 变量名=new 子类类型();

6.理解:

多态是同一个行为具有多个不同表现形式或形态的能力。

多态就是同一个接口,使用不同的实例而执行不同操作。

7.多态中成员的特点
多态成员变量:编译运行看左边
Fu f=new Zi();

System.out.println(f.num);//f是Fu中的值,只能取到父中的值

8.多态成员方法:编译看左边,运行看右边

Fu f1=new Zi();

System.out.println(f1.show());//f1的门面类型是Fu,但实际类型是Zi,所以调用的是重写后的方法。

 9.多态的转型

多态的转型分为向上转型和向下转型两种


向上转型:多态本身就是向上转型过的过程
使用格式:父类类型 变量名=new 子类类型();

适用场景:当不需要面对子类类型时,通过提高扩展性,或者使用父类的功能就能完成相应的操作。

向下转型:一个已经向上转型的子类对象可以使用强制类型转换的格式,将父类引用类型转为子类引用各类型
使用格式:子类类型 变量名=(子类类型) 父类类型的变量;

适用场景:当要使用子类特有功能时。

10.instance关键字

     作用:用来判断某个对象是否属于某种数据类型。

    注意: 返回类型为布尔类型

public class Programmer {
    public void eat(){
        System.out.println("程序员吃饭");
    }
}

class Chinese extends Programmer{
    @Override
    public void eat() {
        System.out.println("中国人用筷子吃饭");
    }

    public void play(){
        System.out.println("中国人打太极");
    }
}

class India extends Programmer{
    @Override
    public void eat() {
        System.out.println("阿三手抓饭");
    }

    public void play(){
        System.out.println("阿三摩托玩的溜");
    }
}

class English extends Programmer{
    @Override
    public void eat() {
        System.out.println("英国人吃饭用刀叉");
    }
    public void play(){
        System.out.println("英国人玩摇滚");
    }
}

main

import java.util.concurrent.Callable;

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

//       Programmer programmer =new Chinese();
//       showEat(programmer);
//       Programmer programmer2 = new India();
//       showEat(programmer2);
//       Programmer programmer3 = new English();
//       showEat(programmer3);
        //或者
           Chinese ch =new Chinese();
           showEat(ch);
           India india = new India();
           showEat(india);
           English english = new English();
           showEat(english);
    }
    public  static void showEat(Programmer programmer){
        programmer.eat();
    }
}

输出

中国人用筷子吃饭
阿三手抓饭
英国人吃饭用刀叉

 存储不同类型的数据

public class Animal {

    String name;
    void showInfo(){
        System.out.println("your name is:"+name);
    }
    public void eat(){
        System.out.println("animal eat");
    }
}

class Duck extends Animal{
    @Override
    public void eat(){
        System.out.println("duck eat");
    }
}

class Goose extends Animal{
    @Override
    public void eat(){
        System.out.println("goose eat");
    }
}

main

import java.util.concurrent.Callable;

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

        //创建一个数组包含两种动物
        Animal[] aniarr=new Animal[2];
        Duck duck=new Duck();
        duck.name="ducklin";
        Goose goose=new Goose();
        goose.name="gooselin";
        aniarr[0]=duck;
        aniarr[1]=goose;
        for (int i = 0; i <aniarr.length ; i++) {
            aniarr[i].eat();
        }
   }
}

输出:

duck eat
goose eat

 有关返回值

public class Programmer {
    public void coding(){
        System.out.println("programmer coding");
    }
}

class Chinese extends Programmer{
    @Override
    public void coding() {
        System.out.println("中国人coding");
    }

    public void play(){
        System.out.println("中国人打太极");
    }
}

class India extends Programmer{
    @Override
    public void coding() {
        System.out.println("阿三coding");
    }

    public void play(){
        System.out.println("阿三摩托玩的溜");
    }
}

class English extends Programmer{
    @Override
    public void coding() {
        System.out.println("英国人coding");
    }
    public void play(){
        System.out.println("英国人玩摇滚");
    }
}

factory类

public class Factory {
    public static Programmer productProgrammer(String message){
        Programmer programmer = null;
        if ("China".equals(message)) {
            programmer=new Chinese();
        }else if("India".equals(message)){
            programmer=new India();
        }else if("English".equals(message)){
            programmer=new English();
        }

        return programmer;
    }
}

main

import java.util.Scanner;
import java.util.concurrent.Callable;

public class Test {
    public static void main(String[] args) {
        System.out.println("input your programmer type:");
        Scanner sc1 = new Scanner(System.in);
        String result = sc1.next();

        Programmer programmer=Factory.productProgrammer(result);
        if (programmer!=null){
            programmer.coding();
        }else {
            System.out.println("sorry,we do not have");
        }
   }
}

 练习1

public class Traffic {
    public void drive(){
        System.out.println("driving");
    }

    public static void main(String[] args) {

        Car car =new Car();
        Bicycle bike = new Bicycle();
        Traffic[] traffic={car,bike};
        for (int i = 0; i < traffic.length; i++) {
            traffic[i].drive();
        }
    }
}

class Car extends Traffic{
    @Override
    public void drive() {
        System.out.println("car driving");
    }
}

class Bicycle extends Traffic{
    @Override
    public void drive() {
        System.out.println("bike driving");
    }
}

输出

car driving
bike driving

 练习2

package com.atguigu.day09;

public class Employee {
    private String name;

    public String getName() {
        return name;
    }

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

    public Employee(){

    }

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

    public double earning(){
        return 0;
    }

    public String getInfo(){
        return "name is:"+name+" earing is:"+earning();
    }
}
package com.atguigu.day09;

public class SalaryEmployee extends Employee{
    private double salary;
    private int workday;
    private int offday;

    public double getSalary() {
        return salary;
    }

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

    public int getWorkday() {
        return workday;
    }

    public void setWorkday(int workday) {
        this.workday = workday;
    }

    public int getOffday() {
        return offday;
    }

    public void setOffday(int offday) {
        this.offday = offday;
    }

    public SalaryEmployee(){

    }
    public SalaryEmployee(double salary,int workday,int offday,String name){
    super(name);
    this.offday=workday;
    this.workday=offday;
    this.salary=salary;
    }
    @Override
    public double earning() {
        return salary-salary/workday*offday;
    }
}

class Manager extends SalaryEmployee{
    private double comms;

    public Manager() {
    }

    public Manager(double salary, int workday, int offday, String name, double comms) {
        super(salary, workday, offday, name);
        this.comms = comms;
    }

    @Override
    public double earning() {
        return (getSalary()-getSalary()/getWorkday()*getOffday())*(1+comms);
    }
}
package com.atguigu.day09;

public class HourEmployee extends Employee {
    private  int workHour;
    private double hourPrice;

    public int getWorkHour() {
        return workHour;
    }

    public void setWorkHour(int workHour) {
        this.workHour = workHour;
    }

    public double getHourPrice() {
        return hourPrice;
    }

    public void setHourPrice(double hourPrice) {
        this.hourPrice = hourPrice;
    }

    public HourEmployee(){}

    public HourEmployee(String name, int workHour, double hourPrice) {
        super(name);
        this.workHour = workHour;
        this.hourPrice = hourPrice;
    }

    @Override
    public double earning() {
        return workHour*hourPrice;
    }
}
package com.atguigu.day09;

import java.util.Scanner;
import java.util.concurrent.Callable;

public class Test {
    public static void main(String[] args) {
        Employee[] employees = new Employee[5];
        SalaryEmployee s1=new SalaryEmployee(10000,20,2,"lin1");
        SalaryEmployee s2=new SalaryEmployee(11000,19,3,"lin2");
        HourEmployee h1=new HourEmployee("lin3",10,300);
        HourEmployee h2=new HourEmployee("lin4",20,300);
        Manager m1=new Manager(2000,21,1,"lin5",90);

        employees[0]=s1;
        employees[1]=s2;
        employees[2]=h1;
        employees[3]=h2;
        employees[4]=m1;
        for (int i = 0; i <employees.length ; i++) {

            System.out.println("name: "+employees[i].getName()+" earing:"+employees[i].earning());
        }
   }
}

 向下转型

public class Animal {

    String name;
    void showInfo(){
        System.out.println("your name is:"+name);
    }
    public void eat(){
        System.out.println("animal eat");
    }
}

class Duck extends Animal{
    @Override
    public void eat(){
        System.out.println("duck eat");
    }
    public void swim(){
        System.out.println("duck swim");
    }
}

class Goose extends Animal{
    @Override
    public void eat(){
        System.out.println("goose eat");
    }
    public void walk(){
        System.out.println("goose walk");
    }
}

main

package com.atguigu.day09;

import java.util.Scanner;
import java.util.concurrent.Callable;

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


        Animal an2=new Duck();
        Animal an3=new Goose();
        Duck duck1=(Duck)an2;
        Goose goose1=(Goose)an3;
        duck1.swim();
        goose1.walk();
   }
}

输出

duck swim
goose walk

 instance例子

        Animal an1=new Animal();
        Duck duck2=new Duck();
        Goose goose2=new Goose();
        boolean b1 = duck2 instanceof Duck;
        boolean b2 = goose2 instanceof Animal;
        System.out.println(b1);
        System.out.println(b2);

输出结果都是true

原文地址:https://www.cnblogs.com/hbxZJ/p/15516340.html