java - object-oriented three characteristics (encapsulation, inheritance, polymorphism)

A. Package

1. What is the package?

  Abstract operations and the data data package together, the implementation details hidden variables. Data is protected within other parts of the program can only access the data by authorized operation (member method).

Step 2. package? 

   1. Use a private member variable require modification package.

   2. The method of providing an open access is provided or private property. Get through (get / set) method

3. The package of benefits and role?

  The role of the package: 1 frame 2. tools

  Package benefits: 1. improve the safety of operation of the data simple 2. 3. hides the implementation of the method.

 

II. Inheritance

1. inherited form?

  The extends B A
  A: SubClass subclass
  B: SuperClass, super class, a parent class, the base class

2. The package benefits?

  1. To reduce code redundancy, to improve reusability of code 2. The code improved scalability 3. Providing polymorphic premise

3. Explain?

  1. subclass inherits the parent class after it has properties and methods of the parent class (we do not think we can inherit the parent class's constructor)
  2. subclass inherits the parent class must exist "is a" relationship
  3. Father after class property was privatized, a subclass can not be accessed directly, but we can access (set / get) through an indirect way, although the parent class of property can not be accessed directly, but we still believe that the property is inherited to .
  4. In addition to the subclass can inherit attributes and methods, but also can define their own unique attributes and methods
  The concept of the parent class is relative, the parent class is divided into: direct and indirect parent parent
  6. subclasses except properties and methods can get immediate parent class, the attributes and methods may also be obtained indirectly parent class
  7. All classes inherit Object class. If a class does not inherit a class display, then the default class will inherit the Object class
  8. A parent class can have multiple children, but a child can have only one parent class (Java's single inheritance characteristics)

4. The method of rewriting?

  After the subclass inherits the parent class, if not satisfied with the parent class's method you can consider rewriting method of use (coverage, rewrite)

  Description:

    1. subclass inherits the parent, if the parent class's method has been rewritten, then the subclass object by calling the parent class method is overridden, the actual method subclasses override calls.

  detail: 

    Permissions modifier return type method name (parameter list) {
      Method member
    }

    

    Method name and parameter list is rewritten method of the parent class and subclasses must override the same method
    2. The method override privileges parent modifiers Subclasses override privileges modifier method is not less than
      the parent class is returns a rewritten value subclass method return value is rewritten result
        Number Number Number and subclass may
        void void can
        int double not
    3. subclass of exception thrown is not greater than the parent class is rewritten method abnormal

    note:

      1. If the parent class is not private modifier then the method overridden by subclasses
      (not override method) 2. If the same method and subclasses, then added simultaneously without static or static (a rewrite process )

    4. @ Override // Note: for explaining a method of the current method is overridden

 

Summary: super and this difference:

     1. The representative is not the same thing:

          this: The caller object that represents your approach.

          super: on behalf of the parent class object reference space.

      2. Use the premise is inconsistent:

          this: can also be used in non-inherited conditions.

          super: can only be used under the conditions of inheritance.

      3. Call the constructor:

           this: class constructor calls this method.

           super: the parent class constructor calls

III. Polymorphic

Various forms of a class of things: 1. Polymorphism  

2. appreciated polymorphism:
  1. Generalized: rewriting method, overloading, polymorphism subclass object
  2. In a narrow sense: Polymorphism subclass object
3. subclass object polymorphism: pointing object referenced parent class subclass

public  class the Test { 
    
    public  static  void main (String [] args) { 
        
        // reference to the parent class to an object subclass of 
        the Person = the p- new new Woman ();    
         // virtual method application (dynamic binding): Compile look to the left, running look to the right 
        p.eat (); 
        p.show (); 
        
        // polymorphism drawback: You can not call the child class's unique methods and properties 
        
        / * 
         * demand: how to call subclass himself alone in the case of polymorphic some methods and properties 
         * downward transition 
         * / 
        
        Woman Woman = (Woman) P; 
        woman.buy (); 
        
        // a ClassCastException - cast exception
         // because the object P itself is transformed into a so Woman Man will Throws
 //         Man m = (Man) P;
 //        m.run (); 
        
        / * 
         * Demand: how to prevent the abnormal type conversion 
         * a instanceof A: a is an instance of an object type A. If it returns true, otherwise to false 
         * / 
        
        IF (P the instanceof Woman) { 
            System.out.println ( "to true" ); 
        } the else { 
            System.out.println ( "to false" ); 
        } 
        

    } 
}

 

Guess you like

Origin www.cnblogs.com/jiujiang/p/11601076.html