2016-07-24 18 views
-2

同じクラスの2つのオブジェクトの属性を比較するメソッドを作成したいと思います。 I'vはPOINT1の属性持っているPointクラスの&ポイント2、持って言う:クラス属性の比較

public class Point{ 
    private double x; 
    private double y; 
    private double z; 

[...] */constructor and methods*/[...] 
} 

public static void main (String[] args){ 
    Point point1 = new Point(5, 10, 20); 
    Point point2 = new Point(0, 5, 10); 
} 

が、私はお互いに対してPOINT1 &ポイント2のx、y、zの値を比較したいのが、私は考えていますそれを行う方法。メソッドのコードブロック内の異なる値をどのように区別することができますか?私ができるなら、これは私が書くことができる理論的な方法です:

public double comparePoints(Point a, Point b){ 
    if (x1 < x2){ 
     System.out.println("Point b has the bigger x-value"); 
     return x2; 
    } 
    etc. 
} 

どのようにこれを行うには?

+0

可能な重複:http://stackoverflow.com/questions/4108604/java-comparable-vs-comparator –

+0

あなたは 'の場合(a.x Joe

+0

はい、a.x構文は欠落部分でした。ありがとうございました! – Play4Fun

答えて

0

コンパレータインタフェースを使用して比較するgetter \ setterを使用して、プライベート宣言されたプロパティに安全にアクセスしたり、obj1.xおよびobj2を使用して各オブジェクトからプロパティにアクセスしたり。バツ。それは、どのようなアプローチを取るかについての正確な要件に依存します。私が更新した次のコードは、2つのオブジェクトの属性の基本比較を行います。

public class Point {  

     private double x; 
     private double y; 
     private double z;  

     //Constructor 
     Point(double l,double m,double n){ 

      this.x = l; 
      this.y = m; 
      this.z = n;   

     } 

    //main method 
    public static void main (String[] args){ 

     Point point1 = new Point(5, 10, 20); 

     Point point2 = new Point(0, 5, 10); 

     //Object that is created with the greater value 
     Point point3 = comparePoints(point1,point2); 

     System.out.println("-----The greater value of property, " 
       + "between the two compared objects is as below----"); 

     System.out.println("x ="+point3.x); 
     System.out.println("y ="+point3.y); 
     System.out.println("z ="+point3.z); 

    } 


    //Compare method declared static, for the satic main method to access 
    public static Point comparePoints(Point a, Point b){  

     System.out.println("Values in Object a = "+a.x+""+a.y+""+a.z); 

     System.out.println("Values in Object a = "+b.x+""+b.y+""+b.z); 

     System.out.println("Point a has the bigger x-value"); 

     Point h = new Point(0,0,0);  

     if (a.x < b.x){ 
      h.x = b.x; 
     } 
     else{ 
      System.out.println("Point a has the bigger x-value"); 
      h.x = a.x; 
     } 

     if (a.y < b.y){ 
      System.out.println("Point b has the bigger y-value"); 
      h.y = b.y; 
     } 
     else{ 
      System.out.println("Point a has the bigger y-value"); 
      h.y = a.y; 
     } 

     if (a.z < b.z){ 
      System.out.println("Point b has the bigger z-value"+b.z); 
      h.z = b.z; 
     } 
     else{ 
      System.out.println("Point a has the bigger z-value"+a.z); 
      h.z = a.z; 
      } 

     return h; 

    } 

} 
0

あなたがオブジェクト上の属性のいずれかを取得したい場合は、次の2つの選択肢があります。getterメソッドを使用して

1 /: a.getAttributeX(ポイント){...}

2 /一度オブジェクトポイントを作成すると、次のように使用できます。 if(ax < bx)then ....

PS:ゲッターメソッドを使用するのは常に安全です!

+0

getメソッドは次のようになります(?): public static double getAttributeX(Point){ return a.x; } – Play4Fun

+0

もちろん、彼のクラスからは次のようになります:public double getx(){return x; } –

関連する問題