1, "encapsulation": the State
information of the object is hidden, does not allow direct access, but through the method provided by the class Ali to achieve internal information access and operation.
Modifier used: Private, without modifier (default), Protected,public,
Private: The current class access, suitable to hide the property within the class, can only be accessed through methods. (commonly used to hide)
Default: The package access permission, as long as the other classes under the package that they belong to are accessible.
Protected: Subclass access, can be the same package, or different package access. generally used for subclass overrides.
Public: Common Properties, which can be accessed by any class. (commonly used for exposure)
Note: This explains why local variables are not modifiers. Because the scope of its local variables is fixed, it is within a method, or code block, so no additional modifiers are required to qualify its scope.
Encapsulation method: Use the Setter _ Getter method to access the hidden properties.
Usage column: Private String name;//defines a hidden property
Public String GetName ()//syntax: modifier data type get[encapsulated property name first letter uppercase]
{
return name; "The return value is the same as the data type above"
}
public void Setnmame (String name) "Note": This is set in and does not require a return value
{
This.name=name;
}
2, "package": the introduction of the package provides a class of multi-level namespaces, for resolving class naming Conflicts , class file management issues, "The general company's own package name is more than the company domain name of the inverted write"
Package Name: all lowercase letters;
Syntax: Package "pack Name"
Note: 1 . A source file can specify only one package, and the classes under the same package are freely accessible.
2. when the corresponding package is not imported with import, the class using the child package in the parent package must use the full name of the child package, which can be used directly after importing the package.
3, import java.util.*; This represents the import util all classes under this package, the * number is a wildcard character.
4, import static Package.packagename.classname, this is the appropriate class to import this package under the property.
5, when you define the package, using the javac-d source code to compile,-d means that the system will automatically generate a folder for the packages, and put the generated class under it, when the execution will automatically look for, of course, the child package as a sub-folder.
3, "Inheritance": Subclasses Inherit all the "Properties" and "methods" of the parent class, " but cannot get the constructor of the parent class ".
Syntax: "Modifier" class "subclass" extends "parent class"
{//code block
}
Note: 1. A class can have only one direct parent class, and there can be countless indirect parent classes. "Single Inheritance"
2, in fact, the inheritance is from the general to the special situation, namely: "is a"; For example, Apple is a fruit.
3. All classes are subclasses of the object class. That is, if the defined class does not explicitly inherit a class with extends, then the object class is inherited by default.
4, overriding the parent class method: A, method name, formal parameter list are the same.
The methods that the subclass overrides must have access permissions that are greater or equal than the access rights of the parent class.
The return value type of the Class C, subclass override method must be smaller or equal than the return value type of the method in the parent class.
"Note": 1, overrides are also called Overrides, that is, subclasses override the method of the parent class, will overwrite (mask) The method of the parent class, when it needs to call with super (parameter) "Take advantage of different parameters, find different methods", "but if the parent method has a private decoration, You cannot invoke or override the ".
2, the constructor of the subclass always calls the constructor of the parent class once, which is equivalent to loading the parent class's related properties and methods onto the subclass.
3, plus @override: To prevent the rewriting of the parent class method is an error, compile time to give a strict check , if not, then only run when the exception can be thrown.
"A direct distinction between similar names"
A, overloading : A way to define different methods of the same parameter name by different parameters between different methods of the same class.
override : Occurs between a subclass and a parent class.
B,this: The invocation takes place inside a class.
Super: Occurs between a child class and a parent class, and the subclass invokes the initialization code, property, or method of the parent class through this keyword.
Note: Since this and super can only be placed on the first line of the method, it is not possible to appear at the same time.
5, polymorphism: Java variables may have two types throughout the use, compile-time corresponding compilation type, and the runtime by the actual value assigned to determine its variables.
Compile-time type: determined by the variable that declares it.
Run-time type: determined by the type actually pointed to (actually given).
6. Keyword: "Reference type Variable" instanceof "class, or interface" Determines whether the preceding object is an instance of the following class, if the return value is true, if the return value is false.
Note: 1, the type of its operand, either the same as the following class, or an inheritance relationship with the following class, or it will cause a compilation error.
2, usually first use instance to determine whether an object can be coerced type conversion, and then forced type conversion, thus guaranteeing the correctness of the program.
7. Initialize the code block: Initializes a class or an instance.
Syntax: "modifier" {Initialize code block}; Note: Modifiers can only be static, or can be omitted.
Features: No name, so cannot be referenced, as long as the creation of the object will be implicitly executed.
There is no static difference: There is static class initialization block, when the class is loaded to initialize the class, implicit execution (automatic execution). No static is automatically executed when an instance of the class is created.
When to execute: Static modifies the execution of the class one load, without the static when the instance is created ( the order of execution is determined by the location of the code block and the constructor ), and if the initialization code block is executed before the constructor, then the code block in the constructor is executed. Conversely, the code in the constructor is executed before the initialization code block is executed.
Getting Started with Java Learning (6)-encapsulation, inheritance, polymorphism, This,super, initial code block