どのように私は、入力変数として定義されたオブジェクトを使用することができますか?

モフセン:

私は今、私のような私の他のクラスのどこかにそれを使用したい、それは2つの整数を取るとして、前に座標のクラスを定義しています

public boolean isLocationValid( Coordinates location ){
       ...and here I want to define that location should be in the boundaries (for example 0<x<10 and 0<y<10)
    }

しかし、私は知らないし、場所のオブジェクトは、xとyを受け入れません!私に何ができる?

ここでは、座標のコードを検索します:

import java.util.IdentityHashMap;

public class Coordinates {
    private int x  ;
    private int y ;

    public  Coordinates(){ //default constructor
        this.x = 0;
        this.y = 0;
    }
    public Coordinates(int x, int y){ //parameterized constructor
    this.x = x;
    this.y = y;
    }

    //getter and setter
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    //the outcome of the rows should be in Letters, so I defined the Hashmap for the alphabet
  static   IdentityHashMap<Integer, String> Row = new IdentityHashMap<Integer, String>();
    /*Adding elements to HashMap*/
    static {
        Row.put(0, "A");
        Row.put(1, "B");
        Row.put(2, "C");
        Row.put(3, "D");
        Row.put(4,"E");
        Row.put(5, "F");
        Row.put(6, "G");
        Row.put(7, "H");
        Row.put(8, "I");
        Row.put(9, "J");
        Row.put(10, "K");
        Row.put(11, "L");
        Row.put(12, "M");
        Row.put(13, "N");
        Row.put(14, "O");
        Row.put(15, "P");
        Row.put(16, "Q");
        Row.put(17, "R");
        Row.put(18, "S");
        Row.put(19, "T");
        Row.put(20, "U");
        Row.put(21, "V");
        Row.put(22, "W");
        Row.put(23, "X");
        Row.put(24, "Y");
        Row.put(25, "Z");

    }


    public String toString() {
        return Row.get(getX())+getY();
    }
}

それはどのように座標の位置変数を定義し、境界でそれを定義することは可能でしょうか?

アンドリュー・S:

そこにゲッターいるCoordinatesクラスは、その中にisLocationValid()メソッド呼び出しlocation.getX()location.getY()あなたの例を展開するには:

public boolean isLocationValid( Coordinates location ){
    // ...and here I want to define that location should be in the boundaries (for example 0<x<10 and 0<y<10)
    return isBetweenMinAndMax(location.getX() && isBetweenMinAndMax(location.getY()))
}

private final int MIN_EXCLUSIVE = 0;
private final int MAX_EXCLUSIVE = 10;
private isBetweenMinAndMax(int value) {
    return value > MIN_EXCLUSIVE && value < MAX_EXCLUSIVE;
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=345377&siteId=1