找到你要的答案

Q:how to extend method of the super class with custom content

Q:如何用自定义内容扩展超类的方法

I am looking for the solution how extend method of the super class with custom content.

for example I have a super class

class A{

  public  A(){
    boolean ok = doSomeInitialWork();

    if(ok){
        specialMehtod();
    }
  }

  specialMehtod(){
     System.out.print("HEllO");
  }
}

so my class A does some "init" things and then calls specialMehtod(). Now I would like to have class B extends A and add some other code to specialMehtod(). Something like:

Class B extends A{

    B(){
        super();
    }

    specialMehtod(){
       System.out.print("BBBBB");      
    }
}

But if I do so, I call a.SpecialMethod() expizit, and actually I only want to "add" System.out.print("BBBBB"); to specialMethod(), so that it still get called from constructor of A.

What I want to achieve, is that finaly I could do in my main class

new B();

ant the output would be. HELLO BBBBB because B() calls constructor of A(), which calls A.specialMethod() extended by B.specialMethod();

Is it possible?

Thanks

我正在寻找解决方案如何扩展超类的方法与自定义内容。

例如我有一个超级班

class A{

  public  A(){
    boolean ok = doSomeInitialWork();

    if(ok){
        specialMehtod();
    }
  }

  specialMehtod(){
     System.out.print("HEllO");
  }
}

所以我班做了“初始化”的东西,然后调用specialmehtod()。现在我想有B类扩展,添加一些其他的代码specialmehtod()。像:

Class B extends A{

    B(){
        super();
    }

    specialMehtod(){
       System.out.print("BBBBB");      
    }
}

但如果我这样做,我叫A specialmethod() expizit,其实我只是想“添加”系统。了。打印(“bbbbb”);对specialmethod(),所以它仍然可以从构造函数的调用

我想达到的目标,是最后我能做我的主类

新的b();

ant the output would be. HELLO BBBBB because B() calls constructor of A(), which calls A.specialMethod() extended by B.specialMethod();

这可能吗?

谢谢

answer1: 回答1:

You would have to so something like this:

public abstract class A{
    public abstract void anotherMethod();

    public A(){
        // some stuff..
        specialMethod();
    }

    private final void specialMethod(){
        System.out.println("specialMethod");
        anotherMethod();
    }
}

public class B extends A{
     public B(){
         super();
     }

     public void anotherMethod(){
         System.out.println("anotherMethod");
     }
}

This way you force your extending class to implement the method anotherMethod() and this method is called from your abstract class A. If your extending class does not need to do anything in anotherMethod(), you can just let the method body empty.


As mentioned in the comments of your question, you have to either inherit or override the method. When overriding a method, you have to call the super-method super.someMethod(); explicitly or when inheriting, you can not add additional functionality to the given method.
So to come with your example, if you don't implement specialMethod() in B, it will print out "HELLO", since A has an implementation for the method, but B does not. If you override the method like you are doing in the example in class B:

specialMethod(){
    System.out.println("BBBBB");
}

It will have the effect that "HELLO" will not be printed out. In this case, the constructor of A will directly call B.specialMethod() (on the instance of the B Object). If you wan't A.specialMethod() to be called, you have to explicitly call it. So, no, you can not 'add functionality' to a method implemented in the super-class without explicitly calling super.specialMethod().

For this reason, I have constructed the example above as a workaround. Please note that I have updated the code. With that example, if you call new B();, the output will be:

specialMethod
anotherMethod

And with this workaround, the extending class does not explicitly have to call super.specialMethod(), but it must implement anotherMethod(). Additionally, by making specialMethod a final method, B is not allowed to override specialMethod.

你必须这样做这样:

public abstract class A{
    public abstract void anotherMethod();

    public A(){
        // some stuff..
        specialMethod();
    }

    private final void specialMethod(){
        System.out.println("specialMethod");
        anotherMethod();
    }
}

public class B extends A{
     public B(){
         super();
     }

     public void anotherMethod(){
         System.out.println("anotherMethod");
     }
}

这样你的力量你的扩展类实现的方法,这种方法被称为anothermethod()从抽象类,如果你的扩展类不需要在anothermethod()做任何事,你可以让法体空。


As mentioned in the comments of your question, you have to either inherit or override the method. When overriding a method, you have to call the super-method super.someMethod(); explicitly or when inheriting, you can not add additional functionality to the given method.
So to come with your example, if you don't implement specialMethod() in B, it will print out "HELLO", since A has an implementation for the method, but B does not. If you override the method like you are doing in the example in class B:

specialMethod(){
    System.out.println("BBBBB");
}

它将有效果,“Hello”将不会打印出来。在这种情况下,一个构造函数将直接调用B. specialmethod()(对B对象实例)。如果你不答specialmethod()被调用,你必须显式地调用它。所以,不,你不能对一个实现在超类中没有显式地调用超类的方法specialmethod()添加功能”。

For this reason, I have constructed the example above as a workaround. Please note that I have updated the code. With that example, if you call 新的b();, the output will be:

specialMethod
anotherMethod

用这种方法,扩展类不明确要叫超级。specialmethod(),但它必须实现anothermethod()。此外,通过方法最终方法,B是不允许重写的方法。

answer2: 回答2:

Use abstract method in A. Like this -

public class A{
   public abstract specialMethod();
}  

Then when B extends A, B have to implement the specialMethod() by its own.

像这样使用抽象方法—

public class A{
   public abstract specialMethod();
}  

当B延伸,B有自己的实施specialmethod()。

java