Java basic learning note _ object-oriented 1

Source: Internet
Author: User
Object-oriented 1. features:
1) emphasize the object and let us change from the executor to the conductor; 2) It is an idea that is more in line with people's thinking habits; 3) It can simplify complicated things; the instructor concluded: anyone who owns the data provides external operations on the data. Typical Case: people draw circles on the blackboard and train drivers brake urgently.
2. Differences between local variables and member variables
1) scope of action: local variables: Within the method; member variables: in the class, valid for the entire class; 2) storage space: local variables: stack memory; member variables: heap memory; 3) initialization value: local variable: You must manually give the value before use; member variable: Default JVM initialization value;
3. in the object-oriented Feature encapsulation (encapsulation) Inheritance (inheritance) polymorphism (polymorphism) Java, an object is created using the new keyword. An object is used to encapsulate specific data. For example, an array is used, and an object is also used. Easy to error: class test {int num = 10; num = 30; // program compilation error, where the error occurs. Reason: The description of a class can only be attributes and behaviors, and cannot be a separate statement. Public void point (){
Int num = 20 ;}} 4. Anonymous object
An anonymous object can call attributes, but it does not make sense. An anonymous object will become junk after being called. An anonymous object can be passed as a parameter, but the address value is actually passed;
5. Encapsulation
Concept: hides the Implementation Details of Object Attributes and only provides public access. Benefits: Isolation of changes, ease of use, increased reusability, and improved security. Principle: hide all the content that does not need to be provided externally, hide all attributes, and provide public methods to access them.
1) Private keywords
Permission modifier, used to modify members (member variables and member functions). initialized members are only valid in this class. When a member is private, security is improved, but access permissions are reduced, in this case, you can access it by providing public methods. Benefits: You can control access to member variables in the method. Note: Private Members are only a form of encapsulation. Class person {} // There is no modifier before the class, but it is also a modifier with a higher access permission than private.
6. constructor 1) features:
* The method name must be the same as the class name. * The return value type cannot be defined as void. The method type is not declared before the method name. * The constructor cannot call the constructor, but must call the constructor. * A class can define multiple constructor methods, that is, the constructor can be overloaded and distinguished by the number, type, or order of parameters. Constructor details: If no constructor is defined when a class is defined, the compilation system automatically inserts a default constructor without parameters, which does not execute any code. Role: The main function is to complete object initialization. It can pass the parameters when defining an object to the object's domain.
2) differences between constructors and general functions. Function: constructor initializes an object. Generally, a function is defined because the object needs to satisfy certain functions. Execution: the constructor is executed when an object is created without calling it. Generally, when a function is called, it is executed and never executed if it is not called. 7. This keyword 1) indicates the reference of the current object, and this indicates who calls the method when using this. 2) When to use this? This can be used to distinguish between local variables and member variables. This can be used for writing, for example, comparing the ages of two objects. compare (p); this is used in constructor to call other constructor in this class. This () must be placed in the first line. summary from the above: this is a pointer to the Object Identity. 8. Partial code blocks such:
Public static void main (string [] ARGs) {int x = 10; system. out. println (x) ;}}role: defines the life cycle of a variable; position: defined in the method;
9. Construct a code block
Class test {system. out. println ("I am constructing code quickly"); // constructing a code block or initializing a code block quickly is a supplement to the constructor and takes precedence over the constructor execution .}} Note: If the source code of the member variable for display initialization (for example, int num = 10;) is placed before the code block is constructed, the display Initialization is executed first; if the initialization source code is displayed in the member variable after the constructed code block, the code in the constructed code block is executed first. In fact, Initialization is a supplement to the constructor. Initialization blocks cannot receive any parameters. initialization can be used to define attributes and methods that are common to all objects. Benefits: Improves the reuse of initialization blocks and the maintainability of the entire application.
10. Static keywords
The static modification method has one more call method than the non-static method. It can be a class name. call, for example, person. eat (); features: 1) when a class is loaded, the static state disappears as the class disappears, indicating that the lifecycle of the class is the longest; 2) the object exists first, make it clear that the static object exists first and then the object exists. 3) it is shared by all objects. 4) it can be called by class names. call;
11. Differences between instance variables (non-static member variables) and class variables (static variables)
1) existing location: the class variable (static variable) exists in the method area as the class is loaded; the instance variable exists in the memory as the object is established; 2) lifecycle: static variables have the longest life cycle and disappear with the disappearance of classes; instance variables disappear with the disappearance of objects; 3) Different: static variables are also called class variables, the entire class is shared by the entire class; the instance variable is the unique data of the object and belongs to the object;
12. Static code block
The static {} static code block is loaded with the class, and is executed only once when the class is loaded. The static code block has similarities and differences with the constructed code block: 1) similarities: When the JVM loads the class, before the constructor is executed, multiple values can be defined in the class. 2) differences: the static code block is executed before the non-static code block. The static code block is executed only once after the new code block is executed for the first time; the construction code block is executed once every new time. When all the methods in a class are static, we recommend that you use a private constructor to prevent other classes from inheriting;

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.