Java-从子类访问父类的值

时间:2017-11-09 03:39:50

标签: java

嘿,我试图弄清楚从子类中的父类访问变量的值。

情景#1

以下是课程DrawingPanel()的代码:

public class DrawingPanel extends JPanel implements MouseListener
{
    private ArrayList<Balloon> balloons;
    private Balloon activeBalloon;
    private boolean picked;
    private int offsetX, offsetY;
    private double offsetR;
    int rotate;

    // Constructor:
    public DrawingPanel()
    {
        setBackground(Color.WHITE);
        addMouseListener(this);
        balloons = new ArrayList<Balloon>();
        activeBalloon = null;
        picked = false;
        rotate = 0;
    }

    // Called from controlPanel when the "Add balloon" button is clicked.
    // Places a new balloon with a random radius and the current color
    // at the center of the drawing panel.
    public void addBalloon(int shape)
    {
        int w = getWidth();
        int h = getHeight();
        int radius = 10 + (int) (Math.random() * w / 2);
        switch (shape)
        {
        case 1:
            activeBalloon = new SquareBalloon(w / 2, h / 2, radius);
            break;
        default:
            activeBalloon = new SquareBalloon(w / 2, h / 2, radius);
            break;
        }
        balloons.add(activeBalloon);
        repaint();
    }

    // Repaints this panel. If activeBalloon is set, paints it on top.
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        for (Balloon b : balloons)
        {
            if (!picked || b != activeBalloon)
            {
                b.draw(g, true);
            }

        }
        if (picked && activeBalloon != null)
            activeBalloon.draw(g, false);
    }

    // Called when the mouse is clicked on the drawing panel.
    // If inside a balloon, makes it "active", remembers the offsets
    // of the click from the center.
    // If on the border of a balloon, makes it "active", remembers the
    // distance of the click from the center.
    @Override
    public void mousePressed(MouseEvent e)
    {
        // Value for parameter rotate is set here on mouse click
        rotate = 2;
        int x = e.getX();
        int y = e.getY();
        picked = false;
        for (int k = balloons.size() - 1; k >= 0 && !picked; k--)
        {
            Balloon b = balloons.get(k);
            if (b.isInside(x, y))
            {
                picked = true;
                offsetX = x - b.getX();
                offsetY = y - b.getY();
                activeBalloon = b;

            } else if (b.isOnBorder(x, y))
            {
                picked = true;
                offsetR = b.distance(x, y) - b.getRadius();
                activeBalloon = b;
            }
        }
        if (picked)
            repaint();
    }

    // "Drops" the picked balloon, if any.
    @Override
    public void mouseReleased(MouseEvent e)
    {
    }

    // Not used:
    @Override
    public void mouseEntered(MouseEvent e)
    {
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
    }
}

以下是继承父类SquareBalloon()的{​​{1}}类。 Balloon类继承Balloon类。

DrawingPanel()

情景#2

以下是课程public class SquareBalloon extends Balloon implements MouseListener { public SquareBalloon() { } /** * Constructs a balloon with a given center, radius and color * * @param x * x-coordinate of the center * @param y * y-coordinate of the center * @param r * radius of the balloon */ public SquareBalloon(int x, int y, int r) { super(x, y, r); } /** * Draws a solid circle if makeItFilled is true and outline only if * makeItFilled is false * * @param g * graphics context * @param makeItFilled * draws a solid circle if true */ @Override public void draw(Graphics g, boolean makeItFilled) { Graphics2D g2d = (Graphics2D) g; g.setColor(color); System.out.println("This is the value of rotate" + rotate + "from the draw() method of SquareBalloon() class."); if (makeItFilled) { Rectangle rect2 = new Rectangle(xCenter - radius, yCenter - radius, radius, radius); if (rotate == 2) { System.out.println("This is the value of rotate" + rotate + "from the makedItFilled if statement in draw() method of SquareBalloon() class."); g2d.rotate(45 * (Math.PI / 180)); } g2d.fill(rect2); } else { Rectangle rect2 = new Rectangle(xCenter - radius, yCenter - radius, radius, radius); if (rotate == 2) { System.out.println("This is the value of rotate" + rotate + "from the other if statement in draw() method of SquareBalloon() class."); g2d.rotate(45 * (Math.PI / 180)); } g2d.draw(rect2); } } } 的代码:

