Inheritance, methods of class rewrite, anonymous objects and abstract classes

  If more than one class have the same attributes and behavior, then we just need to extract these same section, drawn to a single class (parent class); original classes (subclass) as long as inherit the parent class on well, no need to repeat the definition of these same attributes and behavior, but there are some differences in the same section, this time we need to modify the behavior shown in the following figure, tigers and rabbits have to eat on the behavior of these differences, but eat different things. Modification process is called rewriting method , but these animals also have their unique behavior, we complement these unique properties and behavior in the subclass, the required class to built.

A succession

  1, the definition of: subclass inherits the attributes and behavior of the parent class, subclass object that has the same properties of the parent class, the same behavior. Subclasses can directly access the non-private properties and behavior of the parent class.

  2, features: single inheritance and multiple layers inheritance. A subclass can have only one parent, but a parent can have multiple children classes, while the parent class can be used as a subclass of another class.

  3, advantages: to improve the reusability and maintainability of the code, so that the relationship produced (polymorphic premise) between classes.

  4, disadvantages: the coupling of the code to improve, (the principle developed for the high cohesion and low coupling)

  5, inheritance Keywords: extends using the format: public class extends the parent class subclass name {name}

  6, this keyword: represents the current object reference, who will call me, I who represents

  7, super Keywords: reference to the current object on behalf of the parent class

  8, the difference between this and super keywords keywords

  • a: Call to a member variable
    • this. member variable call this class member variables, member variables can also call the parent class
    • super. member variables to call the parent class member variables
  • b: call the constructor method
    • the this (...) method call the constructor of the present Super (...)    
    • Call the constructor of the parent class
  • c: Call to a member method
    • this. This method calls the members of the class member method, you can also call the parent's method
    •  super. members call the method of the parent class members

   9 Notes inheritance constructor

      • All subclasses constructor default constructor with no arguments will visit the parent class
      • If no parent class constructor with no arguments, you need to manually add the parent class constructor in subclass
      • Super (...) or this (....) must appear in the first statement of the method of construction (two constructors can only be one)
      • Parent space in preference to produce sub-class objects:    at each subclass object is created, initialize the parent class space, and then create a subclass object itself. Subclass object that contains the object in the corresponding space of the parent class, can comprise a member of the parent class, if the parent class members private non-modified, the sub-class parent class members are free to use. Code is reflected in the subclass constructor calls must first call the parent class constructor

Second, the method of rewriting

  1. What is the overriding method: When required function subclass the parent class, subclass and main function has its own unique content and method of the parent class can override. So that followed the functions of the parent class, and defines the type of unique content.    Parent child appeared in exactly the same way

  2. The process of rewriting Note:

    • style = "list-style-type: none;"> parent class private methods can not be overridden
    • style = "list-style-type: none;"> When subclass overrides the superclass method, access can not be less
    • style = "list-style-type: none;"> parent static method, subclass must also be rewritten by a static method
    • style = "list-style-type: none;"> subclass overrides the superclass method, the best exactly declared.

Third, anonymous objects

  1. What is anonymous objects: objects with no name

  2, features anonymous object: can only be used once, the call is complete rubbish, garbage collection is jvm recovery. (I want to use multiple anonymous object can not be used)

  3, advantages: save code

Fourth, the abstract class

  The method of the parent class is rewritten its subclasses have subclasses respective different implementations. Then the parent class method declaration and method body, in addition to the statement as well as the French and the body has no sense of the existence. We have no way subject method is called abstract methods. Java syntax requirements includes abstract methods of the class is an abstract class

  1, defining abstract methods: abstract modifier return type method name (parameter list);

  2, the abstract class: abstract class containing method must be an abstract class, an abstract class, but not necessarily an abstract method comprising

  3, defines an abstract class: public abstruct class name of the class {}

  4, Attention:

    Subclass of abstract class must have realized, otherwise it will lose its meaning, the object can not be created

    Subclass of abstract class must override all the abstract methods abstract parent class, otherwise, the compiler error and not through. Unless the subclass is also abstract class.

    An abstract class can have constructor for a subclass is to create objects, initialize the parent class members to use.

Guess you like

Origin www.cnblogs.com/haoyujun135/p/11328161.html
Recommended