假设我有两个类,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