Object-oriented Java Learning (2)

Source: Internet
Author: User

In describing things, the things that already exist have some content, this is we can define them in the constructor, then what is the constructor?

A constructor is a function that is called when a creation object is built, and it can initialize an object. The constructor is the same as the class name and the first letter is capitalized. If a constructor is not defined in a class, then the class must have a constructor for the default null argument. If the specified constructor is defined, then the system default constructor is not available and needs to be redefined once it is used. Also, constructors are not unique and can have multiple, targeted initialization for different objects. Multiple constructors are represented in the class in the form of overloads (as described later). Here are some code:

classperson{PrivateString name; PrivateString sex; Private intAge ; Person () {} person (String name) { This. name=name; } person (String name,string sex) { This. name=name;  This. sex=sex; } person (String name,string sex,intAge ) {        This. name=name;  This. sex=sex;  This. age=Age ; }}

The advent of constructors provides a great help to our initialization of objects. So what is the difference between a constructor and a general function?

There are two kinds of differences:

One: When an object is created, it calls its corresponding constructor to initialize the object. The general function is called after the object is created and requires function functionality.

Two: When an object is created, the constructor is called and called only once. The general function, however, can be called more than once after the object has been created.

What is encapsulation?

Encapsulation is one of the three main features of object-oriented thinking. Encapsulation is the hidden implementation details, providing access to the external interface only. The principle of encapsulation is to hide content that does not need to be made available externally, and to provide public access to the property after it is hidden. Encapsulation of properties, encapsulation of methods, encapsulation of classes, encapsulation of components, modular encapsulation, system-level encapsulation are generally encapsulated. What are the benefits after encapsulation? After encapsulation can be modular, information hiding, code reuse, plug-in easy to debug, and have a certain degree of security.

 class   person{ private   String name;     private   String sex;  private  int   age; Person () {}  public  void   SetName (String name) { this . Name=name;  public   String GetName () { return   name; }}

Note: You can use the keyword this to differentiate between member variables and local variables. This: Represents the current object, which is the reference to the object to which the function belongs. In simple words, which object calls the function where this is located, this represents which object. This can also be used in constructors to call other constructors. However, this reference can only be placed on the first line.

What do you mean by overloading a method?

You can create multiple methods in a class, all with the same name, but with different parameters and different definitions. The first letter of the method is lowercase, starting with the first letter capitalized as GetName () from the second word;

The method overloads the following code:

classdemo{.   .   .  Public intAddintAintb) {    returnA +b; }     Public intAdd (intAintBintc) {     returna+b+C; }      Public DoubleAdd (DoubleAfloatBintc) {     returna+b+C; }}

Talking about the static of the key words.

Static is the literal meaning, a global variable, and the static keyword is used to decorate members, including member variables and member functions. The modified members have the following characteristics: loaded with the load of the class, precedence over the object, being shared by all objects, can be called directly by the class name (class name. static member).

Note: Static methods that are decorated with static can only access static members, and non-static data cannot be called. And the static method can not write this,super and other keywords.

When it comes to static variables, you think of member variables, what is the difference between these two variables?

1, two variables have different life cycles:

The member variable exists as the object is created and released as the object is retracted;

Static variables are loaded as the class loads and disappear as the class disappears.

2, the method of invocation is different:

A member variable can be called only by an object, while a static variable may be called by an object or by a class name.

3. Different aliases:

A member variable is also known as an instance variable, and a static variable can also be called a class variable.

4, the location of the data storage is different:

Member variable data is stored in the object of the heap memory, also known as the object's unique data.

Static variable data is stored in the static area of the method area.

Static has this function, when to use static decoration?

1, when modifying static variables, when the values of the member variables in the Analysis object are the same, then the member variables can be defined as static, as long as the data in the object is different, is the object's unique data, must be non-static, if the same data, the object does not need to modify, as long as the use, It is not required to be stored in an object and can be defined as static.

2, modify the static method, whether the function is static decoration to see a bit: is the function function to access the unique data in the object. To put it simply: from the source point of view, whether the function needs to access non-static member variables, if necessary, the function is non-static.

The difference between static blocks of code, building blocks of code, and ordinary blocks of code:

A static block of code executes as the class loads and executes only once. The function is used to initialize the class.

A code block (method) that is written directly in the main function by a normal code block;

Constructing blocks of code can initialize all objects, and the initialized content is a function common to all objects.

Object-oriented Java Learning (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.