简体   繁体   English

父类和子类之间的设计建议?

[英]An advice for a design between parent and child classes?

I'm working on a physics simulation. 我正在进行物理模拟。

I have an ArrayList that holds all the objects in my simulation. 我有一个ArrayList ,它包含我的模拟中的所有对象。 I have a parent class: Shape , and two child classes: Circle and Rectangle . 我有一个父类: Shape和两个子类: CircleRectangle

The parent class, of course, doesn't have a draw() method but each of the child classes does. 当然,父类没有draw()方法,但每个子类都有。 Therefore, when I'm looping trough the list to draw each element, it doesn't allow me because there isn't a draw() method in the Shape class (as I'm defining the list as ArrayList<Shape> , and adding each new element with a child class instance). 因此,当我循环遍历列表以绘制每个元素时,它不允许我,因为Shape类中没有draw()方法(因为我将列表定义为ArrayList<Shape> ,并且使用子类实例添加每个新元素)。

Is there a way to resolve this problem in a good and neat way? 有没有办法以一种良好而整洁的方式解决这个问题?

it seems to provide an abstract method for the Shape class where all subclasses share a common behaviour is best for the task at hand. 它似乎为Shape类提供了一个抽象方法,其中所有子类共享一个共同的行为最适合手头的任务。

Consider this is the Shape class: 考虑这是Shape类:

public abstract class Shapes{
    public abstract void Draw();
}

the Rectangle class: Rectangle类:

public class Rectangle extends Shapes{
    public void Draw(){
        System.out.println("Rectangle");
    }
}

the Circle class: Circle类:

public class Circle extends Shapes{
    public void Draw(){
        System.out.println("Circle");
    }
}

now considering that both Circle and Rectangle are of type Shape , you can create objects of type Circle or/and Rectangle , add them to the ArrayList, iterate over it, and invoke the Draw() method on each object like so: 现在考虑到CircleRectangle都是Shape类型,你可以创建Circle或/和Rectangle类型的对象,将它们添加到ArrayList,迭代它,并在每个对象上调用Draw()方法,如下所示:

ArrayList<Shapes> shapes = new ArrayList<>();
shapes.add(new Circle());
shapes.add(new Rectangle());
shapes.forEach(Shapes::Draw);

result when Draw() method is invoked on each object: 在每个对象上调用Draw()方法时的结果:

Circle
Rectangle

The neatest way to move forward is to use interfaces. 前进的最好方法是使用接口。

public interface Shape {
    void draw();
}

public class Rectangle implements Shape {
    @Override
    public void draw() { // draw rect }
}

public class Circle implements Shape {
    @Override
    public void draw() { // draw circle }
}

If you want Shape to share some other logic with it's children you can create an AbstractShape class implementing Shape with any additional code and extend the child classes using this abstract class. 如果你希望Shape与它的子项共享一些其他逻辑,你可以使用任何其他代码创建一个实现Shape的AbstractShape类,并使用这个抽象类扩展子类。

This is how I would do it. 我就是这样做的。

A class called Shapes that has a field for List< Shape > Shape is an interface holding the method draw() getArea() or any others. 名为Shapes的类具有List < Shape > Shape的字段,该类是包含方法draw()getArea()或其他任何方法的接口。 Have as many classes implementing Shape , circle, rectangle, square etc. 有尽可能多的类实现Shape ,circle,rectangle,square等。

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

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