【问题标题】:Overload Constructor with Class in Parameter在参数中使用类重载构造函数
【发布时间】:2018-09-05 19:32:18
【问题描述】:

给出了下面的第一个代码。创建 Turtle 类的正确方法是什么? -- 基本上,我试图让它显示没有错误:Turtle t = new Turtle(STARTX, STARTY, w);

我认为我的问题可能出在重载构造函数上:public Turtle (double STARTX, double STARTY, Class w )

import java.awt.*; //import color;

public class PA1{

  //These are constant values that you can use
  private static  final int STARTX = 100;
  private static  final int STARTY = 100;
  private static  final int CHAR_WIDTH = 100;
  private static  final int CHAR_HEIGHT = 100;
  private static  final int CHAR_SPACING = 50;

  public static void main(String[] args){

    //set the width and height of the world
    int width = 1000;
    int height = 1000;
    World w = new World(width, height);

    //create a turtle at the starting x and starting y pos
    Turtle t = new Turtle(STARTX, STARTY, w);

    //Set the turtle pen width.
    t.setPenWidth(15);

    //This is just an example. Feel free to use it as a reference. 
    //draw a T
    //Assume that the turtle always starts in the top left corner of the character. 
    t.turn(90); 
    t.forward(CHAR_WIDTH);
    t.backward(CHAR_WIDTH/2);
    t.turn(90);
    t.forward(CHAR_HEIGHT); 

    //Move the turtle to the next location for the character
    t.penUp();
    t.moveTo(STARTX+CHAR_WIDTH+CHAR_SPACING*1, STARTY);
    t.penDown(); 



    //WRITE YOUR CODE HERE

  }
}

我创建了 2 个新类:

public class World {
    //World w = new World(width, height);
    private double defaultWidth;
    private double defaultLength;

    public World () {
        defaultWidth = 0.0;
        defaultLength = 0.0; }

    public World (double width, double length){
        defaultWidth = width;
        defaultLength = length; }


    public double getWidth () {
        return defaultWidth; }


    public double getLength () {
        return defaultLength; }



    public void setWidth (double width){
        defaultWidth = width;   }


    public void setLength(double length){
        defaultLength = length; }



}

public class Turtle {

    // Turtle t = new Turtle(STARTX, STARTY, w);

    private double defaultSTARTX;
    private double defaultSTARTY;
    //private double defaultW;

    public Turtle ()    {
        defaultSTARTX = 0.0;
        defaultSTARTY = 0.0; 
        //defaultW = 0.0;
                        }

    public Turtle (double STARTX, double STARTY, Class w ){
        defaultSTARTX = STARTX;
        defaultSTARTY = STARTY; 
        //defaultW = w;
        }

    public double getSTARTX () {
        return defaultSTARTX; }


    public double getSTARTY () {
        return defaultSTARTY; }



    public void setSTARTX (double STARTX){
        defaultSTARTX = STARTX;         }


    public void setSTARTY(double STARTY){
        defaultSTARTY = STARTY;         }



}

【问题讨论】:

    标签: java class constructor


    【解决方案1】:

    如果确实需要将 World 对象传递给 Turtle 对象,则应将 Turtle 构造函数定义为:

    public Turtle (double STARTX, double STARTY, World w ){
        defaultSTARTX = STARTX;
        defaultSTARTY = STARTY; 
        defaultW = w;
    }
    

    此外,您必须声明“w”,而不是像您在某些时候所做的那样,而是作为 Turtle 中的“World”类变量:

    private World defaultW;
    

    Class 作为构造函数参数传递,您正在尝试传递泛型类定义,而不是任何类的Object 实例。差异是微妙的,但确实存在,并且是您最可能遇到的问题。

    【讨论】:

      【解决方案2】:

      我在您上面提供的代码中发现了许多问题。我不知道它是否是完整的代码。我试图修复您代码中的所有问题 - 与您的构造函数和其他 java 标准相关。以下对我有用 -

      public class MyClass {
      
          private static  final int STARTX = 100;
          private static  final int STARTY = 50;
      
          public static void main(String args[]) {
              int width = 1000;
              int height = 1000;
              World w = new World(width, height);
      
              //create a turtle at the starting x and starting y pos
              Turtle t = new Turtle(STARTX, STARTY, w);
      
              System.out.println(t.getSTARTX() + " : " + t.getSTARTY() + " : " + t.getWorld().getLength() + " : " + t.getWorld().getWidth());
          }
      }
      
      class World {
          private double width;
          private double length;
      
          public World () {
              this.width = 0.0;
              this.length = 0.0;
          }
      
          public World (double width, double length) {
              this.width = width;
              this.length = length;
          }
      
          public double getWidth () { return this.width; }
          public double getLength () { return this.length; }
          public void setWidth (double width){ this.width = width; }
          public void setLength(double length){ this.length = length; }
      }
      
      class Turtle {
          private double STARTX;
          private double STARTY;
          private World world;
      
          public Turtle () {
              this.STARTX = 0.0;
              this.STARTY = 0.0; 
              this.world = new World();
          }
      
          public Turtle (double STARTX, double STARTY, World w) {
              this.STARTX = STARTX;
              this.STARTY = STARTY; 
              this.world = w;
              }
      
          public double getSTARTX () { return this.STARTX; }
          public double getSTARTY () { return this.STARTY; }
          public World getWorld(){ return this.world; }
          public void setSTARTX (double STARTX){ this.STARTX = STARTX; }
          public void setSTARTY(double STARTY){ this.STARTY = STARTY; }
          public void setWorld (World world){ this.world = world; }
      
      }
      

      希望它能解决您的问题。 快乐编码。 :)

      【讨论】:

        猜你喜欢
        • 2012-11-30
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-01
        • 1970-01-01
        • 2015-11-05
        • 2012-07-22
        相关资源
        最近更新 更多