OOP 3:ポリモーフィズム

3の最初に、オブジェクト指向機能:ポリモーフィズム

図1に示すように、多型:サブクラスの親クラス・オブジェクトへの参照。

              P1 =人の新しい新しい男();多型      

          (親)(サブクラス)

               小容量の容量

         ダブル同等のint型の同等

要約されているように:多型、エンティティを作成した後、あなたは親クラスのメソッドはサブクラスの存在下では存在しません呼び出すことはできません。メソッドの呼び出しは、親クラスをオーバーライドするサブクラスでなければなりません。

図2に示すように、第三のオブジェクト指向の特徴:多型。

  2-1、多型は何を指し?

            多型は、物事の様々な症状として理解することができます。

        )過負荷と1を書き換え、方法; 2)、多型サブクラスオブジェクト

  2-2、使用条件サブクラスオブジェクト多型を有する:(1)クラスの継承を有する;(2)、親クラスのメソッドのオーバーライドのサブクラスが存在しなければなりません

3、プログラムはコンパイラとオペレーティング状態に実行されます。

             (1)、多型について、コンパイラは、「見て左」、この参照変数は、親クラスのタイプを理解します

            (2)、実行時、実際の物理的なオブジェクト、オブジェクトのサブクラスに焦点を当て、「右に見て」。実行の方法は、書き換えの親クラスのサブクラスです。

   多型では、エンティティを作成した後、親クラスの存在下でのサブクラスが存在しません呼び出すことはできません。あなたが親クラスをオーバーライドするサブクラスを呼び出す必要があります。)

多型のサブクラスのオブジェクトは、プロセスにのみ適用されるプロパティには適用されません。

例:Personクラスは、int型のID 1002と定義し、人の人はまた、int型のID 1002に定義されているサブクラス、メインメソッドテストクラス内のステートメントがある:人物P1 =新しいマン() 。 System.out.println(p1.id);これは、値の出力1002を返し、多型のオブジェクトクラス属性が多型ではない場合があるため)。

場合は多形性ステートメント人物P1 =新しい男();その後、我々は関係なく、状況の呼び出しは、プロパティの対応するクラスの左側に番号を割り当て、多形の文であるか、P1プロパティを呼び出していないする必要があります。  

図4に示すように、上下の変換変換:

4-1は、アップキャスト:製造サブクラスオブジェクトは、親に割り当てられています。

例4-2次の手順:あなたが見るのP1とP2缶()メソッドを呼び出したい場合は、(C言語のキャストに似て)下向きに移行する必要があります。

人物P2 = 新しい女();

p2.eat(); p2.walk();

// p2.seeは();(参照()メソッドは、ユニークな女性のサブクラスである親クラスに存在しないので。)それは呼び出すことはできません

W1 =女性(女性)P2; // 下方遷移と同様C 強い言語RPM

グラフィック:

             

  1. instanceofのキーワード:

フォーマット:オブジェクトのinstanceofクラスAを

説明:オブジェクトは、クラスAのインスタンスがあるかどうかを決定します。これは、その後、trueを返すされ、そうでない場合はfalse。

プレゼンテーションの例:

IF(P1 instanceofの女性){// P1は女性タイプか否かが判断されます。

    女W2 =(女)P1; // p1が強いターン女性とタイプの女性であれば

            w2.see();

}

図6に示すように、問題の送信パラメータの複数のパラメータタイプ親クラス状態:

公共ボイドショー(人物P){}

没有多态的话调用show()方法时,传参数只能传递Person类型的;而有了多态后,可以传递Person类型的子类的参数。即形参列表中可以传递Person类的参数或Person类的子类的参数。

如果没有多态性

            public void show(Person p) {      }

            public void show(Woman w) {             }

            public void show(Man m) {          }

            以上这三个调用会有各自的形参传递;

            但有了多态性后一个带有Person类型形参的就可以实现以上三个,因为Woman和Man都是Person类的子类。

二、代码实例解析:

1、Person类(父类):

package com.atguigu.duotai;



public class Person {

    private String name;

    private int age;

    //有参的构造方法

    public Person(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

    public Person() {  //无参的构造方法

        super();

    }



    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 walk() {

        System.out.println("人走路");

    }

    public void eat() {

        System.out.println("人吃饭");

    }

}

2、Woman类(第一个子类):

package com.atguigu.duotai;



public class Woman extends Person{

