假設我有兩個類,A和B.A類被定義爲抽象類,而B擴展了這個抽象類,最後我測試了結果,兩個類都是同一個包的一部分。瞭解Java中抽象類的用途
public abstract class A {
protected abstract void method1();
protected void method2() {
System.out.println("This is Class A's method");
}
}
public class B extends A {
@Override
protected void method1() {
System.out.println("This is B's implementaiton of A's method");
}
}
,現在當我測試他們:
B b = new B();
b.method1();
b.method2();
我得到預期輸出:
This is B's implementaiton of A's method
This is Class A's method
問題:
- 什麼是
@Override
k的目的eyword,因爲如果我省略它, 仍然工作相同。 - 如果我沒有實現抽象方法,我得到一個編譯錯誤。那麼與實現接口有什麼不同?
- 此外,我也可以在B中實現
method2()
。然後輸出變成B中的用法。這是不是也覆蓋了父類的方法?那麼在A類中明確定義方法的目的是什麼?摘要?
有時是谷歌比要求或回答這裏更快:http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – vulkanino 2012-02-08 16:37:19
谷歌被阻止在我們的學校 – 911TurboS 2012-02-08 16:42:12
你的互聯網如何存在沒有沒有谷歌? – jondavidjohn 2012-02-08 16:50:13