- abstract class A {
- public abstract void doSomething();
- }
- class B extends A {
- public void doSomething() {
- System.out.println("do B");
- }
- }
- class C extends A {
- public void doSomething() {
- System.out.println("do C");
- }
- }
- public class Test {
- public static void main(Stringp[] args) {
- A a1 = new B();
- A a2 = new C();
- B b = new B();
- C c = new C();
- doSomething(a1); //这里
- doSomething(a2); //这里
- doSomething(b); //这里,如果下面的方法不是用抽象类作参数,比如用C c做参数,这里会有什么问题?
- doSomething(c); //这里
- }
- public static void doSometing(A a) {//这里,用抽象类
- a.doSomething();
- }
- }
复制代码 |