Java重载和重写的区别--源码实例

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


}


}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章