Java-initialization and cleanup (construction method, overloading, this, initialization, parameter list)

Initialization and cleanup (construction method, overload, this, initialization, parameter list):

Initialization and cleanup provide a safe way for the development of computer science.

The following are the main points and points to be noted for initialization and cleaning:

 

1. Constructor:

Initialization is one of the methods to ensure program safety. In order to ensure that the program is initialized before operating the object, we have the concept of a constructor.

a. The name of the constructor: the same as the class name.

eg:

import java.util.Random;

class Gclass{
	Gclass(){     //构造方法
		System.out.println("我是一个构造方法");
	}
}

class Pclass{
	public void cclass(){    //普通方法
			System.out.println("我是一个普通方法");
	}
}

public class a {

	public static void main(String[] args) {
		new Gclass();      //构造方法的调用
		Pclass t = new Pclass();
		t.cclass();        //普通方法的调用
	}

}

output:
我是一个构造方法
我是一个普通方法

b. Constructor parameters:

A constructor that does not accept any parameters is called a parameterless constructor

eg:

import java.util.Random;

class Gclass{
	Gclass(int i){     //构造方法
		System.out.println("我是一个构造方法");
	}
}

class Pclass{
	public void cclass(int i){    //普通方法
			System.out.println("我是一个普通方法");
	}
}

public class a {

	public static void main(String[] args) {
		for(int i = 0;i < 5;i++){
			new Gclass(i);      //构造方法的调用
			Pclass t = new Pclass();
			t.cclass(i);        //普通方法的调用
		}	
		
	}

}

output:
我是一个构造方法
我是一个普通方法
我是一个构造方法
我是一个普通方法
我是一个构造方法
我是一个普通方法
我是一个构造方法
我是一个普通方法
我是一个构造方法
我是一个普通方法

c. The return value of the constructor: The constructor has no return value, even if it is void.

 

2. Method overloading

Most languages ​​require a unique identifier for each method. However, because the names of the constructors are the same, there is the concept of overloading. In layman's terms, overloading means that several methods have the same name. In order to distinguish and use the same name method, however, the overloaded parameter list must be different.

 

a. Method overload:

eg:

class TreeHeight{
	int h;
	TreeHeight(){
		System.out.println("This is tree!");
	}
	
	TreeHeight(int hh){
		System.out.println("This is new tree "+hh+" feet tall");
	}
}

public class d {

	public static void main(String[] args) {
		for(int i = 0;i < 5;i++){
			new TreeHeight(i);
			new TreeHeight();
		}
		
	}

}
output:
This is new tree 0 feet tall
This is tree!
This is new tree 1 feet tall
This is tree!
This is new tree 2 feet tall
This is tree!
This is new tree 3 feet tall
This is tree!
This is new tree 4 feet tall
This is tree!

 

b. The distinction between overloaded methods:

Each overloaded method must have a unique parameter list, even if only the order of the parameters is different.

 

3.this keyword

When we use this, this generally appears in the method of the class. When this method is not called, who this refers to is unknown, but in reality, if the new object comes out, this refers to the current object. In layman's terms, this is for easy distinction and convenience, and can be completely replaced.

eg:

public class D {
	int pp = 0;
	String s = "initial value";
	D(int p){
		pp = p;
		System.out.println("a w/ int oo,pp= " + pp);
	}
	
	D(String ss){
		System.out.println("a w/ String oo,s = " + ss);
		s = ss;
	}

	D(String s,int p){
		this(p);
		//this(s);   //在构造发法中,使用this调用构造方法的语句必须放在第一行,且只能出现一次,
		this.s = s;
		System.out.println("Strign & int");
	}
	
	D(){
		this("hi",47);
		System.out.println("aaaaaaaa");
	}
	
	void Dd(){
		//this(11);   //只能在构造方法中使用this调用其它的的构造方法,不能再成员方法中使用。
		System.out.println("pp = " + pp +" s = "+s);
	}
	
	public static void main(String[] args) {
		D d = new D();     //执行构造方法
		d.Dd();
	}

}
output:
a w/ int oo,pp= 47
Strign & int
aaaaaaaa
pp = 47 s = hi

requires attention:

  • 1. This can only be used in the construction method to call other construction methods, not in member methods.
  • 2. In the construction method, the statement that uses this to call the construction method must be placed on the first line and can only appear once.
  • 3. You cannot use this to call each other in two construction methods of a class.

 

4. Initialization

a. Static data initialization:

eg:

/*
 * 初始化顺序:
 * 1.先初始化变量,按照顺序
 * 2.再处理构造器
 * 3.再初始化静态对象
 * 4.处理完静态对象后再处理非静态对象
 * 通俗讲就是静态对象先执行。
 */

class Bowl{
	Bowl(int marker){
		System.out.println("Bowl("+ marker +")");
	}
	void f1(int marker){
		System.out.println("f1("+ marker +")");
	}
}

