Make a note of what you learned about Java overloading, overriding, and inheritance.
Overloading is ** writing multiple methods and constructors with the same name with different argument types and numbers. ** **
The same method name, but the return value may change depending on the number of arguments given. This defines a plus method that does the addition.
keisan.Java
//Sum method to add
public class sum {
public int sum(int a) {
return a;
}
public int sum(int a, int b) {
return a + b;
}
public int sum(int a, int b, int c) {
return a + b + c;
}
}
Inheritance is the inheritance of any class by another class.
This time, Keisan.Java is inherited by Task.java, and arguments are given to perform the calculation.
Task.java
public class Task extends Keisan {
public void taskJikkou() {
System.out.println("【Execution result】");
System.out.println("a is"Sum(1)); //result:a is1
System.out.println("a +b is"Sum(5, 10)); //result: a +b is 15
System.out.println("a + b +c is"Sum(100, 150, 200)); //result: a + b +c is 450
}
}
Create an instance in Main.java and execute it.
Main.java
public class Main {
public static void main(String[] args) {
Task task = new Task(); //Create an instance
task.taskJikkou(); //Task.Call the java taskJikkou method.
}
}
【Execution result】
a is 1
a +b is 15
a + b +c is 450
This time I was doing calculations, but it seems that I can express various things by using these. I'm excited that there are many uses, such as "driving" a car and "kodo" of an RPG.
Recommended Posts