iT邦幫忙

2022 iThome 鐵人賽

DAY 18
0

介紹

The relationship between classes and objects was shared earlier. When an object needs to be set with initial attributes, constructors can be used.

If the object itself in the class does not have a constructor, it will automatically generate a basic one (which will not appear in the code and do nothing). Usually, we use the constructor to set the initial content of the object, use it with the keyword “new”, and the constructor name must be the same as the class name:
https://ithelp.ithome.com.tw/upload/images/20221003/20151216XVfNRsn1P1.png
Like the settings we introduced earlier for objects, the last keyword is the constructor used to set the initial value, which we said was the class name. This can be further subdivided into two types, depending on whether or not there are any input arguments. In summary, there are three main types and four characteristics:

  • default constructor
  • self-defined empty constructor
  • self-defined non-empty constructor
  1. The name must be the same as the class name
  2. No return value is generated
  3. Parameters can be passed in
  4. Multiple constructors can be generated, but the parameter types of the functions must be different (multi-loading concept)

The following is a separate introduction to the concepts of constructors with and without parameter passing.


Set Constructors

  • Empty constructor
    Whether it is the Java default or a self-defined empty one (without parameters), there is the concept of a constructor in a program.
    The difference is that one is not displayed and is completely useless, while the other requires self-configuration and does not take any parameters. There is a subclass that inherits the attributes of the parent class.
/*Default*/
public class Main{
	String name;
	int grade;
    char gpa;

	/*public Main(){       //系統預設
    
	}*/
}

/*Constructor(NULL)*/
public class Main{
	String name;
	int grade;
    char gpa;

	public Main(){       //自行設定
	
	}
}

The definition of the constructor will be:

(Modifier) class_name((argument)){
	//Command
} 

  • Constructor (no arguments):
public class Main{
	String name;
	int grade;
	char gpa;

	public Main(){
		name = "CHI";
		grade = 82;
		gpa = 'A';
	}

	void printout(){
		System.out.println("Your personal information ");
	}

	public static void main(String[] args){
		Main newobj = new Main();
		newobj.printout();
		System.out.println("Student name : " + newobj.name);
		System.out.println("Final test : " + newobj.grade);
    System.out.println("GPA : " + newobj.gpa);
	}
}
  • Program analysis: The main function creates a new object newobj, and the content of the last constructor is defined in the middle part. You can find that when the content is printed, the initial values set by the constructor are printed.

Constructors deliever Parameters

Next, we will change the above program to use the function input method and introduce the new keyword this.

public class Main{
	String name;
	int grade;
	char gpa;

	public Main(String name, int grade, char gpa){
		this.name = name;
		this.grade = grade;
		this.gpa = gpa;
	}

	void printout(){
		System.out.println("Your personal information");
	}

	public static void main(String[] args){
		Main newobj = new Main("Chi", 82, A);
		newobj.printout();
		System.out.println("Student name : " + newobj.name);
		System.out.println("Final test : " + newobj.grade);
    System.out.println("GPA : " + newobj.gpa);
	}
}

Output:
https://ithelp.ithome.com.tw/upload/images/20221003/201512168ifWBERVBJ.png

  • Program analysis: this refers to the nearest object, so when we use it, it refers to the name of the object created based on the constructor, and inherits the above-mentioned name results and other elements to this object.

上一篇
Day 17 : The Relationship of Object and Class
下一篇
Day 19 : Modifier
系列文
30天Java由淺入深30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言