Polymorphism
java how to achieve multi-state? The benefits of using polymorphism?
Two types of reference variables:
compile-time type (a little fuzzy, usually a parent)
public void (the Pet PET);
determined by the type declaration.
Runtime type (running, particularly subclass which is which subclass)
determined by the actual corresponding object type.
There are multiple states have three necessary conditions:
We have inherited, to have method overrides the parent class reference point to subclass object
Why rewrite?
Parent class reference point to a certain subclass object using the parent object to call a subclass method,
The method calls must be rewritten.
Why polymorphism (encapsulation and inheritance)
1. Polymorphic
improved scalability and maintainability of the program?
Improve code reusability
2. Packages
Security
increase code reusability
3. inherit
improve code reusability
With two kinds of polymorphic forms of
Parameters 1. As a method of the parent class
public return type method name (object name parent type) { } public void Feed (the Pet PET) { }
2. As a parent class method returns a value (also referred to as a factory design pattern)
Returns the parent class value type as a method of public getPet the Pet (int type) { // declare a pet subject the Pet pet = null; Switch (type) { Case. 1: // reference object points subclass parent class pet = new Dog ( "Cai" 2,60 "golden"); BREAK; }
The following code :( particular application and configuration are omitted set, get setter)
public class Cat extends Pet{
private String color;
@Override
public void eat() {
System.out.println("猫在吃鱼");
}
@Override
public void bark() {
System.out.println("miao miao miao");
}
}
public class Dog extends Pet{
private String brand;
@Override
public void eat() {
System.out.println("狗在吃骨头");
}
@Override
public void bark() {
System.out.println("won won won");
}
}
public abstract class Pet { private String name; private int age; private int love; private int health; public abstract void eat(); public abstract void bark(); }
Two kinds of color polymorphic field is used as follows
the Person class {public / ** * pet adoption method * type as the type of the parent class method returns the value * @return * / public getPet the Pet () { Scanner Scanner new new INPUT = (the System.in); System.out.println ( "Please enter a pet adoption request type 1, 2 dogs, 3 cats, pigs"); int type = input.nextInt (); the Pet PET = null; IF (type == 1) { PET = new new dog () ; } the else IF (type == 2) { PET = new new Cat (); } {the else PET = new new Pig (); } return PET; } public void Feed (the Pet PET) { pet.eat (); } }
public class Test { public static void main(String[] args) { Person xiaoming = new Person(); Pet pet = xiaoming.getPet(); System.out.println(pet); xiaoming.feed(pet); } }
class can be seen that pig polymorphism scalability
Pig the extends the Pet class {public @Override public void EAT () { System.out.println ( "pigs eat cabbage"); } @Override public void Bark () { System.out.println ( "Heng hum"); } }