I don't refer to class slides, so I think it's explained from a different perspective. Maybe I'm saying it (should).
Please let me know if you may make a mistake because it is an ad lib!
Part.1 Try to instantiate it Part.2 Cleaner with constructor ← Imakoko Part.3 Let's control the access and limit the incoming value Part.4 Try using class variables / methods
In Part.1, I wrote a digital student ID card as a class and made Tanaka-kun and Noguchi-kun's student ID cards as instances. I will reuse the code this time as well.
DigitalStudentCard.java
class DigitalStudentCard{
//Student number
int num;
//name
String name;
//Method for output (introduce yourself)
String getSelfIntroduction(){
return "Student number: " + num + "name: " + name);
}
}
class UseClass{
public static void main(String[] args){
//Instance generation
DigitalStudentCard tanaka = new DigitalStudentCard();
//Assign to instance variable
tanaka.num = 118114;
tanaka.name = "Kakuei Tanaka";
//Create another instance
DigitalStudentCard noguchi = new DigitalStudentCard();
//Assign to the variable of the newly created instance
noguchi.num = 118514;
noguchi.name = "Hideyo Noguchi";
//output
System.out.println(tanaka.getSelfIntroduction);
//Student number:118114 Name:Kakuei Tanaka
System.out.println(noguchi.getSelfIntroduction);
//Student number:118514 Name:Hideyo Noguchi
}
}
This time, let's use a constructor </ b> to write these code smarter.
A constructor is a method that is called only once when an instance is created </ b>.
It's like the void setup ()
function of Processing was a function that is executed only once when the application starts.
It is used when you want to insert a value together when creating an instance.
Try adding a constructor to DigitalStudentCard
.
It's annoying to put it in later, so you can set num
and name
when instantiating
The constructor can be implemented by simply writing the class name as it is </ b>. If you want to receive arguments, you can write formal arguments just like a method.
DigitalStudentCard.java
class DigitalStudentCard{
//Student number
int num;
//name
String name;
//constructor
DigitalStudentCard(int num, String name){
//Argument num to instance num(this.num)Substitute to
this.num = num;
//Also
this.name = name;
}
//Method for output (introduce yourself)
String getSelfIntroduction(){
return "Student number: " + num + "name: " + name);
}
}
When using this, give an argument at new DigitalStudentCard ()
.
class UseClass{
public static void main(String[] args){
//Instance generation
DigitalStudentLicense tanaka = new DigitalStudentCard(118114, "Kakuei Tanaka");
//Create another instance
DigitalStudentLicense noguchi = new DigitalStudentCard(118514, "Hideyo Noguchi");
//output
System.out.println(tanaka.getSelfIntroduction);
//Student number:118114 Name:Kakuei Tanaka
System.out.println(noguchi.getSelfIntroduction);
//Student number:118514 Name:Hideyo Noguchi
}
}
By using the constructor, I was able to shorten the code even further.
Since the constructor can be overloaded like the method, you can write multiple constructors to branch the process.
DigitalStudentCard.java
class DigitalStudentCard{
//Student number
int num;
//name
String name;
//constructor
DigitalStudentCard(int num, String name){
//Argument num to instance num(this.num)Substitute to
this.num = num;
//Also
this.name = name;
}
DigitalStudentCard(){
//Part.Make it possible to use it in the same way as 1.
}
//Method for output (introduce yourself)
String getSelfIntroduction(){
return "Student number: " + num + "name: " + name);
}
}
Next, let's do access modifiers! Next → Part.3 Control access to limit incoming values