全部展开
Java中的覆盖和重载之间的区别如下:
override可以翻译62616964757 a 686964616 Fe 78988 e69 d 8331336356562作为覆盖面,从字面上可以看出它可以覆盖一个方法,然后重写它,达到不同的作用最广为人知的覆盖是接口方法的实现,接口中一般只是声明方法,实现时需要实现接口声明的所有方法。 除了这种典型用法外,继承还可能在子类中覆盖父类的方法。 在封面上请注意以下事项。
1、覆盖方法的标识必须与覆盖方法的标识完全一致才能达到覆盖效果
2、所覆盖方法的返回值必须与所覆盖方法的返回值一致
3、所覆盖方法的抛出异常必须与所覆盖方法的抛出异常匹配或为其子类
4、涵盖的方法不能是隐私。 否则,它的子类只定义了新方法,而没有被复盖。
overload可能被翻译为重载。 这意味着,如果通过定义几个同名的方法并定义不同的输入参数来区分这些方法,然后调用它们,则VM将根据不同的参数样式选择并执行相应的方法。 使用重载时,请注意以下事项:
1 .使用重载时,只能在不同的参数样式中使用。 例如,不同的参数类型、不同的参数数量、不同的参数顺序(当然,同一方法中的某些参数类型必须不同)。 例如,它可以是fun(int,float ),但不能是fun(int,int )。 );
2、访问权限、返回类型、抛出的异常无法重载
3、方法异常类型和数量不影响重载;
4、继承时,如果某个方法为父类,权限为priavte,则无法在子类中重载。 定义后,只会定义新方法,而不会产生重载的效果。
以下是override和overload的测试程序。 注释的内容是发生编译错误的代码。 请删除注释,看看编译时会产生什么效果。
overload测试的文件: OverloadTest.java
公共类开销测试{
//以下几种方法用于验证可以通过定义不同的参数类型和参数数量来重载方法:
公共语音fun () {
system.out.println (methodfuninoverloadtest,no parameter );
}
公共语音Un (浮动) {
system.out.println (methodfuninoverloadtest,parameter type: float );
}
公共语音Un (Inti ) {
系统. out.println (methodfuninoverloadtest,parameter type: int );
}
公共void fun (inti 1,int i2 ) {
system.out.println (methodfuninoverloadtest,parameter type: int,int );
}
//以下两种方法用于验证可以通过定义不同的参数顺序来重载方法:
//需要注意。 这里的参数一定不是同一类型。 否则,顺序的优先顺序就没有意义了。
公共语音Fun1(Inti,float f ) {
system.out.println (方法fun1inoverloadtest,sequence of parameters is: int,float ' );
}
公共void fun1(浮动,int i ) {
system.out.println (method fun1inoverloadtest,sequenceofparametersis : float,int ' );
}
//以下两种方法用于验证方法抛出的异常对重载的影响。
//异常的类型和异常的个数对重载都没有任何影响。
公共语音验证2 (throws testexception )。
system.out.println (fun2inoverloadtest,exception: TestException );
}
公共void fun2(inti ) throws TestException,TestException1 {
system.out.println (fun2inoverloadtest,exception: TestException,TestException1' );
}
公共void fun2(浮动) throws Exceptio
n {System.out.println("fun2 in OverloadTest, exception: Exception");
}
// 不能通过抛出的异常类型来重载fun方法。
//public void fun(int i) throws Exception {
// System.out.println("method fun in OverloadTest, parameter type: int, exception: Exception");
//}
// 不能通过返回值重载fun方法。
//public boolean fun(int i) throws Exception {
// System.out.println("method fun in OverloadTest, parameter type: int, exception: Exception, return: boolean");
// return true;
//}
private void fun3() { }
// 不能通过不同的访问权限进行重载
public void fun3() { }
public static void main(String[] args) {
// 这里只是定义了OverloadTest的实例,所以test不会调用
// OverloadTest1中的方法。
OverloadTest test = new OverloadTest1();
// 这里定义了OverloadTest1的实例,因为OverloadTest1是OverloadTest
// 的子类,所以test1会调用OverloadTest中的方法。
OverloadTest1 test1 = new OverloadTest1();
try {
int i = 1, j = 2, m = 3;
// 这里不会调用OverloadTest1的fun方法
// test.fun(i, m, j);
test1.fun(i, j, m);
test1.fun();
// 这个调用不会执行,因为fun3()在OverloadTest中访问权限是priavte
//test1.fun3();
test1.fun3(i);
} catch(Exception e) { }
}
}
class OverloadTest1 extends OverloadTest{
// 在子类中重载fun
public void fun(int i, int m, int n) {
System.out.println("Overload fun1 in OverloadTest1, parameter type: int, int, int");
}
// 这个不是对父类中方法的重载,只是一个新的方法。
public void fun3(int i) {
System.out.println("fun2 in OverloadTest1");
}
}
// 对override测试的文件:OverrideTest.java
public class OverrideTest {
public void fun() throws TestException {
System.out.println("method fun in OverrideTest");
}
private void fun1() {
System.out.println("method fun1 in OverrideTest");
}
public static void main(String[] args) {
OverrideTest test = new OverrideTest1();
try {
test.fun();
test.fun1();
} catch(Exception e) { }
}
}
class OverrideTest1 extends OverrideTest{
// 以下正常Override
public void fun() throws TestException2 {
System.out.println("fun in OverrideTest1");
}
// 不能Override父类中的方法,因为它定义了不同的异常类型和
// 返回值。
//public int fun() throws TestException1 {
// System.out.println("method fun in Test");
// return 1;
//}
// 不能Override父类中的方法,因为它抛出了比父类中非法范围
// 更大的异常。
//public void fun() throws Exception {
// System.out.println("fun in OverrideTest1");
//}
// 这个方法并没有Override父类中的fun1方法,因为这个方法在
// 父类是private类型,所以这里只是相当于定义了一个新方法。
public void fun1() {
System.out.println("method fun1 in Test");
}
}
class TestException extends Exception{
public TestException(String msg) {
super(msg);
}
}
class TestException1 extends TestException {
public TestException1(String msg) {
super(msg);
}
}
class TestException2 extends TestException {
public TestException2(String msg) {
super(msg);
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起