One of the three characteristics of object-oriented JAVA: Succeeding

1, inheritance definition:

     Describe things when there are several duplicate class properties and methods , we can use the inherited method to design.

2, implemented using class A extends B class inheritance.

      Subclasses, A parent, B

3, the subclass inherits the parent class, subclass only get parent-Africa private property , you have to offer if you want to inherit a common set / get method .

       The method also can be public, private method can not be inherited

 

4, in addition to the subclass through inheritance, obtaining the structure of the parent class, also you can define their own unique component.

 

5, inheritance java only supports single inheritance , a class can only inherit from a parent class, on the contrary, a parent can have multiple children classes.

 

Methods rewrite:

1, provided that: there are subclass inherits the parent class

2, subclass inherits the parent class in the future, if the parent class subclass does not apply, then the subclass can override the parent class method

3, rewrite rules:

      1) The method of claim subclass' method return type name (parameter list) "and the method of the parent classes are the same.

       2) The method of subclass modifier modifier be not less than the parent class method.

       3) If the parent class throw exception, then the exception subclass toss abnormality can not exceed the parent class.

       4) Method subclasses and superclasses must be the same as the static or non-static is the same.

 

super keyword:

super keyword can be used to modify the properties, methods, constructors ;

1, when the sub-class and the parent class when the property of the same name, may be through Super. This property  explicitly call attribute declared in the parent class .

       Call the subclass of property of the same name by  this. This property  

2, when the parent class subclasses override, in subclass For methods to explicitly call parent class is rewritten using Super. Method .

      Note: For the property or method is not the same name ...., Or super super property method, this method will do this or properties.

3, super constructor modified by subclasses using super (parameter list) to explicitly call parent class constructor specified .

     1) within the constructor, Super (parameter list) must be declared in the first line;

      2) within the constructor, super (parameter list), or this (parameter list) appears only a;

     3) In the internal structure is not explicitly call super (parameter list), or this (parameter list) of any one of the default parameter empty call the parent class constructor

 

 

 

Code Share:

public class Person {
    protected String name = "oooXXX";
    protected String sex;
    private int age;
    int id = 142; // ID

    public Person() {
        // super (); // there is no will do, it will automatically call the default
        System.out.println ( "This is a person, no arguments constructor !!");
    }



    public Person(String sex){
        this();
        this.sex=sex;
        System.out.println ( "This is a person with a constructor parameter");
    }

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

    public void setSex(String sex) {
        this.sex = sex;
    }

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

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public int getAge() {
        return age;
    }

    public void eat() {
        System.out.println ( "eat myself !!!");
    }

    private void walk() {
        System.out.println ( "go up !!!");
    }

    public void sleep() {
        System.out.println ( "people sleeping !!!");
    }

}
public class Student extends Person {
    The coating method; // override method
    public void eat() {
        System.out.println("students are eating now???");
    }

    // this method is not rewritten, since there would be no walk subclass inherits the parent class () method
    public void walk() {
        System.out.println("students are walking???");
    }
}


public class Worker extends Person{
    int id = 101; // job number
    public Worker(){
        //super();
        super ( "Girls");
        System.out.println ( "This is no worker constructor parameter");
    }
    public void show(){
        System.out.println(this.id);
        System.out.println(super.id);
        System.out.println(super.name);
        System.out.println(this.name);
        System.out.println(this.sex);
        System.out.println(super.sex);
    }
    // override method
    public void eat(){
        super.eat (); // call the parent class is rewritten eat () method
        System.out.println ( "workers to eat it !!!");
        super.sleep();
        System.out.println("-------");
        this.sleep();
    }



}
public class JiChengTest {
    public static void main(String[] args) {
        Student stu = new Student();
        stu.eat();
        private subclass //stu.walk();// parent class can not be inherited
        //stu.name;// subclass can not inherit the parent class's private variables
        stu.setName("oppo");
        System.out.println(stu.getName());
        // subclass indirect access to private variables through public methods,
        stu.walk();

        Worker worker = new Worker();
        worker.eat();
        worker.setSex("男");
        System.out.println("worker::" + worker.getSex() + " " + worker.getName() + " " + worker.getAge());
        //worker::男 null 0

        Worker w2 = new Worker();
        w2.show();
        w2.eat();

       System.out.println(Person.class.getSuperclass().getName());
        //java.lang.Object---- person described here is the default parent class Object
    }


}

 

 

result:

This is a person, no arguments constructor! !
We are eating now ??? Students
OPPO
Students are walking ???
This is a person, no arguments constructor! !
This is the person are arguments constructor
this is a no-argument constructor worker
eat myself! ! !
Workers to eat it! ! !
People are sleeping! ! !
-------
man sleeping! ! !
worker :: Male oooXXX 0
This is a person, no arguments constructor! !
This is the person are arguments constructor
this is a no-argument constructor worker
101
142
oooXXX
oooXXX
girl
girls
eat myself! ! !
Workers to eat it! ! !
People are sleeping! ! !
-------
man sleeping! ! !
java.lang.Object

 

 

 

Published 45 original articles · won praise 8 · views 5849

Guess you like

Origin blog.csdn.net/wenyunick/article/details/104282515