你在a类重载b类的doit() 函数,
不做任何动作就可以无法调用doit()方法,
但是要控制不被暴露,我是觉得不行的啦

解决方案 »

  1.   

    你在a类重载b类的doit() 函数然后抛出一个异常,
      

  2.   

    在a里边重载doit(),这样实例化的时候就不会调用到b的doit()方法了
      

  3.   

    继承该b类后,直接覆盖掉该方法,然后将属性设置为private,这样除了类本身,任何地方都不能使用了。不过,如果是在类里面定义 A a = new A()的话,还是可以调用a.doit()方法的。
      

  4.   

    继承该b类后,直接覆盖掉该方法,然后将属性设置为private,这样除了类本身,任何地方都不能使用了。不过,如果是在类里面定义 A a = new A()的话,还是可以调用a.doit()方法的。
      

  5.   

    不知所云
    出题的人不知在说些什么?
    答题的人全在瞎猜,答案是千奇百怪
    如此,csdn没什么意义了
      

  6.   

    这个问题本身就不是很容易说清楚的。
    to javapro:我试过直接重载为private,但编译器会报错,说不能重载。
    to haode:这样也不行,因为我希望a里边可以调用或者说使用b的doit方法的功能,而且a不再为其使用者和继承者暴露doit方法。不知道这样说能不能理解。
    to udoo:其实,我这个问题的题目就很容易理解,就是将一个public方法在a类中终结,使之对a的继承者或者使用者都看不见这个方法,那也就是说要将这个public方法变为private方法。呵呵,仔细理解一下嘛,理解万岁,csdn万岁!
      

  7.   

    outer2000(天外流星) 的想法真是让我佩服,佩服!
      

  8.   

    to furarmy (爪哇猫) :你是觉得这样能为自己做个后门吗?
      

  9.   

    这在c++里面倒是很容易实现,不过在java里面到真是没有仔细考虑过,再想想。up
      

  10.   

    抛exception的方法好像是唯一想得到的方法,但不太符合楼主的要求。楼主要编译错。
      

  11.   

    在A:
    public final void doit()
    B将无法继承A的doit方法
      

  12.   

    啊哟, 说错了, 见谅
    楼主的想法实在是太强了, sorry
      

  13.   

    I see now,But it is impossible to do that.
    Overriding methods can have the same or more access privileges of the overriden methods.Superclass | Subclass
    1)private | private->protected->public
    2)Friendly | friendly->protected->public but NOT private
    (no modifier)
    2)protected | protected->public but NOT private
    3)public | public but NOT private or protectedThe reason.
    Suppose class A extends class B;then A is also a kind of B.class  A extends B{
        private f();
    }
    class B {
        public f();
    }then if in a methodB b=getInstanceOfB90;
    //It can return subclass of B,A,because A is  a kind of B
    b.f();
    //This is definitely right,but if the method f() of A have less
    //access privileges than the overriden f() of B,the compiler will
    //complain.
    If you are not know it completely,you can send me email
     [email protected]
      

  14.   

    这样就不能使用继承,因为B与A的接口不兼容.
    可以在B中包含并维护一个A的实例.然后,在B中对A的方法派发.如:
    class B{
       ...
       A a = new A();
       ...
       public aMethod(){
          a.aMethod();
       }
       ...
    }
      

  15.   

    但是在某种特殊的环境下,可能A必须继承于B。如,做一个swing javabean,完成一个特定对象的容器功能,可能我只能让A提供addArea方法,而不能将JComponent(B)的总多add方法暴露。A的使用者只知道使用addArea就能加入一个对象,甚至不必知道对象是什么。而如果暴露了add方法,可能就混乱了。
      

  16.   

    typical misuse of "extends".
    Even though you can declare doit as private and disallow
    new B().doit();
    what do you do for 
    A a = new B();
    a.doit();?you should NOT use inheritance. Polarislee(北极星) is right.