In java learning, anonymous functions, constructor methods, constructor code blocks, calling constructor methods in constructor methods (small records in java learning), and java Constructor

Source: Internet
Author: User

In java learning, anonymous functions, constructor methods, constructor code blocks, calling constructor methods in constructor methods (small records in java learning), and java Constructor

In java learning, calling constructor methods in anonymous functions, constructor methods, constructor code blocks, and constructor methods (small records in java learning)

Author: Star)

 

Anonymous Functions

Anonymous object: an object without a name

 

Note:

1. Generally, an anonymous object is not used to assign values to the attribute, and the attribute value cannot be obtained. Every time a new object is created, it is a new object.

2. An anonymous object is never an object.

For example, person new (). name = "star ";No.

 

Benefits of anonymous objects: easy to write.

Use Cases of anonymous objects:
1. if an object calls a method once, it can be called using an anonymous object.
2. It can be called as an actual parameter in the Method

 

Constructor
Custom initialization methods in Object-C: init calls these methods when an Object is created;
1. attribute values can be initialized when an object is created.
2. The method is called only once when an object is created.



In java, this method is called the constructor.

Role of constructor: Initialize the object, and initialize the property value when the object is created.

 

Create a constructor:

1. the constructor does not return a type. The method name and class name must be consistent.

2. the constructor is not manually called. It is called by jvm (Java Virtual Machine) when an object is created.

3. If a class is not defined, jvm (Java Virtual Machine) will add a non-argument constructor to this class by default during compilation.

4. if you define a constructor, the jvm will not create a constructor without parameters.

5. When creating an object, there must be several parameters, and the corresponding constructor should also have several parameters.

Format:

1. modifier Class Name (parameter list ){}

2. Direct Class Name (parameter list ){}

The Code is as follows:

1 class Person{
2 / / public properties
3     String name;
4     int age;
Five
6 / / create a constructor (with two parameters)
7     public Person(String n,int a){
8         name = n;
9         age = a;
10}
11 / / nonparametric construction method
12     public Person(){
Thirteen
14}
Fifteen
16 / / behavior
17     public void study(){
18 system. Out. Println (age + "year old" + name + "learning");
19}
20}
21 public class Star_gzff {
Twenty-two
23     public static void main(String[] args) {
24 / / create a person object
25 person P = new person ("Star", 12); / / here is the construction method of two parameters
26 / / call normal method work
27         p.study();
Twenty-eight
29 / / no parameterless construction method. An error will occur here
30 person P1 = new person(); / / here is the construction method without parameters
31 p1.name = "Xiaoming";
32         p1.study();
33}
34}

 

Method overload

In java, methods can be renamed. We call this method a method overload.

Note:

1. A method with the same name is called a method overload.

2. Any method (common method, constructor) can implement method overloading.

3. Ensure that the order or number of parameters are different in the parameter list.

For example, Person (String a, int B );

Person (int B, String a); // different order

Person (String a, int B, String c); // different numbers

 

Construct code blocks

There are three types of code blocks:

 

1. Construct a code block

 

2. Partial code block: it is written in the method and expressed in braces.

 

3. Static code block

 

In the preceding constructor, a constructor can call a common method. This common method is used when an object is created without calling the method later.

For example:

Public Person (String n, int ){
Name = n;
Age =;
Study (); // call a common method
}

If we have many such constructor methods (without parameters, one parameter, two parameters ...), every call to this method makes the code complex and troublesome, so the code block is constructed.

Code block function:Unified object initialization. (This method is used before an object is created)

The format of the constructed code block: {} is a braces ..

Code Analysis:

1 class Person{
2 / / public properties
3     String name;
Four
5 / / building blocks
6 {
7         age = 18;
8}
9 / / create a constructor
10     public Person(String n,int a){
11         name = n;
12         age = a;
13}
Fourteen
15     int age = 20;
Sixteen
17     public void study(){
Eighteen
19 system. Out. Println (age + "year old" + name + "learning");
20}
21}
Twenty-two
23 public class Star_gzdmk {
Twenty-four
25     public static void main(String[] args) {
Twenty-six
27 person P = new person ("Star", 12);
28         p.study();
29}
30}

