Java面向对象基础三大特征之多态

1.多态

1.1多态的概述

  • 什么是多态

    ​ 同一个对象,在不同时刻表现出来的不同形态

  • 多态的前提

    • 要有继承或实现关系
    • 要有方法的重写
    • 要有父类引用指向子类对象

1.2多态中的成员访问特点

  • 成员访问特点
    • 成员变量
      ​ 编译看父类,运行看父类
    • 成员方法
      ​ 编译看父类,运行看子类
  • 代码示例
    • 动物类

      public class Animal {
          public int age = 40;
      
          public void eat() {
              System.out.println("动物吃东西");
          }
      }
      
    • 猫类

      public class Cat extends Animal {
          public int age = 20;
          public int weight = 10;
      
          @Override
          public void eat() {
              System.out.println("猫吃鱼");
          }
      
          public void playGame() {
              System.out.println("猫捉迷藏");
          }
      }
      
    • 测试类

      public class AnimalDemo {
          public static void main(String[] args) {
              //有父类引用指向子类对象
              Animal a = new Cat();
      
              System.out.println(a.age);
      //        System.out.println(a.weight);
      
              a.eat();
      //        a.playGame();
          }
      }
      

1.3多态的好处和弊端

  • 好处

    ​ 提高程序的扩展性。定义方法时候,使用父类型作为参数,在使用的时候,使用具体的子类型参与操作

  • 弊端

    ​ 不能使用子类的特有成员

1.4多态中的转型

  • 向上转型

    ​ 父类引用指向子类对象就是向上转型

  • 向下转型

    ​ 格式:子类型 对象名 = (子类型)父类引用;

  • 代码演示

    • 动物类
    public class Animal {
        public void eat() {
            System.out.println("动物吃东西");
        }
    }
    
    • 猫类
    public class Cat extends Animal {
        @Override
        public void eat() {
            System.out.println("猫吃鱼");
        }
    
        public void playGame() {
            System.out.println("猫捉迷藏");
        }
    }
    
    • 测试类
    public class AnimalDemo {
        public static void main(String[] args) {
            //多态
            //向上转型
            Animal a = new Cat();
            a.eat();
    //      a.playGame();
    
    
            //向下转型
            Cat c = (Cat)a;
            c.eat();
            c.playGame();
        }
    }
    

1.5多态的案例

  • 案例需求

    ​ 请采用多态的思想实现猫和狗的案例,并在测试类中进行测试

  • 代码实现

    • 动物类
    public class Animal {
        private String name;
        private int age;
    
        public Animal() {
        }
    
        public Animal(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 void eat() {
            System.out.println("动物吃东西");
        }
    }
    
    • 猫类
    public class Cat extends Animal {
    
        public Cat() {
        }
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("猫吃鱼");
        }
    }
    
    • 狗类
    public class Dog extends Animal {
    
        public Dog() {
        }
    
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("狗吃骨头");
        }
    }
    
    • 测试类
    public class AnimalDemo {
        public static void main(String[] args) {
            //创建猫类对象进行测试
            Animal a = new Cat();
            a.setName("加菲");
            a.setAge(5);
            System.out.println(a.getName() + "," + a.getAge());
            a.eat();
    
            a = new Cat("加菲", 5);
            System.out.println(a.getName() + "," + a.getAge());
            a.eat();
        }
    }
    

练习题

练习1;

class Person {
 protected String name="person";
 protected int age=50;
 public String getInfo() {
 return "Name: "+ name + "\n" +"age: "+ age;
 }
 }
 class Student extends Person {
 protected String school="pku";
 public String getInfo() {
 return "Name: "+ name + "\nage: "+ age
 + "\nschool: "+ school;
 }
 }
 class Graduate extends Student{
 public String major="IT";
 public String getInfo()
 {
 return "Name: "+ name + "\nage: "+ age
 + "\nschool: "+ school+"\nmajor:"+major;
 }
 }
