15 polymorphism

Override (Override) 
in the inheritance, the subclass exactly the same way if a signature (method name and argument types) method defines a parent class, referred overwritten (Override). 
Override (override) and Overload (overload) except that, if a different method signature is Overload, Overload is a new method; the same if the method signature, return value are the same, is Override. 
Note: The same method name, the same method parameters, return values but different methods, but also a different approach. In Java programs, such a situation, the compiler will complain. 
class the Person {
     public  void RUN () {} 
} 

public  class Student {the extends the Person 
    @Override // the Compile error! 
    public  void RUN (String S) {} 
} 
// add @Override allows the compiler to check whether the correct override, but not required @Override 


polymorphism (polymorphic) 
polymorphism means, to call for a certain type of method, a method that actually performs depends on the type of the actual running time method, and allows you to add more types of sub-categories and expand the functions, but do not need to modify the code based on the parent class. 
public  class{Main
     public  static  void main (String [] args) {
         // give an ordinary income, wages and enjoy special benefits a small partner considered taxes: 
        Income [] = incomes new new Income [] {
             new new Income ( 3000 ), // Common 
            new new the salary ( 7500 ), // wage 
            new new StateCouncilSpecialAllowance ( 15000 ) // allowance 
        }; 
        . the System OUT .println (totalTax (incomes,)); 
    } 
    // variable parameter Income class 
    public  static  Double totalTax(Income... incomes) {
        double total = 0;
        for (Income income: incomes) {
            total = total + income.getTax();
            //当income = Income(3000) ,会执行Income类中的getTax
            //当income = Salary(3000) ,会执行Salary类中的getTax
        }
        return total;
    }
}

class Income {
    protected double income;

    public Income(double income) {
        this.income = income;
    }

    public double getTax() {
        return income * 0.1; // 税率10%
    }
}

class Salary extends Income {
    public Salary(double income) {
        super(income);//初始化字段
    }
    
    @Override
    public double getTax() {
        if (income <= 5000) {
            return 0;
        }
        return (income - 5000) * 0.2;
    }
}

classThe extends Income {StateCouncilSpecialAllowance
     public StateCouncilSpecialAllowance ( Double Income) { 
        Super (Income); 
    } 

    @Override 
    public  Double getTax () {
         return  0 ; 
    } 
} 



using polymorphism, totalTax () method requires Income and deal, it does not need to know exactly Salary and there is StateCouncilSpecialAllowance, you can correctly calculate the total taxes. 

Object overwriting method 
because all ultimately inherited from class Object, Object and defines several important methods: 
toString (): the output instance String; 
the equals (): determining whether the logical instance is equal to two; 
the hashCode (): instance of a hash value is calculated. 

Where necessary, we can override Object of several methods used to achieve specific functions. 
class the Person { 
    ... 
    @Override
    public String toString () {
         return  " the Person: name = " + name; 
    } 
} 

call super 
in the override method subclass, if the parent class is overwritten to be called, can be invoked by the super. 
class the Person {
     protected String name;
     public String hello () {
         return  " the Hello, " + name; 
    } 
} 

class Student the extends the Person { 
    @Override 
    public String hello () {
         // call the parent class hello () method: 
        return Super. the Hello () + " ! " ;
    }
} 

Final 
Inheritance is a subclass can override the parent class allowed. If a parent class does not allow a subclass of its overwritten, the method can be marked as final. A final method can not be modified Override (overwritten): 
class the Person {
     protected String name;
     public final String Hello () { // do not allow overwriting 
        return  " the Hello, " + name; 
    } 
} 


if a class does not want any other class inherits from it, you can mark the class itself Final 
Final class the Person {
     protected String name; 
} 

a class instance field, can also be modified with a final. Modified with final field can not be modified after initialization. 
class the Person {
     public Final String name;
     public the Person (String name) { //Can be initialized in the final field structure 
        the this .name = name; 
    } 
} 


personal understanding: 
Polymorphism is a variety of ways to solve the problem of pre-written set type, the type is determined by the actual running time of how to call?

 

Guess you like

Origin www.cnblogs.com/nsss/p/11417494.html