1

クラス Point 内に Point というコンストラクターがある場合:

public Point(double x, double y) {
        this.x = x;
        this.y = y; 
}

Square という別のクラスを使用してポイントを初期化するにはどうすればよいですか。たとえば、正方形の 4 つのポイントを初期化したい。どうやってやるの?

それが理にかなっているのかどうかはわかりません。しかし、私は最善を尽くしました...もっとよく説明できるように質問してください。

4

6 に答える 6

5

Square クラスには、次のようなコンストラクターが必要です。

public Square(Point p1, Point p2, Point p3, Point p4) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
        this.p4 = p4;
}

次のように Square を初期化します。

Square s = new Square(new Point(1,1), new Point(2,2), new Point(3,3), new Point(4,4));
于 2012-11-05T14:02:06.727 に答える
3

ポイントに withが必要な場合はSquare、それらを属性にします。

class Square {
    Point p1, p2, p3, p4;
    public Square() {
      p1 = new Point(0,0);
      p2 = new Point(0,0);
      p3 = new Point(0,0);
      p4 = new Point(0,0);
    }
}

もちろん、これを定義して使用する方法は他にも無数にあります。何を選択するかは、主にクラス/プログラムの設計によって異なります。

于 2012-11-05T14:02:57.607 に答える
2

私が従うかどうかはわかりませんが、あなたが言うようなコンストラクターがある場合はnew Point(10.0, 15.0)、指定されたcoordiantesにポイントを作成するために呼び出すことができます。

多分それはあなたが求めているこのようなものですか?

class Square {
    private Point upperLeftCorner;
    private Point upperRightCorner;
    private Point lowerLeftCorner;
    private Point lowerRightCorner;

    public Square(double x, double y, double size) {
        upperLeftCorner = new Point(x, y);
        lowerLeftCorner = new Point(x, y+size);
        upperRightCorner = new Point(x+size, y);
        lowerRightCorner = new Point(x+size, y+size);
    }
}
于 2012-11-05T14:05:16.157 に答える
2

問題がわかりません。通常の-syntaxを使用して初期化されるSquaretype の 4 つのメンバーを持つだけです。Pointnew

class Square {
    Point topLeft;

    public Square() {
        topLeft = new Point(0,0);
    }
}
于 2012-11-05T14:01:59.833 に答える
2

できるよ

public Shape(Point... points) {
    this.points = points;
}

また

public Quadrilateral(Point p1, Point p2, Point p3, Point p4) {
    this.p1 = p1;
    this.p2 = p2;
    this.p3 = p3;
    this.p4 = p4;
}

また

public Square(double x, double y, double size) {
    this.x = x;
    this.y = y;
    this.size = size;
}
于 2012-11-05T14:02:46.620 に答える
0

これはあなたが意味するものですか?

java GeoTest [{0.0,0.0}, {0.0,10.0}, {10.0,10.0}, {10.0,0.0}]

コード:

import java.util.Arrays;
public class GeoTest{
    public static void main(String[] args){
        System.out.println(Arrays.toString(new Square(new Point(0,0), new Point(10,10)).getCourners()));
    }
}
class Point{
    private double x;
    private double y;
    public Point(double x, double y) {
            this.x = x;
            this.y = y; 
    }
    public String toString(){
        return new String("{"+x+","+y+"}");
    }
    public double getX(){
        return x;
    }
    public double getY(){
        return y;
    }
}

class Square {
    private Point lowerLeft;
    private Point upperRight;
    /**
     * Assuming x & y axis as follows: 
     * Lower left corner (x,y):0,0 -> upper right corner (x,y): n,n where n > 0
     */
    public Square(Point lowerLeft, Point upperRight){
        this.lowerLeft = lowerLeft;
        this.upperRight = upperRight;
    }
    public Point[] getCourners(){
        return new Point[]{lowerLeft, new Point(lowerLeft.getX(),upperRight.getY()),upperRight, new Point(upperRight.getX(), lowerLeft.getY())};
    }
}
于 2012-11-05T14:19:48.533 に答える