    private boolean isBeauty;

    //getters和setters方法

    public boolean isBeauty() {

        return isBeauty;

    }

    public void setBeauty(boolean isBeauty) {

        this.isBeauty = isBeauty;

    }

    public Woman() {

        super();

    }

    public Woman(boolean isBeauty) {

        super();

        this.isBeauty = isBeauty;

    }

    //重写自定义方法

    public void walk() {

        System.out.println("女窈窕的走路");

    }

    public void eat() {

        System.out.println("小口吃饭");

    }

    public void see() {

        System.out.println(this.getName()+"今年"+this.getAge()+"岁了");

    }

}

3、Man类(第二个子类):

package com.atguigu.duotai;



public class Man extends Person{

    private boolean smoking;



    public boolean Smoking() {

        return smoking;

    }

    public void setSmoking(boolean smoking) {

        this.smoking = smoking;

    }

    //有参的构造方法

    public Man( boolean smoking) {

        super();

        this.smoking = smoking;

    }

    //无参的构造方法

    public Man() {

        super();

    }

    //重写从父类继承的方法;

    public void walk() {

        System.out.println("男儿走路,不低头");

    }

    public void eat() {

        System.out.println("男-大口吃饭");

    }

    public void see() {

        System.out.println(this.getName()+"看到了很多健身器材");

    }

}

4、TestPerson类(测试类):

package com.atguigu.duotai;

/*

 * 面向对象的第三个特性:多态性;

 * 1、多态性指的是什么?

 *      多态性可理解为一个事物的多种表现形式。

 *   1)、方法的重载与重写; 2)、子类对象的多态性

 * 2、子类对象多态性使用的前提:(1)、要有类的继承;(2)、要有子类对父类方法的重写

 * 3、程序运行分为编译状态和运行状态

 *      对于多态性来说,编译时,"看左边",将此引用变量理解为父类的类型

 *      运行时,"看右边",关注于真正对象的实体,子类的对象。那么执行的方法就是子类重写父类的方法。

 * (在多态性中,创建实体后,不能调用子类中存在而父类中不存在的方法。必须调用的是子类重写父类的方法。)

 * */

public class TestPerson {

    public static void main(String[] args) {

        Person p=new Person();

        p.eat();

        p.walk();

       

        Man m=new Man();

        m.eat();

        m.walk();

        System.out.println("**********");

//      new Man().eat();       也可以运行出结果

//      new Man().walk();

       

        //子类对象的多态性:父类的引用指向子类对象

        Person p1=new Man();   //向上转型

        p1.eat();  p1.walk(); //以左为虚拟方法调用,

//      通过父类的引用指向子类的对象实体,

//      当调用方法时实际执行的必须是子类重写父类的方法;

//      不可以调用子类中存在而父类中没有的方法

//      p1.see();      不能调用(因为see()方法是子类Man特有的,在父类中不存在。)

       

        Person p2=new Woman();     //

        p2.eat();  p2.walk();

//      p2.see();      不能调用(因为see()方法是子类Woman特有的,在父类中不存在。)

        Woman w1=(Woman)p2; //向下转型(类似于C语言的强转),使用强转符。

        w1.see();  //强转后就可以调用see()方法了

       

//      /*

//      * 错误:java.lang.ClassCastException

//      * */

//      Woman w2=(Woman)p1;     不可以将Man类型强转为Woman类型,因为二者都是Person的子类。

//      w2.see();

       

//      Woman w2=(Woman)new Man();

       

        //instanceof:

//      格式: 对象a instanceof 类A 解释:判断对象a是否是类A的一个实例。是的话返回true;否则false。

        if(p1 instanceof Woman) {  //判断p1是否是一个Woman类型。

            Woman w2=(Woman)p1;    //如果p1是Woman类型则用Woman强转

            w2.see();

        }

        if(p1 instanceof Man) {       //判断p1是否是一个Man类型的。

            Man m1=(Man)p1;    //如果p1是Man类型的则强转。

            m1.see();

        }

        if(p1 instanceof Person) {

            System.out.println("Hello world!");

        }

    }

}

运行结果如下:

人吃饭

人走路

男-大口吃饭

男儿走路,不低头

**********

男-大口吃饭

男儿走路,不低头

小口吃饭

女窈窕的走路

null今年0岁了

null看到了很多健身器材

Hello world!

 

三、多态性的一个使用:

package com.atguigu.duotai;

//多态使用的一个类子

public class TestAnimal {