class Table{
	static Bowl bowl1 = new Bowl(1);
	Table(){
		System.out.println("Table()");
		bowl2.f1(1);
	}
	void f2(int marker){
		System.out.println("f2("+ marker +")");
	}
	static Bowl bowl2 = new Bowl(2);
}

public class StaticClass {
	public static void main(String[] args){
		System.out.println("aaaaaaaaaaaaaa");
	}
	static Table table = new Table();  //静态对象先执行。

}
output:
Bowl(1)
Bowl(2)
Table()
f1(1)
aaaaaaaaaaaaaa

 

eg2: Compared with the previous program, it is easier to understand

class Bowl{
	Bowl(int marker){
		System.out.println("Bowl("+ marker +")");
	}
	void f1(int marker){
		System.out.println("f1("+ marker +")");
	}
}

class Table{
	static Bowl bowl1 = new Bowl(1);
	Table(){
		System.out.println("Table()");
		bowl2.f1(1);
	}
	void f2(int marker){
		System.out.println("f2("+ marker +")");
	}
	static Bowl bowl2 = new Bowl(2);
}

class Cupboard{
	Bowl bowl3 = new Bowl(3);
	static Bowl bowl4 = new Bowl(4);
	Cupboard(){
		System.out.println("Cupboard()");
		bowl4.f1(2);
	}
	void f3(int marker){
		System.out.println("f3("+ marker +")");
	}
	static Bowl bowl5 = new Bowl(5);
}

public class StaticClass {
	public static void main(String[] args){
		System.out.println("aaaaaaaaaaaaaa");
		new Cupboard();
		System.out.println("aaaaaaaaaaaaaa");
		new Cupboard();
		table.f2(1);
		cupboard.f3(1);
	}
	static Table table = new Table();
	static Cupboard cupboard = new Cupboard();

}
output:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
aaaaaaaaaaaaaa
Bowl(3)
Cupboard()
f1(2)
aaaaaaaaaaaaaa
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)

b. Non-static instance initialization:

/*
 * 初始化顺序:
 * 1.先初始化变量,按照顺序
 * 2.再处理构造器
 * 3.再初始化静态对象
 * 4.处理完静态对象后再处理非静态对象
 * 通俗讲就是静态对象先执行。
 */

class Bowl{
	Bowl(int marker){
		System.out.println("Bowl("+ marker +")");
	}
	void f1(int marker){
		System.out.println("f1("+ marker +")");
	}
}

class Table{
	
	Table(){
		System.out.println("Table()");
		bowl2.f1(1);
	}
	void f2(int marker){
		System.out.println("f2("+ marker +")");
	}
	Bowl bowl2 = new Bowl(2);Bowl bowl1 = new Bowl(1);
}

public class StaticClass {
	public static void main(String[] args){
		System.out.println("aaaaaaaaaaaaaa");
		Table table = new Table(); 
		table.f2(2);
	}
}
output:
aaaaaaaaaaaaaa
Bowl(2)
Bowl(1)
Table()
f1(1)
f2(2)

5. Variable parameter list, basic type and basic type packaging class

a. Basic types and basic types of packaging:

  • Java's basic data types include 8 kinds of boolean, char, byte, short, int, float, long, and double.
  • The corresponding packaging classes are: Boolean, Character, Byte, Short, Integer, Float, Long, Double.

 

b. Variable parameter list:

Variable parameter lists can easily create objects and call methods, and are often used in situations where the number and types of parameters are unknown.

  • All classes indirectly or directly inherit from the Object class.

 

c. Variable parameter list and basic type packaging example:

eg:

public class bie {
	
	/*
	 * 所有的类都间接或直接继承于Object类。
	 * 定义了一个Object的可变参数列表数组
	 * 遍历打印每一个参数
	 */
	static void printarray(Object[] args){
		for(Object obj : args){
			System.out.println(obj + " ");
		}
	} 
	
	/*
	 * 定义了一个包装类型为Integer的的可变参数列表数组
	 * 遍历打印每一个参数
	 */
	static void inf(Integer...args){
		for(Integer i : args){
			System.out.println(i + " ");
		}
	}
	
	public static void main(String args[]){
		
		printarray(new Object[]{
				new Integer(23),   //可变参数为int型
				new Float(3.14)    //可变参数为float型
		});
		printarray(new Object[]{"one","two","three",new Integer(1)});
		
		inf(111,222,333);  //直接可以传入int型参数
	}

}
Output:
23 
3.14 
one 
two 
three 
1 
111 
222 
333 

In the above, Java initialization and cleaning up common problems, I hope that my article can help you learn.

I am original, please explain when reprinting, thank you!

Guess you like

Origin blog.csdn.net/weixin_40042248/article/details/81914650
Recommended