/*
建立InstanceTest 类,在类中定义方法
method(Student stu);
在method中:
(1)根据e的类型调用相应类的getInfo()方法。
(2)根据e的类型执行:
如果e为Person类的对象,输出:
“a person”;
如果e为Student类的对象,输出:
“a student”
“a person ”
如果e为Graduate类的对象,输出:
“a graduated student”
“a student”
“a person” 
*/

详细代码

  • Person类
public class Person {
    protected String name="person";
    protected int age=50;
    public String getInfo() {
        return "Name: "+ name + "\n" +"age: "+ age;
    }
}
  • Student类
class Student extends Person {
    protected String school="pku";
    public String getInfo() {
        return "Name: "+ name + "\nage: "+ age
                + "\nschool: "+ school;
    }
}
  • Graduate类
class Graduate extends Student{
    public String major="IT";
    public String getInfo()
    {
        return "Name: "+ name + "\nage: "+ age
                + "\nschool: "+ school+"\nmajor:"+major;
    }
}
  • InstanceTest类(测试类)
public class InstanceTest {
    public static void main(String[] args) {
        InstanceTest test = new InstanceTest();
        Student s = new Student();
        test.method(s);
    }

    public void method(Student s) {
        System.out.println(s.getInfo());

        if (s instanceof Graduate) {
            System.out.println("a graduated student");
        }

        if (s instanceof Student) {
            System.out.println("a student");
        }

        if (s instanceof Person) {
            System.out.println("a person");
        }
    }
}

练习2:
定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。

GeometricObjectprivate String color;
private int weight;
提供有参/无参构造方法,setter/getter,提供findArea()方法:计算面积用的

Circleprivate double radius;
提供有参(含父类)/无参构造方法,setter/getter,重写findArea()方法:计算圆面积

MyRectangleprivate double width;
private double height;
提供有参(含父类)/无参构造方法,setter/getter,重写findArea方法:计算矩形面积

/*
定义一个测试类GeometricTest,编写equalsArea方法测试两个对象的面积是否相等,编写displayGeometricObject方法显示对象的面积。(注意:通用性!)*/

详细代码

  • GeometricObject类
public class GeometricObject {
    private String color;
    private int weight;
    //findArea()方法:计算面积
    public GeometricObject(){}
    public GeometricObject(String color,int weight){
        this.color=color;
        this.weight=weight;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getColor() {
        return color;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public int getWeight() {
        return weight;
    }
    public double findArea(){
        return findArea();
    }
}
  • circle类
public class Circle extends GeometricObject {
    private double radius;

    public Circle(){
    }
    public Circle(String color,int weight, double radius){
        super(color,weight);
        this.radius=radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    @Override
    public double findArea() {
        return Math.PI*radius*radius;
    }
}
  • MyRectangle类
public class MyRectangle extends GeometricObject{
    private double width;
    private double height;
    //提供有参(含父类)/无参构造方法,setter/getter,重写findArea()方法
    public MyRectangle(){}
    public MyRectangle(String color,int weight,double width,double height){
        super(color, weight);
        this.height=height;
        this.width=width;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getHeight() {
        return height;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double getWidth() {
        return width;
    }
    @Override
    public double findArea() {
        return width*height;
    }
}
  • GeometricTest类(测试类)
public class GeometricTest {
    public static void main(String[] args) {
        GeometricTest test = new GeometricTest();
        GeometricTest test1=new GeometricTest();
        Circle circle = new Circle();
        MyRectangle myRectangle = new MyRectangle();
        //给半径赋值
        circle.setRadius(4.0);
        circle.findArea();
        //长宽赋值
        myRectangle.setWidth(2);
        myRectangle.setHeight(3.0);
        myRectangle.findArea();

        test.equalsArea(circle.findArea(),myRectangle.findArea());

        test.displayGeometricObject(circle.findArea(),myRectangle.findArea());
    }
    //equalsArea方法判断面积是否相等
    public  void equalsArea(double area1,double area2){
        if (area1==area2){
            System.out.println("面积相等");
        }else {
            System.out.println("面积不相等");
        }
    }
    //displayGeometricObject方法显示对象面积
    public void displayGeometricObject(double area1,double area2){
        System.out.println("圆面积为:"+area1);
        System.out.println("矩形面积为:"+area2);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值