简体   繁体   English

子类在具有不同构造函数的抽象超类中实现的常用方法

[英]subclasses common methods implemented in abstract superclass with different constructors

I know that this has been discussed already here but i have something to add up to that question. 我知道这已经在这里讨论了但我有一些东西要加上这个问题。 I need to make an abstract class called AbstractGraph, and it has to be extended by two types of graph implementations: one uses a matrix, the other one uses lists. 我需要创建一个名为AbstractGraph的抽象类,它必须通过两种类型的图形实现进行扩展:一种使用矩阵,另一种使用列表。

So far i have something like this: 到目前为止,我有这样的事情:

abstract class AbstractGraph implements Graph {

    public void removeAllEdges(){
        //implementation here 
    }

}

and the subclasses: 和子类:

public GraphMatrix(){
    GraphMatrix()
    {
        //implementation for matrix type 
    }
}

public GraphList(){
    GraphList()
    {
        //different implementation for lists type
    }
}

both implementations would have the same removeAllEdges() method written exactly the same, so i guess it has to be placed in the abstract class. 两个实现都会使用完全相同的removeAllEdges()方法,因此我猜它必须放在抽象类中。 My question is, how do i get to use references to this from inside the abstract class ? 我的问题是,如何从抽象类中使用this引用? Do i have to fill the implementations with getters and setters ? 我是否必须使用getter和setter填充实现? If so, what would be the benefit of using a single implementation of a method for two (or more) subclasses. 如果是这样,对两个(或更多)子类使用单个方法实现的好处是什么。

You can use the same code for removeAllEdges as long as it does not depend on the underlying structure (eg, the matrix or the list) or the functionality is implemented by encapsulated methods that use the underlying graph structure (eg, getters, setters, and other common methods that you may need). 只要不依赖于底层结构(例如,矩阵或列表),或者功能是通过使用底层图形结构的封装方法(例如,getter,setter和您可能需要的其他常用方法)。 In my perspective, the latter methods would be the abstract methods of this class, since they need to be implemented according to the underlying graph structure. 在我看来,后面的方法将是这个类的抽象方法,因为它们需要根据底层的图形结构来实现。

One of the benefits is code reuse. 其中一个好处是代码重用。 You write once, and use it in every graph implementation. 您只需编写一次,并在每个图形实现中使用它。 In your case you have two graph classes, but you may end up having multiple representations for graphs depending on your application. 在您的情况下,您有两个图表类,但您最终可能会根据您的应用程序对图表进行多次表示。 Writing general methods is a nice way of saving time spent in coding, and it also helps you to maintain your code in a more efficient and organized way. 编写通用方法是节省编码时间的好方法,它还可以帮助您以更高效,更有条理的方式维护代码。 The debugging is also easier!! 调试也更容易!

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

相关问题 使用超类的“protected final”方法来保留子类的通用代码 - Using superclass “protected final” methods to keep common code for subclasses 将超类中实现的方法转换为抽象 - Turning a method implemented in superclass into abstract 抽象方法和构造函数 - Abstract Methods and Constructors 使用抽象超类生成子类的随机实例 - Generating Random instances of subclasses using a Abstract Superclass 我如何创建3个超抽象类,它们已经在构造函数中提供了数量不同的相似子类 - How can i create a super abstract class of 3 already present similar subclasses having different number of parameters in their constructors 在超类构造函数中调用抽象方法 - Calling abstract methods in a superclass constructor SuperClass数组上的子类的Java多态性调用方法 - Java Polimorphism calling methods of subclasses on an array of SuperClass Java:强制子类重写超类的方法 - Java: Force subclasses to override methods of the Superclass 是否可以在超类对象上调用子类的方法? - Is it possible to call subclasses' methods on a superclass object? 从抽象 class 调用方法,其中具体子类具有这些方法的不同输入参数 - Calling methods from abstract class where concrete subclasses have different input parameters of those methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM