03_javase oop--Object-oriented (constructor, static keyword)

Source: Internet
Author: User

Object Oriented (iii)

first, constructor (construction method)

  1. Overview and role

Constructors are also called constructor methods, and when you construct an object of a class, the constructor runs to initialize the instance domain (member variable) to the desired state.

  function: The object's data (attributes) are assigned to the initialization, or no assignment can be made.

  2. Example:

  1) Code 

1  Public classPerson {2     //member Variables3String name;//name4     intAge//Age5 6     //Empty Constructor7      PublicPerson () {8 9     }Ten  One     //a parametric constructor A      PublicPerson (String name,intAge ) { -          This. Name =name; -          This. Age =Age ; the     } - } -  - classDemo { +      Public Static voidMain (string[] args) { -         //Empty Constructor +person P1 =NewPerson (); A         //Parametric constructor (initialized to object assignment) atperson P2 =NewPerson ("Wang Yangming", "55"); -  -     } -}

  There are two constructors, one is an empty constructor and the other is a parametric constructor. The constructor is always invoked with the execution of the new operator, and the constructor cannot be invoked on an already existing object to achieve the purpose of re-setting the instance domain .

  2) features of the constructor format:

    • The constructor has the same name as the class (same size)
    • Each class can have more than one constructor
    • Constructor parameters can have 0, one, or more parameters
    • The constructor does not return a value
    • Constructors are always called along with the new operator

 

  3. Overloading of constructors

1) Code

1     //Empty Constructor2      PublicPerson () {}3 4     //a parametric constructor5      PublicPerson (intAge ) {6          This. Age =Age ;7     }8      PublicPerson (String name) {9          This. Name =name;Ten     } One      PublicPerson (String name,intAge ) { A          This. Name =name; -          This. Age =Age ; -}

The four method names, such as person (), man (int age), or person (String name), are the same, regardless of the return value, only the argument list. This is the overload of the constructor method. Overloading is simply assigning values to different properties as needed at new.

Note: 1. If we do not give a construction method, the system will automatically provide an argument-free constructor.

2. If we give the construction method, then we will no longer use the system, if necessary, we must give it ourselves. It is recommended to always give a parameterless constructor.

  4. What is the difference between assigning values to member variables through the set method and the constructor?

  Before we give the Set/get method, but assign a value to the member variable, what difference does he have with the constructor assignment?

First, the constructor assigns a value to the object that occurs when the object that constructs the class is called with new execution. The constructor cannot be re-assigned to an object that already exists. The set method, after the object instantiation, is assigned to the member variable by calling the Set method, which can be re-assigned to update.

Second, the set method emphasizes modifying the property value, while the constructor initializes the object's properties.

Ii. process of object creation (memory map)

  

 third, the static keyword

  Features of 1.static:

1) loaded with the load of the class

2) Better than object existence

3) shared by all objects of the class (example: Class students have a teacher, then the teacher can be static, for students to share. )

4) can be called directly through the class name. In fact, he can also be called by the object name itself. Of course, it is recommended to call with the class name.

2.static Precautions:

A: There is no this keyword in the static method   
* How to understand it?
* Static is loaded as the class loads, and this is present as the object is created.
* Static than object exists first.
B: static methods can only access static member variables and static member methods
        * static method:
            * Member variable: can only access static variable
             * member method: Only static member methods can be accessed
        * Non-static method:
             * member variable: can be static or non-static
             * Member Method: However, it is a static member method, or it can be a non-static member method.
        * easy to Remember:
            * Static can only be accessed statically.

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" > 3. Static variables and member variables and differences

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" > first , a static variable is also called a variable of a class, loaded as the class loads. Member variables are also called object variables, which are created as objects are created.

So the specific differences are:

1) Different:

Static variables belong to class

Member variables belong to an object

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" > 2) different locations in memory:

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" > static variable belongs to the static zone in the method area

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" > member variable belongs to heap memory

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" > 3) memory time differs:
Static variables are loaded as the class loads and disappear as the class disappears

< Span style= "COLOR: #008000" > < Span style= "COLOR: #008000" >

4) Call different

A static variable can be called by a class name, or it can be called by an object

Member variables can only be called by object name.

If there is any mistake, please correct me.

Email: [Email protected]

03_javase oop--Object-oriented (constructor, static keyword)

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.