Java object-oriented thinking 2

Source: Internet
Author: User

1. The main function is a special type of function that can be called by a virtual machine as a program entry. The main function format is fixed.
Public: The function has the most access rights.
Static: The delegate function already exists with the loading of the class.
void: The main function does not have a specific return value.
Main: Not a keyword, it is a special word that can be recognized by a virtual machine.
(string[] arr): An array of function arguments, string elements. Arr can be changed.
When the virtual machine calls the main function, it passes in new string[0]; Called as: Class. Method name ();
3. When do I use static? Static loads as the class loads.
Use static variables: when there is shared data in the object. The data is statically decorated. Unique data is defined as non-static in heap memory.
Use static functions: "When the function does not have access to non-static data (the unique data of the object), the function can be defined as a static"
For example: public void Show () {System.out.print ("OK")};} can add static
4. Static application (tool Class)
Each application has common features that can be extracted and packaged separately.
If there is no unique data in a class, there are only a few specified methods, then the method of that class can be statically. And the construction can be privatized
You can save space in heap memory by making new objects accessible only through the class name.
5. Static code block:
static{
Statements executed by static code blocks.
}
Executes only once, as the class is loaded. and takes precedence over the main function, constructor execution.
Used to initialize a class. Constructs a block of code to initialize an object. constructor to initialize the corresponding object.
To construct a code block:
{
Constructs a code block statement.
}
6.person p=new person (); Stack memory the main function has p (address)
Execution order: ① load Person.class (class) file loaded into memory->② static code block execution (if any)->③ heap memory space Open, assign address
->④ establishes the unique properties of the object in heap memory and initializes the object with the default initialization->⑤ the display initialization of the property->⑥ to construct a block of code.
->⑦ initializes the object with the corresponding constructor initialization ⑧ the memory address to the P variable in the stack memory.
7. Non-static omitted "this." Static omitted "class name."
Static no access to heap memory (object non-static)
The constructor points to the heap memory, and the method is stored in the method area.
8. Design pattern: Partial thinking (the most effective way to solve a kind of problem)
Singleton design pattern: Resolves a class that only has one object in memory.
Single example design mode 2: A hungry man type, lazy type.
9. Object-oriented: inheritance, child inherit parent. Child extends father.
"This class" "Super. Parent class"
Inheritance: To improve code reusability, classes and classes produce relational polymorphism.
Tips: You must have an owning relationship between classes and classes to inherit. Affiliation (IS...A)
If the content subclasses in the parent class should not inherit, then do not inherit.
Only single inheritance is supported in Java, and multiple inheritance is not supported. (A class can inherit only another class)
Multiple inheritance poses a risk: when the same functionality is defined in multiple parent classes, the functionality is not the same, and it is an error.
Java supports multilayer inheritance (b extends a;c extends B)
Use the features in the inheritance system: Check the parent class feature to create a subclass object usage feature.
"Variable: A variable with a non-private name is added with this class. Use the parent class to add super.
Function: A child class appears as a function of the parent class, and the subclass object calls the function, which runs the contents of the subclass function as if the parent function were overwritten. This condition is another feature of the function;
10. Aggregation: Has a (parent in child)
Polymerization:
11.final:
1. You can modify classes, functions, and variables.
2. A class that is final modified cannot be inherited.
3. The final modified method cannot be replicated, nor can it be inherited
4. A final modified variable can be assigned only once, both to modify the member variable and to modify the local variable. Plus the final modifier specification for all letters capitalized, multiple words composed, between words through _ connection.
5. When the inner class defines a local position in a class, only the local variables that are final decorated are accessible.
12. Abstract class: When multiple classes appear the same function, but the functional body is different, pull up, only define the function, do not extract the functional body.
1. Abstract methods are in the abstract class.
2. Abstract methods and abstract classes must be decorated with the abstract keyword.
3. Abstract classes cannot create objects with new, and invoking abstract methods makes no sense.
4. When a method in an abstract class is used, a subclass object invocation must be established after all the abstract methods have been replicated by the subclass, "if the subclass covers only a subset of the abstract methods, then the subclass is an abstract class"
13. Object-oriented interface: (LIKE...A ... )
Interface: Can be understood as a special abstract class. When abstract class methods are abstract, the class can be represented in the form of an interface.
The ① interface is not available to create objects because there are abstract methods. Requires quilt class implementation. Subclasses can be instantiated only after the ② subclass overrides the abstract method in the interface. Otherwise, the subclass is an abstract class. 】
The ③ interface is a static constant ④ interface can be implemented by the class (the class and interface is the implementation relationship), is the multi-inheritance does not support the conversion form. 】
Class is used to define classes. The interface is used to define interfaces.
Interface Format features:
1. Interface Common definition: constant (final), abstract method
2. The members in the interface have fixed modifiers, and the members are public.
Constants: public static final.
Method: Public abstract.
14. The inheritance of the interface is changed to implement (implements)
15. A class can inherit a class that inherits multiple interfaces at the same time (only multiple inheritance exists between interfaces).
Features: interfaces can be implemented as multiple implementations (classes and interfaces are implementation relationships), and are not supported for multi-inheritance conversion forms.
Interfaces and interfaces can inherit more.
Interface external Exposure rules
Interface is a function extension of a program
Interfaces can be used to implement multiple implementations.
16. Basic functions defined in the class, extension functions defined in the connector

Java object-oriented thinking 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.