I. Encapsulation (with return value)
-1. The concept hides some of the class's information inside the class, does not allow the external program to access it directly, but uses the method provided by the class to implement the operation and access to the hidden information
-2. Syntax private int num;
3 steps
1. Hide member variables modify visibility to precede a member variable with a private result: Other classes display an error when invoking a member variable when creating an object
2. Create a common Getter/setter method in this class to create a common method public void SetName (String Name) {name=name1} when calling method Name.setname ("content to join");
3. Adding attribute restrictions in Setter/getter
* * Note:
1. The method name must be the first letter of the set start capital
2. The return value of a method must be the type of the member variable (the value returned is a member variable)
3. Use this this to represent the current object when calling your own member variables and member methods in this class
Two. concept of inheritance:
inheritance is special in its own--general relationship, that is, often said is-a relationship. The subclass inherits the parent class, indicating that the subclass is a special parent class and has a
Some properties or methods.
2. initialization sequence in inheritance:
From the structure of a class, there are four common patterns within it: attributes (including class properties and instance Properties), methods (including class methods and instance methods), constructors, and
Initializes the block (including the initialization block of the class and the initialization block of the instance). For the initialization order in the inheritance, it also divides into the initialization of the class and the initialization of the object.
Class initialization:
during the preparation phase of the JVM load class, the memory space is first allocated for all class properties and class initialization blocks of the class. And initializes it during the first initialization phase of the class, between class properties and class initialization blocks
The order in which they are initialized determines the order in which they are defined. If the class has a parent class, the class properties and class initialization blocks of the parent class are initialized first, beginning with the Object class.
Object initialization:
3. This and Super in succession :
The This in the constructor represents the object reference that is currently being initialized, and this in the method represents the object reference that is currently calling this method. This specific usage is shown in a few ways:
1. when there are multiple overloaded constructors, and one constructor needs to call another to construct it, It is called with this(param) in its first rowand only in the first row;
2. when a method in the object needs to call the other methods in this object, use this as the keynote, or do not write, in fact, the default is this as the keynote;
3. when the object property and the local variable name in the method are the same, in this method you need to explicitly use this as the keynote to represent the properties of the object, if this problem does not exist, you can not explicitly write this .
In fact, one of the problems involved is the search rule for variables: first local variables = variables defined in the current class , and variables defined in the parent class that can inherit from the quilt . Parent class ...
Super represents calling the corresponding properties and methods in the parent class. In the method, if you need to call the method of the parent class, be sure to write it in the first line
4. inheritance and combination:
From the simple realization effect, the inheritance and the combination can achieve the same goal. and are an effective way to implement code reuse.
But in the general concept level, the two have the obvious difference.
inheritance manifests itself as a general --special relationship, a subclass is a special parent class and a is-a relationship. The parent class has a generic attribute for all subclasses.
The combination manifests as the whole --part relation, namely the has-a relation. In the composition, the "part" is extracted separately to form its own class definition, and in the "whole"
in this class definition, a section is defined as one of these properties, and through the Get and set methods, you can invoke the properties and methods in the "partial" class.
When new creates an object, it first allocates memory for object properties and initialization blocks, and performs default initialization. If there is a parent class, first allocate memory and perform initialization for the parent class object and initialization block.
The initializer in the parent class constructor is then executed before the object properties and initialization blocks of the child class are initialized.
The
learns to summarize the knowledge points (encapsulation, inheritance) of Java object-oriented programming using mind mapping.