簡體   English   中英

參數中帶有類的重載構造函數

[英]Overload Constructor with Class in Parameter

在給出下面的第一個代碼。 創建Turtle類的正確方法是什么? -基本上,我試圖Turtle t = new Turtle(STARTX, STARTY, w);不顯示任何錯誤: 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;         }



}

如果確實需要將World對象傳遞給Turtle對象,則應按以下方式定義Turtle構造函數:

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

另外,您必須聲明“ w”不像在某些時候所做的那樣為double,而應作為Turtle中的“ World”類變量:

private World defaultW;

Class作為構造函數參數傳遞時,您嘗試傳遞通用類定義,而不是任何類的Object實例。 差異很細微,但確實存在,並且是您最有可能遇到的問題。

我在您上面提供的代碼中發現了許多問題。 我不知道這是否是完整的代碼。 我試圖解決您代碼中的所有問題-與您的構造函數和其他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; }

}

希望它能解決您的查詢。 快樂的編碼。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM