您的位置:首页 > 其它

方法重载与方法重写的概念和区别(转载)

2017-05-25 17:50 183 查看
方法重载:一个类中有一个方法A,你又在这个类中创建了一个方法B,方法B的名字和A一样,返回值类型也一样,但是参数的类型或个数不同,此时B重载了A。
例如:


public class TestClass{
public int test(int i){return 1;}
public int test(float f){return 1;}
}


  

方法重写:一个类M继承另一个类N,N中有一个方法A,这时你在M写了一个方法B,方法B的名字、返回值以及参数都和A一样,此时B重写了A。 例如:


public class TestClass1
{
public int test(int i)
{return 1;} }
public class TestClass2 extends TestClass1
{ public int test(int i){return 2;} }


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: