-[Latest] How to build Java environment on Ubuntu
-[Even beginners can do it! ] How to create a Java environment on Windows 10 (JDK14.0.1)
-[Easy-to-understand explanation! ] How to use Java instance
-[Even beginners can do it! ] How to install Eclipse on Windows 10 (Java environment construction)
As prior knowledge, the contents of the above link are required.
--Overload
means defining a method with the same method name but with number of different arguments
or different argument types
.
--If there are multiple constructors with the same number of arguments
, theconstructors with the same argument type are called. --You can also call another constructor by using
this ()from within the constructor. --The call to the constructor by
this () must be written at the
beginning `of the constructor.
Test class
package package name;
public class main class name{
public static void main(String[] args) {
//Instance generation
Class name Variable name 1=new class name();
Class name Variable name 2=new class name(Actual argument);
Class name Variable name 3=new class name(Actual argument of a type different from variable name 2);
}
}
Overlord class
package package name;
class class name{
//Definition of instance variables
private type name variable name;
//Constructor 1 (no arguments)
name of the class(){
Initialization process, etc.
}
//Constructor 2 (with arguments)
name of the class(Type name Argument name){
Initialization process, etc.
}
//Constructor 3 (with different arguments than constructor 2)
name of the class(Type name Argument name){
Initialization process, etc.
}
}
--Basic overload is described as above.
[File (F)]-> [New (N)]-> [Java Project]
.
Test1
for the project name and click the Done
button.
[File (F)] → [New (N)] → [Class]
.
Test1
for the package and name and click the Done
button.
Test1.java
has been created.
Enter Test1
in the package, enter TestOverload
in the name, and click the Finish
button in the same way as in 6.3.
Test1.java
and TestOverload.java
are created.--The advantage of using overload
is that you don't have to remember multiple methods with different names
.
--For example, it can be used when there are people who have confirmed attendance
andpeople who have not decided in advance, such as
confirmation of attendance`. (It can be completed only with the constructor without creating three methods of attendance, absence, and undecided.)
Test1.java
//Attendance confirmation system
package Test1;
public class Test1 {
public static void main(String[] args) {
//Instance generation
TestOverload to1 = new TestOverload("A");
TestOverload to2 = new TestOverload("B",1);
TestOverload to3 = new TestOverload("C",2);
System.out.println("-----April 1st-----");
//View the contents of the instance
to1.showAttendance();
to2.showAttendance();
to3.showAttendance();
}
}
TestOverload.java
package Test1;
public class TestOverload {
//Definition of instance variables
private String name;
private int attend;
//Constructor 1 (no arguments)
TestOverload(){
this("Not entered",0);
}
//Constructor 2 (with arguments)
TestOverload(String name){
this(name,0);
}
//Constructor 3 (with different arguments than constructor 2)
TestOverload(int attend){
this("Not entered",attend);
}
//Constructor 4 (2 arguments)
TestOverload(String name,int attend){
this.name = name;
this.attend = attend;
}
//Display method
void showAttendance(){
String str = "";
switch(attend) {
case 0:
str += "Undecided";
break;
case 1:
str += "Attendance";
break;
case 2:
str += "Absence";
break;
}
System.out.println(name+"Is"+str);
}
}
--Copy the above sentence, specify S-JIS
as the character code, save the file name as Test1.java
, TestOverload.java
, and execute it. ↓ ↓
-[Useful to remember !!!] Easy creation of constructor and getter / setter in Eclipse -[Even beginners can do it! ] How to write Javadoc -[Easy-to-understand explanation! ] How to use Java encapsulation
Recommended Posts