DrawingPanel()

以下是继承父类public class DrawingPanel extends JPanel implements MouseListener { private ArrayList<Balloon> balloons; private Balloon activeBalloon; private boolean picked; private int offsetX, offsetY; private double offsetR; int rotate; // Constructor: public DrawingPanel() { setBackground(Color.WHITE); addMouseListener(this); balloons = new ArrayList<Balloon>(); activeBalloon = null; picked = false; rotate = 0; } // Called from controlPanel when the "Add balloon" button is clicked. // Places a new balloon with a random radius and the current color // at the center of the drawing panel. public void addBalloon(int shape) { int w = getWidth(); int h = getHeight(); int radius = 10 + (int) (Math.random() * w / 2); switch (shape) { case 1: activeBalloon = new SquareBalloon(w / 2, h / 2, radius); break; default: activeBalloon = new SquareBalloon(w / 2, h / 2, radius); break; } balloons.add(activeBalloon); repaint(); } // Repaints this panel. If activeBalloon is set, paints it on top. @Override public void paintComponent(Graphics g) { // to restore original angle of balloon objects if (rotate > 2) rotate = 0; super.paintComponent(g); for (Balloon b : balloons) { if (!picked || b != activeBalloon) { b.draw(g, true, rotate); } } if (picked && activeBalloon != null) activeBalloon.draw(g, false, rotate); } // Called when the mouse is clicked on the drawing panel. // If inside a balloon, makes it "active", remembers the offsets // of the click from the center. // If on the border of a balloon, makes it "active", remembers the // distance of the click from the center. @Override public void mousePressed(MouseEvent e) { // Value for parameter rotate is set here on mouse click rotate++; int x = e.getX(); int y = e.getY(); picked = false; for (int k = balloons.size() - 1; k >= 0 && !picked; k--) { Balloon b = balloons.get(k); if (b.isInside(x, y)) { picked = true; offsetX = x - b.getX(); offsetY = y - b.getY(); activeBalloon = b; } else if (b.isOnBorder(x, y)) { picked = true; offsetR = b.distance(x, y) - b.getRadius(); activeBalloon = b; } } if (picked) repaint(); } // "Drops" the picked balloon, if any. @Override public void mouseReleased(MouseEvent e) { } // Not used: @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } } 的{​​{1}}类。 SquareBalloon()类继承Balloon类。

Balloon

有人可以解释为什么在场景#1 中,DrawingPanel()方法启动后变量int的值的变化没有被传递给public class SquareBalloon extends Balloon implements MouseListener { public SquareBalloon() { } /** * Constructs a balloon with a given center, radius and color * * @param x * x-coordinate of the center * @param y * y-coordinate of the center * @param r * radius of the balloon */ public SquareBalloon(int x, int y, int r) { super(x, y, r); } /** * Draws a solid circle if makeItFilled is true and outline only if * makeItFilled is false * * @param g * graphics context * @param makeItFilled * draws a solid circle if true * @param rotate * receives rotate variable value set in mousePressed() method of * DrawingPanel() class */ @Override public void draw(Graphics g, boolean makeItFilled, int rotate) { Graphics2D g2d = (Graphics2D) g; g.setColor(color); System.out.println("This is the value of rotate" + rotate + "from the draw() method of SquareBalloon() class."); if (makeItFilled) { Rectangle rect2 = new Rectangle(xCenter - radius, yCenter - radius, radius, radius); if (rotate == 2) { System.out.println("This is the value of rotate" + rotate + "from the makedItFilled if statement in draw() method of SquareBalloon() class."); g2d.rotate(45 * (Math.PI / 180)); } g2d.fill(rect2); } else { Rectangle rect2 = new Rectangle(xCenter - radius, yCenter - radius, radius, radius); if (rotate == 2) { System.out.println("This is the value of rotate" + rotate + "from the other if statement in draw() method of SquareBalloon() class."); g2d.rotate(45 * (Math.PI / 180)); } g2d.draw(rect2); } } } 的子类class,以便在mousePressed类的Balloon方法中执行条件为 rotate == 2 的if语句?因为在场景#2 中它确实有用。

谢谢!

0 个答案:

没有答案