Java 基础 polymorphism(多态)

来源:互联网 发布:支持java的游戏引擎 编辑:程序博客网 时间:2024/05/21 07:13

多态: 当存在多个类依次继承(呈瀑布型继承),当在主函数中实例化最底层(辈份最小)的类时,构造函数的调用顺序是从最高辈分的类到最底层类。

----A superclass references refers to a subclass object. (note the opposite is a syntax error.)

e.g:

Animal [0] = new Dog();// Animal[0] is super reference, and the Dog() is the new object.


----Which subclass’s method to invoke is decided at the execution time. In detail, it depends on the data type of the object.

Abstract Classes and Methods


1. An abstract method is declared using keyword abstract.

2.Once a class has at least one abstract method, it must be declared as abstract class.

3.Important: abstract classes cannot be used to instantiate objects, because they are incomplete.

4.If a superclass is an abstract class, its subclasses must implement all the abstract methods in order to be instantiated objects; otherwise they are also abstract.

The previous statement implies that both the superclass and subclass can be abstract.

5Classes that can be used to instantiate objects are calledconcrete classes

6.The subclass of an abstract superclass can be abstract or concrete, depending on whether it implements all the abstract method(s) in the superclass


Instanceof:

instanceof operator

–Can help figure out what type of object is referenced

instanceof requirement: •Requirements of the operands of instanceof

–The data type of thereference and the right operand must have inheritance relationship, directly or indirectly.

instanceof operator

Can help figure out what type of object is referenced

if(superRefinstanceof SubClass1){

     // downcasting

     ((SubClass1)superRef).method1();

}

if(superRefinstanceof SubClass2){

     // downcasting

     ((SubClass2)superRef).method2();

}

if(superRefinstanceof SubClass3){

     // downcasting

     ((SubClass3)superRef).method3();

}


Final method and class:

final must can be initialized.

final class can be extended.


// this is the interface
package Lab3;/** * This is an interface * */public interface Package {public double cost();//determine cost for the packagepublic void input();//input data for the package}
// this is the Crate class. it is an abstract class.
package Lab3;import java.util.Scanner;/** * This class is an abstract class and it implement interface of Packsge * implement one method * */public abstract class Crate implements Package{public double amount;public void input(){if(this instanceof MetalCrate){System.out.print("Please input the weight for the "+getClass().getSimpleName()+ " (lbs) : ");}else{System.out.print("Please input the weight for the "+getClass().getSimpleName()+ " (lbs) : ");}Scanner input = new Scanner(System.in);double num = Double.parseDouble(input.nextLine());this.amount = num;}}

// these are the subclass of Crate

package Lab3;/** * This class is derived by Crate. *  * */public class MetalCrate extends Crate implements Package{public double cost(){return 1.3*this.amount;}}

package Lab3;/** * This class is derived by Crate. *  * */public class WoodCrate extends Crate implements Package{public double cost() {return 1.4*this.amount;}}


//this is the box class implement interface
package Lab3;import java.util.Scanner;/** * This class implement the interface of Package * implement two method cost and input.. */public class Box implements Package{private double amount;public double cost(){return 1.2*this.amount;}public void input(){System.out.print("Please input the weight for the "+getClass().getSimpleName()+ " (lbs) : ");Scanner input = new Scanner(System.in);double num = Double.parseDouble(input.nextLine());this.amount = num;}}

//this is the letter class implement interface.
package Lab3;import java.util.Scanner;/** * This class implement interface of package * it has two methods.. * */public class Letter implements  Package{private double amount;public double cost(){return 5*this.amount/100;}public void input(){System.out.print("Please input the number of pages for the "+getClass().getSimpleName()+ " (pgs) : ");Scanner input = new Scanner(System.in);int num = Integer.parseInt(input.nextLine());this.amount = num;}}

//main class

package Lab3;import java.util.Random;import java.util.Scanner;/** * Main class */public class lab3 {static Scanner scan = new Scanner(System.in);public static Package load_a_package() {Package pack = null;int rand = (new Random()).nextInt(4);switch (rand) {case 0:pack = new Box();break;case 1:pack = new Letter();break;case 2:pack = new MetalCrate();break;case 3:pack = new WoodCrate();break;}pack.input();return pack;}public static void main(String args[]) {Package pack = null;for (int i = 0; i < 5; i++) {System.out.printf("\n**** package %d****\n", i);pack = load_a_package();System.out.printf("The cost of this package is $%.2f\n",pack.cost());}}}

Output:


<interface> Package ( Crate (MetalCrate WoodCrate) Box Letter)





0 0
原创粉丝点击