Article From:https://www.cnblogs.com/MrTanJunCai/p/9906774.html

*************************Constructor ************************ Constructor

Why why is used

Student s1 = new Student();

s1.sno = 1;//Break the encapsulation of programs
s1.name = “aaa”;
s1.age = 20;

//Recommend

s1.setSno(1);
s1.setName(“aaa”);
s1.setAge(20);

Assuming that the object has 10 attributes, 10 calls are required to create the object and assign values to the 10 attributes of the object.
setters()Method, obviously more trouble! Is there a way to create objects in one sentence at the same time for
All attribute assignments for this object?

Yes. Use constructors

Two.What

The constructor is a special function.

Function: It is used to initialize the properties of an object.

Constructors are also called constructors and constructors.

Three.How

public class Student{

public Student(){

}
}

Characteristic:

1. Same name as class

2. No return value (don’t even write void)

Demonstration code:

package com.chapter6.Demonstration constructor;

public class Student {

private int sno;
private String name;
private int age;

public Student(int sno,String name){
System.out.println(“Create the object int sno, String name, using the constructor with two parameters.
this.sno = sno;
this.name = name;
}
//Constructor
//Characteristic:
//1.Same name as class
//2.No return value (even void does not need to be written)
public Student(int sno,String name,int age){
System.out.println(“Use the constructor of three parameters to create the object int sno, String name, int age “;
this.sno = sno;
this.name = name;
this.age = age;
}

public void study(){
System.out.println(“I am learning.
}

public static void main(String[] args) {
//Student s1 = new Student(1,”aaa”,20);

Student s1 = new Student(1,”bbb”);
}
}

 

*************************The constructor does not write the essence of the return value ****************************************************

In fact, the constructor has a return value, which is a reference to the object just created.

public Student Student(int sno,String name){
System.out.println(“Create the object int sno, String name, using the constructor with two parameters.
this.sno = sno;
this.name = name;

return new Student(1,”aaa”,20);
}

perhaps

public void Student(int sno,String name){
System.out.println(“Create the object int sno, String name, using the constructor with two parameters.
this.sno = sno;
this.name = name;

}

Neither of the above is a constructor. It’s a common method, but it happens to have the same name as the constructor.

 

Constructors do not write return values, is a rule of Java designers, compilers are also based on this rule to find which method is the constructor…..

Instead of saying that the constructor really does not have a return value

 

*************************Default constructor****************************************

Student s1 = new Student(1,”aaa”,20);//The underlying layer calls the constructor to create the object

 

Student s1 = new Student();//We could also create objects when we didn’t learn constructors before.

Why? The reason is that even though we don’t write a constructor, the compiler automatically provides us with a parameter-free one.
The constructor, which has no parameters, is called the default constructor, or the default constructor.

What is the default constructor

A constructor without parameters is also called a default constructor and a constructor without parameters.

public class Student{

//default constructor
public Student(){
//super();
}
}

 

Note: Once a programmer writes a constructor with parameters, the compiler will not provide you with that default constructor

Programmers need to get into the habit of adding default constructors without parameters when they write constructors with parameters.

 

*************************Object initialization block**************************************************************

One.Why

Question: Since the function of initialization block and constructor is exactly the same, why do we need initialization block when we have constructor?

Is the initialization block redundant???

No

When the same code appears in multiple constructors, the same code can be extracted and extracted into the object initialization block.

Because the object initialization block takes precedence over the constructor, this solves the problem of code redundancy.

Two.What

Function: Like a constructor, it is used to initialize the properties of an object.

public class Student{

//1.attribute

//4.Object Initialization Block
{

}

//3.Constructor

 

//2.Method
}

 

Demonstration code:

 

package com.chapter6.Object initialization block;

public class Student {
private int sno;
private String name;
private int age;

private int grade;//grade

//Object Initialization Block
{
System.out.println(“Object initialization block execution “;
this.grade = 1;
}

public Student(){
System.out.println(“Default constructor execution “;
}

public Student(int sno,String name){
System.out.println(“The constructor of the two parameters executes int sno, String name “.
this.sno = sno;
this.name = name;
}

public Student(int sno,String name,int age){
System.out.println(“The constructor of the three parameters executes int sno, String name, int age “.
this.age = age;
this.sno = sno;
this.name = name;
}

public static void main(String[] args) {
Student s1 = new Student(1,”aaa”,20);
}
}

*******************************Object initialization process (or what exactly is done at the bottom of an object created with new)********************************************************

Student s1 = new Student(1,”aaa”,20);//Object initialization

 

1. Allocate space for instance variables of objects and initialize them by default

 

2. Initialization at declaration time and initialization of object initialization block (execution from top to bottom in the order of writing code)

 

3. Initialization of constructors

After performing the above three steps, the object is created and the address of the object just created is returned.

 

Instance variables are subordinate to objects, and each object has its own instance variables.

 

 

*******************************thisPointer******************************************************************

this The point is to translate myself into Chinese “I”

I. In the constructor

Pointing to the object being initialized

 

II. Use in Method

Pointing to the object calling the method

s1.study();//This in study () points to S1

 

3. Use this to call other constructors

public Student(int sno,String name,int age){

this(sno,name);//Calling the other constructor int String must go to the first sentence of the current constructor or or compile errors.

System.out.println(“The constructor of three parameters, int sno, String name, int age, is used.

this.age = age;
}

*******************************Overload************************************************************************************

What is heavy load

Method names are the same, and overloading is called between methods with different parameter lists.

The difference of parameter list is reflected in three aspects:

1. The number is different.

2. Different types

3. Different order

The compiler decides which method to call, first of all, by the name of the method. If introduce has multiple overloaded methods, the compiler decides which method to call through the type, number and order of the parameters.

To determine which method to call, if you encounter two methods of the same type (for example, the parameters are all int type, the compiler will be confused, there is a saying that you are wrong not to let the computer confuse the computer).

 

Interview Question: The name of the method is the same, and the list of parameters is the same. Only the methods with different return values are called overload???

The duplicate method compiler considers it to be a duplicate method. So it is not called overload.

 

Overload: Also known as static binding, static binding, pre-binding.

Binding bindings: The process of determining which code to execute by giving a method name is called binding.

Static Binding: When compiling, decide which code to execute.

Demonstration code:

package com.chapter6.Demonstration overload;

/**
* Author: Zhangngzy Company: Blue Bridge Software College Time: July 3, 2017 11:15:30 AM Function: Demonstration Overload
* The names of methods are the same, and overloading is called between different methods of parameter list. The differences of parameter list are reflected in three aspects: 1. Number of different methods, 2. Type of different methods and 3. Order of different methods.
*/
public class Student {

private int sno;
private String name;
private int age;

/*public int introduce(int sno) {

System.out.println(“I am a student, my school number is: “+sno”;
return 1;
}

public long introduce(int age){
System.out.println(“I am a student, my age is: “+age”;
return 1;
}*/

public void introduce(String name) {

System.out.println(“I am a student, my name is: “+name”;
}

public void introduce(int sno, String name) {
System.out.println(“I am a student. My student number is’+sno+’, and my name is’+name’.
}

public void introduce(String name, int sno) {
System.out.println(“I’m a student. My name is + name + and the number is + sno.
}

public static void main(String[] args) {
Student s1 = new Student();

s1.introduce(1);

}
}

 

********************************Constructors are also overloadable **********************************

**************************static Static**************************************************************************

I. Modification methods

1. Static method

Class name. Method name ();

 

Modified member variables

1. Static variables (class variables)

How to access static variables:

Class name. Attribute name

Subordinate to the class, all objects of the class share a static variable…

 

Requirements: Statistics on how many objects were created using the Student class

Demonstration code:

package com.chapter6.Demonstrate static;
/**
* Author: zhangzy
* Company: Blue Bridge Software College
* Time: 3 July 2017, 2:11:12 p.m.
* Function: Demonstrate static
* Requirements: Statistics on how many objects were created using the Student class
*/
public class Student {

private int sno;
private String name;
private int age;

private static int sum;

//Static Initiation Block
//Function: Initializes static attributes of classes
static{
System.out.println(“Static initialization block execution “;
sum = 10;
}

public Student(){
sum++;
}

public Student(int sno,String name,int age){
sum++;
this.sno = sno;
this.name = name;
this.age = age;
}

public static void study(){
System.out.println(“I am learning.
}

public static void main(String[] args) {
Student s1 = new Student(1,”aaa”,20);//1.Class Initialization 2. Object Initialization

Student s2 = new Student(2,”bbb”,25);//Perform object initialization only

s1.sum = 50;
System.out.println(s1.sum);//50
System.out.println(s2.sum);//50
System.out.println(Student.sum);//Recommend
//System.out.println(“A total of’+Student.sum+’objects were created.
}

}

*****************************Class initialization process********************************************************
Object Initialization Timing: New Object Timing

Class initialization timing: the first time a class is used

1.When using this class to create objects

Student s1 = new Student();

2.Calling static methods of classes

Student.study();

3.Static properties of access classes

Student.sum

 

Class initialization process

1. Class loading

Read the class binary (. class) to the method area

2.Allocate space for class static variables and initialize by default

3.Initialization at declaration time, static initialization block initialization (top-down order)

Static Initiation Block

Function: assign values to class attributes (static variables)

static {

}

**************************Differences between instance variables and static variables***************************************

I. Example variables

Subordinate to the object, each object has its own instance variable, the instance variable is allocated in the heap.

II. Static variables

Subordinate to the class, all objects of the class share a static variable, which is in the static zone.

 

3. Local variables

In the stack

 

***************************************************************

Class initialization takes precedence over object initialization

 

Leave a Reply

Your email address will not be published. Required fields are marked *