Note:

1. The construction code block must be written in the position of the member variable (attribute.

2. This method is used before the object is created.

3. When the java compiler compiles the java source file, it puts the declaration of the member variable at the beginning.

4. Initialization of member variables is performed in the constructor. Once compiled by the java compiler, the methods used to construct the code block are moved to the constructor for execution and placed at the beginning of the constructor.

5. If there is no constructor method, only the member variables initialize and construct code blocks and execute them in the order of the current Code.

In a common method, if you add a local variable with the same name as the attribute name:

System. out. println (name), which is the name of the local variable.

If the this keyword is used, System. out. println (this. name); is the name in the constructor.

In the constructor, if the input parameter name is the same as the attribute name, for example, name = name; // The two names obtained are local variables.

This. name = name; // The first name obtained is the attribute.

This Keyword:

1. indicates the call object of the method, which is similar to self in oc.

2. If the member variable and the local variable have the same name, the method calls the local variable by default (proximity principle). this allows the method to call the member variable.

3. If the method does not have a local variable with the same name as the member variable, the java compiler adds this to the variable by default.

 

Call constructor in Constructor

The constructor can call common methods (instance methods ).

Constructor can also be called in constructor.

The problem code is as follows:

class Student{
//Public attribute
String ID;
String name;
String classNum;
String sex;
Int age;
//Create construction method
public Student(String id,String name,String classNum,String sex,int age){
this.name = name;
This.id =id;
this.classNum = classNum;
this.sex = sex;
this.age = age;
}
//Overloaded construction method, only writing four parameter
public Student(String id,String name,String classNum,String sex){
this.name = name;
This.id =id;
this.classNum = classNum;
this.sex = sex;
}
//Overloaded construction method, write only three parameter
public Student(String id,String name,String classNum){
this.name = name;
This.id =id;
this.classNum = classNum;
}
//Common method
public void Student(){
}
}

In this way, the code is complex. At this time, we can try to call between constructor methods.

For example, the constructor of the four parameters contains the constructor of the three parameters. I only need to call the constructor of the three parameters in the constructor of the four parameters, adding a parameter that is different from the three parameters can simplify the code.

The code for solving the first step is as follows:

 

class Student{
//Public attribute
String ID;
String name;
String classNum;
String sex;
Int age;
//Create construction method
public Student(String id,String name,String classNum,String sex,int age){
Student (ID, name, classnum, sex); / / four parameters
this.age = age;
}
//Overloaded construction method, only writing four parameter
public Student(String id,String name,String classNum,String sex){
Student (ID, name, classnum); / / three parameters
this.sex = sex;
}
//Overloaded construction method, write only three parameter
public Student(String id,String name,String classNum){
this.name = name;
This.id =id;
this.classNum = classNum;
}
//Common method
public void Student(){
}
}

 

However, if you call this method like this, there will be a problem. You call not a constructor, but a common method.

Conclusion: You can call a method directly by using the method name. A common method is called instead of a constructor.

The final solution is as follows:

 

class Student{
//Public attribute
String ID;
String name;
String classNum;
String sex;
Int age;
//Create construction method
public Student(String id,String name,String classNum,String sex,int age){
this(id,name,classNum,sex);
this.age = age;
}
//Overloaded construction method, only writing four parameter
public Student(String id,String name,String classNum,String sex){
this(id,name,classNum);
this.sex = sex;
}
//Overloaded construction method, write only three parameter
public Student(String id,String name,String classNum){
this.name = name;
This.id =id;
this.classNum = classNum;
}
//Common method
public void Student(){
}
} 

 

Summary of constructor calls:

1. Calling a method using a method name is a common method, and this method is not used as a constructor.

For example: Student (name, id, classNum, sex );

2. It is also incorrect to add this before the method name.

Such as: this, Student (name, id, classNum, sex );

3. The correct method to call should be to call the constructor using this () directly. If the first statement is used, an error is reported.

Such as: this (name, id, classNum, sex );

This. id = id;

4. Do not call the constructor between two constructor methods. Will lead to an endless loop.

For example: this (name, id, classNum, 10); // a constant is uploaded.

 


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.