Inheritance concepts

package cn.learn.extend;

/*
   定义一个父类
 */
public class Employee {

    int num=20;
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employee(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Employee() {
    }

    public void method(){
        System.out.println("我来自父类!!");
    }
}
Package cn.learn.extend; 

/ * 
1. Employee generated by the subclass 
    Note: the same name and parent class member variables variables subclassing / member methods 
    (non-private member variables) There are two ways to access 
    directly: by subclass object access member variables / member method (excluding construction method) 
            to the left of the equal sign is the one who will priority is who, if not then keep looking up, never looking down (ie, the object of which is generated by the class who would take a) 
    indirect: access to member variables by member method 

2. overridden (override): in the hierarchy, parameter as the parameter list, the same method name, overwrite the parent class 
  overloads (overload): the same method name, parameter list is not like 

3. rewrite Note: returns the value of the subclass method must be less than or equal [] the parent class return value 
  extension Note: java.lang.Object class is the highest of all classes of common parent (ancestor class), java.lang .String is the Object class 

4. rewrite Note: permission subclass method (modifier) must be greater than or equal] [parent permission 
  extension Note: public> protected> (default) > priva te (?? This is the class member variables modified reason) 



@override - effectively rewrite notes, played detect whether the parameter list and consistency of 

 * / 
public  class Teacher the extends the Employee {

    int NUM = 100;     // may be the same name as the parent class 

    @Override 
    public  int getAge () {
         return Age; 
    } 

    @Override 
    public  void the setAge ( int Age) {
         the this .age = Age; 
    } 

    Private  int Age; 

    // rewritable the method of method,
     // call the three variables, the subclass num, topical num, parent NUM 
    @Override
     public  void method () {
         int NUM = 50; // local variables 
        System.out.println ( "local variable I is: "+ NUM); 
        System.out.println ( "I am a subclass of variables" + the this .num);       // foreign method such methods of NUM 
        System.out.println ( "I am a parent class in the variable" + Super .num );     // parent class NUM 
    } 
}
Package cn.learn.extend; 

/ * 
    Inheritance is a prerequisite polymorphic 
    subclass definition format 
    public class subclasses of the parent class name {name} extends 

    succession: 
    constructor call parent class is a subclass of precedence, since the subclass Constructors have "super ()", so the parent class constructor priority a default 
    [this is also the standard no-argument constructor of the class], but if I did not write the parent class constructor with no arguments, the default call will complain, 
    but in the constructor of a subclass can be in there they have a reference function to prevent an error in the format "super (parameter data)" 
    ** but only a subclass constructor call to be eligible, and must be the first statement, and can only be called a 

    subclass must call the parent class constructor, do not write it will default to call 


    the super keyword usage 
    1. access the member variables of the parent class method in a subclass 
    2. call the parent class method in a subclass method with the same name in the parent class 
    3. in the constructor subclass, access to the parent class constructor 

    this keyword 
    1.this access method used members of this class member variable, a method for distinguishing between members of a local variable with the same name 
    2. in this class to make Members of the present class method access member methods, can be omitted, the same name used to emphasize the method of distinguishing the parent class 
    3. In this constructor overload class, access methods another configuration, format: this (parameter data)
    Note: 3 this must be the first statement and called only once, we can see this with the super can not be used at the same time, another thing is to have constructed the Senate can not be called with no parameters, it will fall into an infinite loop 



 * / 
public  class the Extends {
     public  static  void main (String [] args) { 
        Teacher One = new new Teacher (); 

        one.method (); 
        one.setAge ( 21 is ); 
        System.out.println ( "current Age:" + one.getAge () + " \ T "+ one.num); // 21 is 100 


    } 

}

 

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11449545.html