简体   繁体   English

子类中与抽象父类的不同方法行为

[英]Different method behavior in child class from abstract parent class

I'm working on a drawing project, and I have certain tools (ie a drawing brush, an eraser, etc.) that are their own classes, each inheriting from an abstract Tool class with some basic properties and abstract/non-abstract methods.我正在做一个绘图项目,并且我有某些工具(即画笔、橡皮擦等)是它们自己的类,每个工具都继承自具有一些基本属性和抽象/非抽象方法的抽象工具类. The main problem I am seeing is when I need to change the color for these tools, I want to figure out a way to do it for all tools, but also allow certain tools (like an Eraser) to remain unchanged since it's color should always just be the background color.我看到的主要问题是当我需要更改这些工具的颜色时,我想找出一种方法来为所有工具做到这一点,但也允许某些工具(如橡皮擦)保持不变,因为它的颜色应该始终只是背景颜色。

I am receiving the onColorChange(int newColor) method call from elsewhere, but this is the starting point for where I trigger the color change for the tools.我从其他地方收到onColorChange(int newColor)方法调用,但这是我触发工具颜色更改的起点。 Right now, it only changes the color for the active tool, but that means each tool has a different color set and it gets confusing.现在,它只更改活动工具的颜色,但这意味着每个工具都有不同的颜色集,这会让人感到困惑。

I want a color change to trigger the setColor() method for all of the Tools, so I thought about putting the setColor() method as a static method in the Tool class, but that wouldn't allow me to override in the Eraser class to prevent the color being changed.我想要颜色更改来触发所有工具的setColor()方法,所以我考虑将 setColor() 方法作为静态方法放在 Tool 类中,但这不允许我在 Eraser 类中覆盖以防止颜色被改变。

Anyone have any ideas on how to implement this?有人对如何实现这个有任何想法吗? TLDR: I want the onColorChange() event to trigger a color change for all of the tools, but also I want to override that in the Eraser class so no color change takes place for that tool. TLDR:我希望 onColorChange() 事件触发所有工具的颜色更改,但我也想在 Eraser 类中覆盖它,因此该工具不会发生颜色更改。

Currently, with my implementation, I have the following:目前,通过我的实施,我有以下内容:

Tool currentTool;
Tool myBrush;
Tool myEraser;
Tool myPencil;

// This is called from elsewhere
pubic void setup() {
   myBrush = new Brush();
   myEraser = new Eraser();
   myPencil = new Pencil();
   currentTool = myBrush;
}

// This is called from elsewhere
public void onColorChange(int newColor) {
   currentTool.setColor(newColor);
}

Tool.java工具.java

public abstract class Tool {

    protected String toolName;

    protected Paint myPaint;

    public String getToolName() {
        return mToolName;
    }

    public int getColor() {
        return myPaint.getColor();
    }

    public Paint getPaintObject() {
        return myPaint;
    }

    protected abstract void setColor(int newColor);
}

Eraser.java橡皮擦.java

public class Eraser extends Tool {


    public Eraser() {
        toolName = "ERASER";


        myPaint = new Paint();
        mFgPaint.setColor(Color.parseColor("#FFFFFF"));
        mFgPaint.setAntiAlias(true);
        mFgPaint.setStrokeWidth(DEFAULT_WIDTH);
        mFgPaint.setStyle(Paint.Style.STROKE);
        mFgPaint.setStrokeJoin(Paint.Join.ROUND);
        mFgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void setColor(int newColor) {
        // Empty, since I cannot change the paint color of the eraser
    }

}

Brush.java刷机.java

public class Brush extends Tool {

   
    public Brush() {
        toolName = "BRUSH";
        myPaint = new Paint();
        setColor(Color.parseColor("#000000"));
        mFgPaint.setAntiAlias(true);
        mFgPaint.setStrokeWidth(DEFAULT_WIDTH);
        mFgPaint.setStyle(Paint.Style.STROKE);
        mFgPaint.setStrokeJoin(Paint.Join.ROUND);
        mFgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void setColor(int newColor) {
        myPaint.setColor(newColor);
    }

Pencil.java铅笔.java

public class Pencil extends Tool {

   
    public Brush() {
        toolName = "PENCIL";
        myPaint = new Paint();
        setColor(Color.parseColor("#000000"));
        mFgPaint.setAntiAlias(true);
        mFgPaint.setStrokeWidth(DEFAULT_WIDTH);
        mFgPaint.setStyle(Paint.Style.STROKE);
        mFgPaint.setStrokeJoin(Paint.Join.ROUND);
        mFgPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void setColor(int newColor) {
        myPaint.setColor(newColor);
    }

I would recommend to add a new abstract class ColorableTool extending Tool which stores the method setColor我建议添加一个新的抽象类ColorableTool扩展Tool存储方法setColor
Eraser will extend Tool while Brush and Pencil will extend ColorableTool Eraser将扩展Tool ,而BrushPencil将扩展ColorableTool
If your project goes more into polymorphism you should check interfaces for such behavior (in Java) !如果您的项目更多地涉及多态性,您应该检查接口是否存在这种行为(在 Java 中)! :) :)

Edit 1编辑 1

You might check your current tool is a ColorableTool using instanceof like this您可能会使用这样的instanceof检查您当前的工具是ColorableTool

if(currentTool instanceof ColorableTool) {
  // Do Stuff
}

EDIT 2编辑 2

As you want to update every color I suggest to use a static paint, an instance of paint shared among the Tool (or ColorableTool if you implemented my solution).当您想更新每种颜色时,我建议使用static绘画,即在Tool之间共享的绘画实例(如果您实施了我的解决方案,则为ColorableTool )。 It should look like它应该看起来像

public abstract class Tool {

    protected String toolName;
    static final Paint myPaint = new Paint(); // A shared paint between tools

    public String getToolName() {
        return toolName;
    }

    public int getColor() {
        return myPaint.getColor();
    }

    public Paint getPaintObject() {
        return myPaint;
    }

    public static void setColor(int newColor) {
        Tool.myPaint.setColor(newColor);
    }
}

Therefore calling Tool.setColor will update color for every tool !因此调用Tool.setColor将更新每个工具的颜色!

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

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