Javaä¸è¦çåéè½½çåºå«å¦ä¸ï¼
override å¯ä»¥ç¿»è¯ä¸ºè¦çï¼ä»åé¢å°±å¯ä»¥ç¥éï¼å®æ¯è¦çäºä¸ä¸ªæ¹æ³å¹¶ä¸å¯¹å
¶éåï¼ä»¥æ±è¾¾å°ä¸åçä½ç¨ãæçæçè¦çå°±æ¯å¯¹æ¥å£æ¹æ³çå®ç°ï¼å¨æ¥å£ä¸ä¸è¬åªæ¯å¯¹æ¹æ³è¿è¡äºå£°æï¼èå¨å®ç°æ¶ï¼å°±éè¦å®ç°æ¥å£å£°æçæææ¹æ³ãé¤äºè¿ä¸ªå
¸åçç¨æ³ä»¥å¤ï¼å¨ç»§æ¿ä¸ä¹å¯è½ä¼å¨åç±»è¦çç¶ç±»ä¸çæ¹æ³ãå¨è¦çè¦æ³¨æ以ä¸çå ç¹ï¼
1ãè¦ççæ¹æ³çæ å¿å¿
é¡»è¦å被è¦ççæ¹æ³çæ å¿å®å
¨å¹é
ï¼æè½è¾¾å°è¦ççææï¼
2ãè¦ççæ¹æ³çè¿åå¼å¿
é¡»å被è¦ççæ¹æ³çè¿åä¸è´ï¼
3ãè¦ççæ¹æ³ææåºçå¼å¸¸å¿
é¡»å被è¦çæ¹æ³çææåºçå¼å¸¸ä¸è´ï¼æè
æ¯å
¶åç±»ï¼
4ã被è¦ççæ¹æ³ä¸è½ä¸ºprivateï¼å¦åå¨å
¶åç±»ä¸åªæ¯æ°å®ä¹äºä¸ä¸ªæ¹æ³ï¼å¹¶æ²¡æ对å
¶è¿è¡è¦çã
overload对æ¥è¯´å¯è½æ¯è¾çæï¼å¯ä»¥ç¿»è¯ä¸ºéè½½ï¼å®æ¯æå¯ä»¥å®ä¹ä¸äºå称ç¸åçæ¹æ³ï¼éè¿å®ä¹ä¸åçè¾å
¥åæ°æ¥åºåè¿äºæ¹æ³ï¼ç¶ååè°ç¨æ¶ï¼VMå°±ä¼æ ¹æ®ä¸åçåæ°æ ·å¼ï¼æ¥éæ©åéçæ¹æ³æ§è¡ãå¨ä½¿ç¨éè½½è¦æ³¨æ以ä¸çå ç¹ï¼
1ãå¨ä½¿ç¨éè½½æ¶åªè½éè¿ä¸åçåæ°æ ·å¼ãä¾å¦ï¼ä¸åçåæ°ç±»åï¼ä¸åçåæ°ä¸ªæ°ï¼ä¸åçåæ°é¡ºåºï¼å½ç¶ï¼åä¸æ¹æ³å
çå 个åæ°ç±»åå¿
é¡»ä¸ä¸æ ·ï¼ä¾å¦å¯ä»¥æ¯fun(int, float)ï¼ ä½æ¯ä¸è½ä¸ºfun(int, int)ï¼ï¼
2ãä¸è½éè¿è®¿é®æéãè¿åç±»åãæåºçå¼å¸¸è¿è¡éè½½ï¼
3ãæ¹æ³çå¼å¸¸ç±»ååæ°ç®ä¸ä¼å¯¹éè½½é æå½±åï¼
4ã对äºç»§æ¿æ¥è¯´ï¼å¦ææä¸æ¹æ³å¨ç¶ç±»ä¸æ¯è®¿é®æéæ¯priavteï¼é£ä¹å°±ä¸è½å¨å类对å
¶è¿è¡éè½½ï¼å¦æå®ä¹çè¯ï¼ä¹åªæ¯å®ä¹äºä¸ä¸ªæ°æ¹æ³ï¼èä¸ä¼è¾¾å°éè½½çææã
ä¸é¢æ¯å¯¹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);
}
}
温馨提示:内容为网友见解,仅供参考