Java - 重载和覆盖

时间:2011-07-21 09:48:30

标签: java overloading override

01    class SubClass extends SuperClass {}
02    class AppSuperClass {
03        /**
04         * @param superClass
05         */
06        public void print(SuperClass superClass) {
07            System.out.println("AppSuperClass:superclass is parameter");
08     
09        }
10        /**
11         * @param subClass
12         */
13        public void print(SubClass subClass) {
14            System.out.println("AppSuperClass:subclass is parameter");
15   
16        }
17    }
18   
19    class AppSubClass extends AppSuperClass {
20      /**
21       * @param superClass
22       */
23      public void print(SuperClass superClass) {
24          System.out.println("AppSubClass:superclass is parameter");
25   
26      }
27      /**
28       * @param subClass
29       */
30      public void print(SubClass subClass) {
31          System.out.println("AppSubClass:subclass is parameter");
32   
33      }
34  }
35  public class OverloadedTest {
36      public static void main(String[] args) {
37          AppSuperClass appSuperClass = new AppSuperClass();
38          AppSuperClass appSubClass = new AppSubClass();
39          SuperClass superClass = new SuperClass();
40          SuperClass subClassInstance = new SubClass();
41          /*
42           * Making request to print AppSuperClass
43           *  1. Passing SuperClass instance
44           *  2. Passing SubClass instance (*make note of the type) <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1304052800g" alt=":)" class="wp-smiley">
45           */
46   
47          appSuperClass.print(superClass);
48          appSuperClass.print(subClassInstance);
49          /*
50           * Above is repeated with AppSubClass instance
51           */
52          appSubClass.print(superClass);
53          appSubClass.print(subClassInstance);
54      }
55   
56  }

当我跑步时,我得到了

       AppSuperClass:superclass is parameter
       AppSuperClass:superclass is parameter
       AppSubClass:superclass is parameter
       AppSubClass:superclass is parameter

我怎样才能获得

           AppSuperClass:superclass is parameter
           AppSuperClass:subclass is parameter
           AppSubClass:superclass is parameter
           AppSubClass:subclass is parameter

作为o / p而不改变任何对象的类型?

3 个答案:

答案 0 :(得分:2)

你的问题不是很清楚,但听起来你基本上是在执行时间重载之后,这在Java中根本就不存在。重载在编译时完全解决。一种选择是:

public void print(SuperClass superClass) {
    if (superClass instanceof SubClass) {]
        print((SubClass) superClass);
        return;
    }
    System.out.println("AppSuperClass:superclass is parameter");
}

请注意,您还需要在覆盖中执行此操作,或者使用执行此操作的模板方法,以及可以在子类中重写的单独的printImpl(SuperClass)方法。

要将此置于逻辑极端,您可能会:

class AppSuperClass {

    public final void print(SuperClass superClass) {
        if (superClass is SubClass) {
            printImpl((SubClass) superClass);
        } else {
            printImpl(superClass);            
        }
    }

    protected void printImpl(SuperClass superClass) {
        ...
    }

    protected void printImpl(SubClass subClass) {
       ...
    }
}

AppSubClass只会覆盖printImpl(一次或两次重载)。

编辑:如评论中所述,另一种方法是使用Visitor pattern。这不是我非常喜欢的模式,但 if 你可以修改SuperClassSubClass以了解AppSuperClass(或它实现的接口)它可以为你工作。

答案 1 :(得分:0)

您将参数传递为SuperClass。从开始使用真正的类型,或者在调用方法时使用强制类型。

SubClass subClassInstance = ....

或者在方法中使用实例类型检查。

答案 2 :(得分:0)

问题是编译器只知道参数至少是SuperClass类型,并且不知道您分配给变量的对象的实际类型。因此,它始终使用采用SuperClass参数的方法。

您可以在方法中检查参数的类,但是因为您想要使用它而超载,我不敢这样做。