    public static void main(String[] args) {

        TestAnimal t=new TestAnimal();

        t.func(new Animal());

        t.func(new Dog());       //因为有了多态性所以形参内用Animal的子类也行。

        t.func(new Cat());

    }

    public void func(Animal a) {//包括了:(1)、public void func(Dog a) {   }  和public void func(Cat a) {   }

        a.eat();

        a.jump();

       

        //还可以用强转

        if(a instanceof Dog) {

            Dog d=(Dog)a;

            ((Dog) a).say();

//          d.say();       二者结果相同;

        }

        if(a instanceof Cat) {

            Cat c=(Cat)a;

            ((Cat) a).catchMouse();

//          c.catchMouse();

        }

    }

}

class Animal{

    String name;

    int age;

   

    public void eat() {

        System.out.println("进食");

    }

    public void jump() {

        System.out.println("Jump");

    }

}

class Dog extends Animal{

    public void eat() {

        System.out.println("吃狗食");

    }

    public void jump() {

        System.out.println("Dog Jump");

    }

    public void say() {

        System.out.println("汪汪");

    }

}

class Cat extends Animal{

    public void eat() {

        System.out.println("吃猫食");

    }

    public void jump() {

        System.out.println("Cat Jump");

    }

    public void catchMouse() {

        System.out.println("猫抓鱼");

    }

}

四、多态的使用练习1:

结果如下所示:

四、多态的使用练习2:

 

1、GeometricObject类(父类):

package com.atguigu.duotai.test;



public class GeometricObject {

    protected String color;

    protected double weigth;

   

    public GeometricObject(String color, double weigth) {

        super();

        this.color = color;

        this.weigth = weigth;

    }

    public String getColor() {

        return color;

    }

    public void setColor(String color) {

        this.color = color;

    }

    public double getWeigth() {

        return weigth;

    }

    public void setWeigth(double weigth) {

        this.weigth = weigth;

    }

   

    public double findArea() {

        return 0.0;

    }

}

2、Circle类(GeometricObject类的第一个子类):

package com.atguigu.duotai.test;



public class Circle extends GeometricObject{  //报错,Circle有一个默认空参的,而GeometricObject没有。

    private double radius;



    public double getRadius() {

        return radius;

    }



    public void setRadius(double radius) {

        this.radius = radius;

    }



    public Circle(String color, double weigth, double radius) {

        super(color, weigth);

        this.radius = radius;

    }

    public double findArea() {

        return Math.PI*radius*radius;

    }

}

3、MyRectangle(GeometricObject类的第二个子类):

package com.atguigu.duotai.test;



public class MyRectangle extends GeometricObject{

    private double width;

    private double height;

    public MyRectangle(String color, double weigth, double width, double height) {

        super(color, weigth);

        this.width = width;

        this.height = height;

    }

    public double getWidth() {

        return width;

    }

    public void setWidth(double width) {

        this.width = width;

    }

    public double getHeight() {

        return height;

    }

    public void setHeight(double height) {

        this.height = height;

    }

   

    public double findArea() {

        return width*height;

    }

}

4、TestGeometric(测试类):

package com.atguigu.duotai.test;



public class TestGeometric {

    public static void main(String[] args) {

        TestGeometric t=new TestGeometric();

       

        Circle c1=new Circle("Green",2.3,1.0);

        Circle c2=new Circle("Red",2.3,1.0);

       

        MyRectangle m1=new MyRectangle("blue",2.3,3.0,2.0);

        t.displayGemoetricObject(c2);

       

        boolean b=t.equalsArea(c1, c2);

        System.out.println(b);

    }

   

    //判断两个对象的面积是否相等

    public boolean equalsArea(GeometricObject o1,GeometricObject o2) {

//      if(o1.findArea()==o2.findArea())

//          return true;

//      else

//          return false;

        return o1.findArea()==o2.findArea();

    }

    public void displayGemoetricObject(GeometricObject o) {

        System.out.println(o.findArea());

    }

}

 

错误整理:

1、

出现上图所示错误,原因是:SavingAccount类继承父类Account,父类Account类中为书写无参的构造方法。导致此类错误发生。

おすすめ

転載: blog.csdn.net/qq_39044046/article/details/93604857