Java foundation (basis) ----- explain the three characteristics of object-oriented

   1. Package (Encapsulation)

     Package information hiding, also known, is the use of abstract data types and data based on the operation data packaged to make it an integral whole,

Data Hiding in the abstract internal data, as much as possible to hide details of the data, retaining only some of the interfaces make contact with the outside world

     Encapsulation of thought:

  • The class of property privatization
  • Public methods (setter & getter) to achieve call
package com.yyx.pratice;

/**
 * JavaBean
 */
public class Person {
    private String name;
    private Integer age;

    public Person() {
        super();
    }

    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
    }

    /*
     * Packaging Properties
     */
    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

   2. Inheritance (Inheritance)

     Inheritance is derived from an existing class in a new class, a new class can absorb data attributes and behavior of an existing class, and can extend new capabilities   

     Inheritance is expressed by a relation between the intersecting object class, such that certain object can inherit the data members and member of a class of objects method additionally.

Object If class B inherits class A, belongs to B, it will have all the class A or part properties (data attributes) and function (operation), we call the inherited class A base class, a parent class or superclass, and said B is a derived class inherits the class or subclass of a

     Note: inheritance java class supports only single inheritance: a class can only inherit from a parent class. On the contrary, a parent can have multiple children classes; a parent class can have multiple interfaces, that is, the interface may be multiple inheritance

           Subclass objects are instantiated to create objects of the parent class

2.1 parent

package com.yyx.pratice;

public class Person {
    private String name;
    private Integer age;

    public Person() {
        super();
        System.out.println ( "I am empty-argument constructor of the parent class" );
    }

    public Person(String name, Integer age) {
        super();
        this.name = name;
        this.age = age;
        System.out.println ( "I am the parent class constructor has a parameter" );
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

    public void walk() {
        System.out.println ( "walking!" );
    }
}

2.2 subclass

package com.yyx.pratice;

public class Student extends Person {
    private String stuNo;

    public Student() {
        super();
    }

    public Student(String name, Integer age, String stuNo) {
        super(name, age);
        this.stuNo = stuNo;
    }

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    @Override
    public  void eat () {
         Super .eat (); // display eat calls the parent class () method 
        System.out.println ( "I rewrote eat () method of the parent class" );
    }

    public void study() {
        System.out.println ( "Study their subclasses () method" );
    }
}

2.3 Test class

package com.yyx.pratice;

public class JavaPratice {
    public static void main(String[] args) {
        Student student=new Student();
        student.eat();
        student.walk();
        student.study();
        
        Student stu=new Student("李四", 23, "1232");
        stu.eat();
        stu.walk();
        stu.study();
    }
} 
The result:
I was empty-argument constructor of the parent class
to eat!
I rewrote eat the parent class () method
to walk!
Subclass own study () method
I have a parent class constructor parameter
to eat!
I rewrite eat the parent class () method
to walk!
subclass own study () method

   3.  polymorphism (Polymorphism)

     Polymorphisms, can be understood as a form of multiple phenotypes of things. Polymorphism Overload 1) a method of rewriting 2) subclass objects: two forms

     I.e. the parent reference Polymorphism subclass object points subclass object, the parent can call the parent class class references present in the methods and properties, can not call extension subclass, as references to the parent class is the sub-stack class object inherits the parent class;

But the newly added if forced to superclass converted into a subclass, then you can call the subclass and superclass method does not have the; at the same time, a method of the parent class only and is not overridden in the subclass defined in the parent class in under the circumstances, it can be referenced by the parent type of call;

For the method defined in the parent class, if subclasses override this method, then the reference to the parent class types will call the subclass of this method, which is the dynamic link

     Premise polymorphism using subclass object: to achieve inheritance or subclass ① ② have to have a rewriting of the parent class method

     Running into a compiled state and running. For the polymorphism, the compiler, the "look left", this reference variable understand the type of parent class; running, "Look to the right", focused on the real object entity: Object subclass. The method of execution is a subclass rewrite

package com.yyx.pratice;

public class JavaPratice {
    public static void main(String[] args) {
        Person person = new Student();
        person.eat (); // can call the parent class has a subclass of Person eat overridden () method of 
        IF (Person the instanceof Student) {
            Student stu1 = (Student) person;
            /*
             * Call the subclass only way
             */
            stu1.study();
            stu1.walk();
        }

        Behavior behavior = new Student();
        behavior.walk (); // only call walk subclass has been rewritten interface of Behavior () method of 
        IF (behavior the instanceof Student) {
            Student stu2 = (Student) behavior;
            /*
             * Call the subclass only way
             */
            stu2.study();
            stu2.eat();
        }
    }
}

class Student extends Person implements Behavior {

    @Override
    public void eat() {
        super.eat();
    }

    @Override
    public void walk() {
        System.out.println ( "people have to walk" );
    }

    public void study() {
        System.out.println ( "students to learn" );
    }

}

class Person {
    public void eat() {
        System.out.println ( "people have to eat" );
    }
}

interface Behavior {
    void walk();
}

 

Guess you like

Origin www.cnblogs.com/fengfuwanliu/p/11386492.html