Java Object-oriented

Source: Internet
Author: User

1. Object-oriented and process-oriented

(1) Process-oriented processes, performers: it is a more in line with people's thinking habits of thought;

(2) Object-oriented emphasis object, conductor: It makes complex content simple;

(3) The characteristics of object-oriented thinking: It makes us from the executor to the conductor;

2. Object-oriented features

N. a package; inherit; polymorphic

3. Classes and objects

(1) A description of the same kind of things in the real world (member variables and methods);

(2) The actual existence of specific individuals in the real world of objects;

4. The difference between a member variable and a local variable

1. Scope:

The scope of a member variable is accessible throughout the class;

The local variable is the scope of the {} in which it is defined, and the statement in {} is reclaimed when it is executed;

2. Initial value:

The local variable must be manually assigned before it is used, the system will not assign a default value, and the member variable system assigns the default initial value .

3. Area of Memory:

Local variables are automatically released in the stack memory of the method's run, in the stack, including the arguments to the methods in the calling object, and the space in the stack memory when the method call is completed. When the method call is complete, it is freed.

The member variables are in the object, the objects are in the heap, the member variables are in the heap, and the unused objects are reclaimed by the garbage collection mechanism at an indeterminate time.

However, if you specify a constructor, the compiler will not specify it for you, that is, you cannot write a new point () to the one you specified.

 PackageCn.czbk.no4; Public classCar {String dest;intnum; Public voidrun () {intx=10; SYSTEM.OUT.PRINTLN (Num+"....."+dest); SYSTEM.OUT.PRINTLN (x);} Public voidShow () {System.out.println (num+dest);} Public Static voidMain (string[] args) {Car car=NewCar (); Car.run (); Car.show ();}}

6. Encapsulation

(1) The advantages of encapsulation: Easy to reuse, high safety.

(2) Private (private) permission modifier.

7. Private (proprietary)

Private decorated member variables and members cannot be accessed directly in other classes.

Note: Private is only a form of encapsulation, encapsulation is a kind of idea, that is, to hide the specific implementation, only exposed to external access mode.

8. Construction method

(1) Purpose: Used to initialize properties in an object.

A: Format class name () {Code}person () {}

B:person () {}public person () {}

(2) The characteristic does not define the null parameter construction, the system will automatically give an empty parameter constructs, defines the construction method whether has the parameter and the null parameter, the system no longer gives the null argument

(3) Overloaded construction method, which is based on the parameter list to distinguish which construction method.

9. This keyword

Each object has a pointer to itself, just this.

The role of this

1. Distinguish between member variables and local variables in a method. This member variable;

The 2.this statement can invoke other construction methods in the construction method;

3. In one method, this can call other methods of this class, which can be omitted:

(1) Represents a reference to the current object. When using, who calls the method, this represents who. (2) When do I use this?

A: You can use this to differentiate between the name of the authority variable and the member variable.

B:this Calling member Methods

C:this calls the other constructs of this class, note that the first line of the constructor method must be placed.

 PackageCn.czbk.no4; Public classtestthis {String name;    String sex;  PublicTestthis () {//This is to represent an object that is equivalent to the construction method so it cannot be testthis (' Xiao Wang ', ' Male ')         This("Xiao Wang", "Male"); }     Publictestthis (String name,string sex) { This. name=name;  This. sex=sex; }     PublicString GetName () {return  This. Name; }     Public voidsetName (String name) { This. name=name; }     PublicString Getsex () {return  This. Sex; }     Public voidsetsex (String sex) { This. sex=sex; }     Public Static voidMain (string[] args) {testthis test=NewTestthis (); System.out.println (Test.name+test.sex); }}

11. Detailed steps for creating an object in memory: person p = new person ();

1. Main method into the stack, assign space to class type variable P

2. Space in heap memory, allocating memory addresses

3. Establish object-specific properties in heap memory and initialize them by default

4. Explicitly initialize an attribute

5. Assign the memory address to the P variable in the stack memory

Static characteristics

Loads as the class is loaded, disappearing as the class disappears. Overrides the object. Static can be shared by all objects, or it can be called by the class name.

 PackageCn.czbk.no4; Public classteststatic {Static intA;StaticString s= "can be called when a class is loaded"; Public Static voidShow () {System.out.println ("You can call directly without creating an object");} Public Static voidMain (string[] args) {teststatic test=Newteststatic (); Test.show (); System.out.println (Test.s+test.a);}}
 Package Cn.czbk.no4;  Public class Staticdemo {publicstaticvoid  main (string[] args) {person P=New person ();p. Sayhi (person.b);}} class person{string A;  Public Static String b= "123"; void Sayhi (String s) {System.out.println (s);}}

13. Method Area

A method area is a region of memory shared by each thread that stores data such as the class information, constants, static variables, and immediately compiled code that have been loaded by the virtual machine.

Local code block & construct code block & static code block:

Local code block:

Function: The life cycle of a control variable constructs a block of code:

Function: It can initialize all objects and exist in the class. When a constructor in a class exists in an overloaded form, and there is a common member variable or a common method, it can be initialized by constructing a code block, which reduces the duplication of the code.

Static code block:

Action: Loads the drive. When a method in a class is statically constructed, and the constructor is private, then we cannot, when we instantiate the class, but want to add some properties to the class, we can use the static code block.

 PackageCn.czbk.no4; Public classTestsequence { Publictestsequence () {System.out.println ("Call constructor Method");} Testsequence (String s) {System.out.println ("Call construction Method" +s);}Static{inta=9999; System.out.println ("Static code Area" + (a+1));} {System.out.println ("Construct code Area");} Public Static voidMain (string[] args) {testsequence T=Newtestsequence (); Testsequence s=NewTestsequence ("abc"));}}

14. Static use

When a member of a class is modified with static, it has one more invocation method, the original object name. Members can continue to be called, now, through the class name. Member, can also be called.

When does the class load?

Loaded at the first use, and then resident memory.

. Final keyword

Final can be decorated with classes, methods, and variables. The final decorated class cannot be inherited. The final modified method cannot be overridden. A final modified variable is a constant that can only be assigned once.

Final and private differences

The final decorated class can be accessed.
Private can not decorate the outer class, but can decorate the inner class, when speaking the inner class will demonstrate
Final and private methods subclasses cannot be overridden, final does not allow overrides, private cannot override.
A final modified variable can only be assigned once when the display is initialized or the constructor is initialized, and cannot be changed later. Private modified variables, nor allow direct access or modification to the quilt class or other classes in a package, but he can

The set and get methods are used to change the value and value.

Java Object-oriented

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.