转:java中覆盖、重写与重载的区别

来源:互联网 发布:农村淘宝合伙人怎么样 编辑:程序博客网 时间:2024/05/20 00:11

初次见到这两个单词并没有什么特别的感觉,但是时间长了,却发现书上一会儿用override,一会儿又用overload,搞得我的迷迷糊。于是就做了个总结,希望能对和我一样对这两个概念模糊不清的网友有一个帮助。

override为覆盖又叫重写,从字面就可以知道,它是覆盖了一个方法并且对其重写,以求达到不同的作用。对我们来说最熟悉的覆盖就是对接口方法的实现,在接口中一般只是对方法进行了声明,而我们在实现时,就需要实现接口声明的所有方法。除了这个典型的用法以外,我们在继承中也可能会在子类覆盖父类中的方法。在覆盖要注意以下的几点:

1、重写方法的参数列表必须完全与被重写的方法的相同,否则不能称其为重写而是重载.

2、重写方法的访问修饰符一定要大于被重写方法的访问修饰符(public>protected>default>private)。

3、重写的方法的返回值必须和被重写的方法的返回一致;

4、重写的方法所抛出的异常必须和被重写方法的所抛出的异常一致,或者是其子类;

5、被重写的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行重写。

6、静态方法不能被重写为非静态的方法(会编译出错)。

overload对我们来说可能比较熟悉,可以翻译为重载,它是指我们可以定义一些名称相同的方法,通过定义不同的输入参数来区分这些方法,然后再调用时,vm就会根据不同的参数样式,来选择合适的方法执行。在使用重载要注意以下的几点:

1、在使用重载时只能通过不同的参数样式。例如,不同的参数类型,不同的参数个数,不同的参数顺序(当然,同一方法内的几个参数类型必须不一样,例如可以是fun(int, float), 但是不能为fun(int, int));

2、不能通过访问权限、返回类型、抛出的异常进行重载;

3、方法的异常类型和数目不会对重载造成影响;

下面是对override和overload的测试程序,其中注释中的内容都是会产生编译错误的代码,我们将注释去掉,看看在编译时会产生什么效果。

// 对overload测试的文件:overloadtest.java

public class overloadtest {

// 下面几个方法用来验证可以通过定义不同的参数类型和参数的数目进行方法重载。

public void fun(){

system.out.println("method fun in overloadtest, no parameter");

}

public void fun(float f) {

system.out.println("method fun in overloadtest, parameter type: float");

}

public void fun(int i){

system.out.println("method fun in overloadtest, parameter type: int");

}

public void fun(int i1, int i2) {

system.out.println("method fun in overloadtest, parameter type: int, int");

}

// 下面的两个方法用来验证可以通过定义不同的参数顺序进行方法重载。

// 需要注意:这里的参数肯定不是相同的类型,否则的顺序的先后就毫无意义。

public void fun1(int i, float f) {

system.out.println("method fun1 in overloadtest, sequence of parameters is: int, float");

}

public void fun1(float f, int i) {

system.out.println("method fun1 in overloadtest, sequence of parameters is: float, int");

}

// 下面的两个方法用来验证方法抛出的异常对于重载的影响.

// 无论是异常的类型还是异常的个数都不会对重载造成任何的影响。

public void fun2() throws testexception {

system.out.println("fun2 in overloadtest, exception: testexception");

}

public void fun2(int i) throws testexception, testexception1 {

system.out.println("fun2 in overloadtest, exception: testexception, testexception1");

}

public void fun2(float f) throws exception {

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);

}

}


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
原创粉丝点击