简体   繁体   English

超类常用方法实现

[英]Superclass Common Method Implementation

I'm fairly new to OO programming specifically with Java. 我对使用Java的OO编程很新。 I have a question pertaining to inheritance. 我有一个关于继承的问题。

I have a printing method that I'd like to be common among subclasses. 我有一个打印方法,我希望在子类中很常见。 The code in the print method is usable for all subclasses except for a response object that is specific to each individual subclass. print方法中的代码可用于所有子类,但特定于每个子类的响应对象除外。

I'm thinking that i need to probably just override the method in each subclass providing the specific implementation. 我想我可能只是覆盖每个子类中提供特定实现的方法。 However it feels like there would be a slicker way to keep the common method in the super class and while somehow supplying the specific response object based on the subclass accessing it. 然而,感觉就像在超类中保持公共方法并且以某种方式基于访问它的子类提供特定响应对象一样。

Any thoughts? 有什么想法吗? Sorry if this seems elementary.... 对不起,如果这看起来很......

You will want an abstract base class that defines what is done, while the child classes define how it is done. 您将需要一个定义完成内容的抽象基类,而子类定义它是如何完成的。 Here's a hint as to how such a thing might look 这是一个关于这样的事情看起来如何的暗示

public abstract class BaseClass{

    public final String print(){
        return "response object: " + responseObject();
    }
    protected abstract Object responseObject();

}

This is loosely related to the Template Method pattern, which might be of interest to you. 这与模板方法模式松散相关,您可能会对此感兴趣。

You are absolutely right, there is a better way. 你是绝对正确的,还有更好的方法。 If your implementations share a great deal of code, you can use template method pattern to reuse as much implementation as possible. 如果您的实现共享大量代码,则可以使用模板方法模式尽可能多地重用实现。

Define a printReponse method in the superclass, and make it abstract. 在超类中定义printReponse方法,并使其成为抽象方法。 Then write your print method in the superclass that does the common thing and calls printResponse when needed. 然后在执行常见printResponse的超类中编写print方法,并在需要时调用printResponse Finally, override only printResponse in the subclasses. 最后,仅覆盖子类中的printResponse

public abstract class BasePrintable {
    protected abstract void printResponse();
    public void print() {
        // Print the common part
        printResponse();
        // Print more common parts
    }
}

public class FirstPrintable extends BasePrintable {
    protected void printResponse() {
        // first implementation
    }
}

public class SecondPrintable extends BasePrintable {
    protected void printResponse() {
        // second implementation
    }
}

You can do something like this 你可以做这样的事情

public class A {
     protected String getResponse(){ return "Response from A"; }
     public void print(){
          System.out.println( this.getName() );
     }
}

public class B extends A {
     protected String getResponse(){ return "Response from B"; }
}

A a = new A();
B b = new B();

a.print(); // Response from A
b.print(); // Response from B

ie you dont need to override the print method, just the getResponse 即你不需要覆盖print方法,只需要覆盖getResponse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM