【问题标题】:Inherit Square class into circle class将 Square 类继承为 circle 类
【发布时间】:2017-01-19 19:30:40
【问题描述】:

我有一个方形类,我想创建一个圆形类来扩展和继承所有方形类属性...我如何让圆形类做到这一点。

Square 类具有绘制、颜色、下降和大小属性。我如何让圆形类从方形类继承这些东西

方形类

package GamePackage;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Random;

public class Square {

    public int XLocation;
    public int Size;
    public int YLocation = -Size;
    public int fallSpeed = 1;
    Random rand = new Random();


   int R = rand.nextInt((256-40)+1)+40;
   int G = rand.nextInt((256-40)+1)+40; 
   int B= rand.nextInt((256-40)+1)+40;
   Color RandomColour = new Color (R, G, B);

    public int generateRandomXLocation(){
        return XLocation = rand.nextInt(Game.WINDOW_WIDTH - Size);
    }

    /*
    //creates a random value and stores it in squareWidth
    */
    public int generateRandomSquareSize(){ 
        return Size = rand.nextInt((30-17)+1)+17;
    }



    public int generateRandomFallSpeed(){
        return fallSpeed = rand.ints(3, 3, 8).findFirst().getAsInt();  // 3, 3, 8
    }

    /*
    //paints the square with the variables generated in the random methods
    */
    public void paint(Graphics g){

        g.setColor(RandomColour); 
        g.fillRect(XLocation,YLocation,Size,Size);
    }



    /*
    //sets the squareWidth and square fallSpeed to a random value for every square created
    */
    public Square(){
        generateRandomXLocation();
        generateRandomSquareSize();
        generateRandomFallSpeed();

    }



    public void update(){

        //changes the squares xLocation and fallSpeed if the created square reaches the bottom of the screen
        if(YLocation >= Game.WINDOW_HEIGHT){
            generateRandomXLocation();
            generateRandomFallSpeed();
            generateRandomSquareSize();
            YLocation = -Size;
        }

        //moves the square down if the square is inside the window
        if(YLocation <= Game.WINDOW_HEIGHT){
            YLocation += fallSpeed;
        }
    }


    public Rectangle GetBounds(){ 
        Rectangle rectangle = new Rectangle(XLocation,YLocation,Size,Size); 
       return rectangle;
    } 
}

【问题讨论】:

  • 供您参考:从方形继承圆形听起来不太好。尝试创建一个 Shape 类。然后从它继承 Square 和 Circle。
  • 为了游戏,它们都是坠落的物体,这是有道理的
  • 没错。 “它们都是坠落的物体”形状。不是正方形。圆不能是正方形!
  • 或者更好的是,使用组合而不是继承。

标签: java inheritance


【解决方案1】:

如果您想拥有一个子类,只需执行 class A extends B,其中 B 是超类。但是,如果我是你,我会这样做:

public abstract class Shape{
    protected int locX;
    protected int locY;
}

public class Square extends Shape{
    //properties of Square
}

public class Circle extends Shape{
    //properties of Circle
}

甚至没有任何继承..

public abstract class ShapeProperties{
    protected int locX;
    protected int locY;
    //and any other members
}

public class Square{
    ShapeProperties sp;
}

public class Circle{
    ShapeProperties sp;
}

或者..

public interface Positionable{
    public void getLocX();
    public void getLocY();
}

public class Circle implements Positionable{
    //properties of Circle

    @Override
    public int getLocX(){

    }    

    @Override
    public int getLocY(){

    }  

    public Rectangle getBounds(){
        return new Rectangle(getLocX(), getLocY(), size, size);
    }
}

如果您计划将 Circle 扩展到 Square 以获取圆形对象的边界(或 hitbox)。你可以写一个getBounds 方法:

public class Circle{
    //properties of Circle

    public Rectangle getBounds(){
        return new Rectangle(locX, locY, size, size);
    }
}

【讨论】:

    【解决方案2】:

    我认为您尝试做的是错误的方法。

    如果您希望 Square 和 Circle 具有相同的属性,您应该创建一个名为 Shape 的接口,该接口具有您想要实现的属性,然后您只需执行 public class Square implements Shapepublic class Circle implements Shape,然后在两个 Square 中实现属性和圈子。

    如果您希望在所有形状中只实现一个实现,那么除了创建上述接口之外,您还可以创建一个抽象类 ShapeBase,然后您的类将类似于 public class Square extends ShapeBase implements Shapepublic class Circle extends ShapeBase implements Shape,其中您的通用实现将在 ShapeBase 类中完成,任何特定的实现都将在 Square 和 Circle 中完成。

    【讨论】:

      【解决方案3】:

      您应该查看一些 Java 教程。第一个谷歌结果给了我:https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

      你在想这个吗?

      public class Circle extends Square {
      }
      

      然后,Square 的每个公共/受保护(可能还有包)方法和属性在 Circle 中也可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 2023-03-09
        • 2021-09-26
        相关资源
        最近更新 更多