【问题标题】:What is wrong with my inheritance structure?我的继承结构有什么问题?
【发布时间】:2014-02-04 16:46:00
【问题描述】:

我正在尝试制作一个显示继承和多态性的程序。该程序应该显示一面旗帜(特别是几内亚的旗帜)。

这是代码:

import java.awt.*;
import javax.swing.*;

public class FlagB extends JPanel
{
    public void paint (Graphics g)
    {
        Flag Guinea = new Flag(50,290,560);
        Guinea.drawFlag(g);
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Bolivian Flag");
        frame.add(new FlagB());
        frame.setSize(1000,725);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class Flag
{
    private FlagBase fb;
    private FitRect rp; //rp for rectangle parameters
    private LeftTriColor lt;
    private MiddleTriColor mt;
    private RightTriColor rt;
    private int x;
    private int y;
    private int z;


    public Flag(int x,int y,int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        fb = new FlagBase();
        rp = new FitRect(x,y,z);
        lt = new LeftTriColor(x,y,z);
        mt = new MiddleTriColor(x,y,z);
        rt = new RightTriColor(x,y,z);
    }

    public void drawFlag(Graphics g)
    {
        fb.FlagBase(g);
        lt.drawRect(g);
        mt.drawRect(g);
        rt.drawRect(g);
    }
}

class FlagBase // The outline of the flag
{
    public void FlagBase(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.drawRect(49,49,876,562);
    }
}

class FitRect
{
    protected int l;
    protected int length;
    protected int width;
    public FitRect(int x,int y,int z)
    {
        x = l;
        y = length;
        z = width;
    }
}

class LeftTriColor extends FitRect
{
    public LeftTriColor(int x,int y,int z)
    {
        super(x,y,z);
    }

    public void drawRect(Graphics g)
    {
        g.setColor(Color.RED);
        g.fillRect(l,l,length,width);
    }
}

class MiddleTriColor extends FitRect
{
    public MiddleTriColor(int x,int y,int z)
    {
        super(x,y,z);
    }

    public void drawRect(Graphics g)
    {
        g.setColor(Color.YELLOW);
        g.fillRect(l+length+1,l,length,width);
    }
}

class RightTriColor extends FitRect
{
    public RightTriColor(int x,int y,int z)
    {
        super(x,y,z);
    }

    public void drawRect(Graphics g)
    {
        g.setColor(Color.GREEN);
        g.fillRect(l+2*length+3,l,length+1,width);
    }
}

新窗口中唯一显示的是旗帜的轮廓。我认为这与 FitRect 的参数传递有关。是它,还是别的什么? 我一直在使用 JCreator。

【问题讨论】:

  • 我认为主要问题是您正在尝试绘制几内亚国旗,但您的框架标题是“玻利维亚国旗”。
  • 人们可能会认为FlagB 会扩展Flag
  • 您是否尝试过在 IDE 调试器中运行它以查看它在哪里做错了?先尝试一下,然后在发现时缩小问题范围。
  • @Reimeus 你是什么意思?
  • @SamIam FlagB 实际上是 JPanel 对象。 Flag是FlagB中绘制的内容

标签: java inheritance polymorphism computer-science


【解决方案1】:

这应该可以解决。

class FitRect
{
    protected int l;
    protected int length;
    protected int width;
    public FitRect(int x,int y,int z)
    {
        l = x;         //was x = l
        length = y;    //was y = length
        width = z;     //was z = width
    }
}

【讨论】:

  • 谢谢。是否有原因导致它无法以其他方式工作?
  • 另一种方式没有意义。像这样,您说l 将具有x 的值,它作为参数传递。就像你以前的方式,你说x(没有存储在任何地方)的值将是l(它甚至没有值,所以它会是空的或垃圾)。很高兴它有帮助,请将答案标记为已接受:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2020-04-07
  • 1970-01-01
相关资源
最近更新 更多