在近段时间的自主学习中我遇到了一些十分容易忽视的问题,虽然这些问题并不会太影响我们的学习进程,但是为了以后更好的发展,早日把它解决也是不错的选择(我会在这几天把问题一次性解决完)。
对于“重载”简单的定义:
- 就重载而言:对一个方法进行各种类型的扩展(减少此方法不适用的类型);因为重载就是对于一个方法的返回值和形参的类型进行变更,其中的内容和方法名都无需修改(同学们,一定在包,文件的命名方面勤快一点,明明是前不久练习时候写的代码现在全是demo_0x 完全找不到):
package Overriding;
public class test {
public static class Overloading {
public int test() {
System.out.println("test1");
return 1;
}
public void test(int number) {
System.out.println("test2");
}
public String test(int number, String string) {
System.out.println("test3");
return "returntest3";
}
public static void main(String[] args) {
Overloading overloading = new Overloading();
System.out.println(overloading.test());
System.out.println("------------");
int number = 1;
overloading.test(number);
System.out.println("------------");
System.out.println(overloading.test(1, "test3"));
System.out.println("------------");
}
}
}
结果:
test1
1
------------
test2
------------
test3
returntest3
------------
以上就是通俗易懂的例子,如果有什么不懂的随时可以给我留言OVO。
最后附上Google给的专业定义:Method Overriding in Java. If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.