簡體   English   中英

有沒有一種方法可以重用我的部分代碼,這些代碼做同樣的事情但訪問 Java 類的不同屬性?

[英]Is there a way I can reuse part of my code that does the same thing but access different attributes of a class in Java?

所以我正在做一個程序,應該驗證切割時三角形是否可以產生兩個新三角形,我完成了所有邏輯並且代碼按預期工作,但是當我這樣做時我注意到我必須應用一遍又一遍地為不同的屬性使用相同的邏輯,這似乎非常沒有效率。 這是代碼:

public static class Triangle{
        private int[] x = new int[3];
        private int[] y = new int[3];
    
        public int getX(int position) {
            return x[position];
        }
        public int getY(int position) {
            return y[position];
        }
        
        //Get the information regarding the points that make the triangle
        public void setDomains(Scanner readVar){     
            readVar.nextLine();
            this.x[0] = readVar.nextInt();
            this.y[0] = readVar.nextInt();
            this.x[1] = readVar.nextInt();
            this.y[1] = readVar.nextInt();
            this.x[2] = readVar.nextInt();
            this.y[2] = readVar.nextInt();
            
        }

        //Verify if one of the other points is lower or higher than the present one
        public boolean areDiffX(int i, int j, boolean isLower){
 
            if(isLower){
                if(this.getX(i) < this.getX(j)){
                    return true;
                };
            }else{
                if(this.getX(i) > this.getX(j)){
                    return true;
                };
            }
     
            return false;
        }
        //Verify if one of the other points is lower or higher than the present one
        public boolean areDiffY(int i, int j, boolean isLower){
     
            if(isLower){
                if(this.getY(i) < this.getY(j)){
                    return true;
                };
            }else{
                if(this.getY(i) > this.getY(j)){
                    return true;
                };
            }
     
            return false;
        }
    
        //Verify if from the X axes two new triangles can ensurge
        public boolean compareTriangleDomain(int i, int j, int k){
    
            boolean hasDomain = false;
            boolean hasCodomain = false;
    
            hasDomain = areDiffX(i, j, true);
            hasCodomain = areDiffX(i, k, false);
            if(hasDomain == true && hasCodomain == true){
                return true;
            }
     
            hasDomain = areDiffX(i, j, false);
            hasCodomain = areDiffX(i, k, true);
            if(hasDomain == true && hasCodomain == true){
                return true;
            }
    
            return false;
        }
        
        //Verify if from the Y axes two new triangles can ensurge
        public boolean compareTriangleCodomain(int i, int j, int k){
    
            boolean hasDomain = false;
            boolean hasCodomain = false;
    
            hasDomain = areDiffY(i, j, true);
            hasCodomain = areDiffY(i, k, false);
            if(hasDomain == true && hasCodomain == true){
                return true;
            }
     
            hasDomain = areDiffY(i, j, false);
            hasCodomain = areDiffY(i, k, true);
            if(hasDomain == true && hasCodomain == true){
                return true;
            }
     
            return false;
        }
        
        //Verify if from a certain point of a triangle can be produced two new triangles
        public boolean cutTriangle(int i, int j, int k){
     
           boolean halveTriangleX = compareTriangleDomain(i, j, k);
           boolean halveTriangleY = compareTriangleCodomain(i, j, k);
           if(halveTriangleX || halveTriangleY){
                return true;
           }
           return false;
     
        }

        //Verify if a triangle can produce two new triangles
        public void checkCut(){

            boolean canCut = false;
            boolean hasntCut = true;

            canCut = cutTriangle(0, 1, 2);
            if(canCut == true && hasntCut == true){
                System.out.println("YES");
                hasntCut = false;
            }
            canCut = cutTriangle(1, 0, 2);
            if(canCut == true && hasntCut == true){
                System.out.println("YES");
                hasntCut = false;
            }
            canCut = cutTriangle(2, 1, 0);
            if(canCut == true && hasntCut == true){
                System.out.println("YES");
            }
            else if(hasntCut == true){
                System.out.println("NO");
            }
           
        }
    }

我想也許我可以向 get 函數添加一個新參數,以便指定我正在使用的軸,但我不知道這是否是一種愚蠢的方法,歡迎任何建議和建議我仍然Java 和一般編碼的嬰兒。

有幾種方法可以做到這一點。 我會簡單地編寫一個函數來獲取值而不是索引,然后是兩個獲取值的新函數:

public boolean areDiff(int a, int b, boolean isLower){
  if (isLower) {
    if (a < b) {
      return true;
    }
  } else {
    if (a > b) {
      return true;
    }
  }     
  return false;
}

接着:

public boolean areDiffX(int i, int j, boolean isLower){
   return areDiff(this.getX(i), this.getX(j), isLower);
}

和一個類似的areDiffY函數。

您可以重寫compareTriangleDomain以獲取值並使用areDiff並免除compareTriangleCodomainareDiffX/Y

也許我挖得太深了,但我想建議以下方法。

三角形顯然由平面上的三個點組成。 為什么不做一個類來代表點呢? 我將使用 Java 14 中出現的record語法。我還添加了兩個比較點的方法和兩個比較器。 一個允許您通過x坐標比較點,第二個允許您通過y比較點。

import java.util.Comparator;
import java.util.function.ToIntFunction;

public record Point2D(int x, int y) {

    private static Comparator<Point2D> compareBy(ToIntFunction<Point2D> keyExtractor) {
        return (o1, o2) -> Comparator.comparingInt(keyExtractor).compare(o1, o2);
    }

    public static Comparator<Point2D> byX = compareBy(Point2D::x);
    public static Comparator<Point2D> byY = compareBy(Point2D::y);

    public boolean less(Point2D other, Comparator<Point2D> comp) {
        return comp.compare(this, other) < 0;
    }

    public boolean higher(Point2D other, Comparator<Point2D> comp) {
        return comp.compare(this, other) > 0;
    }
}

現在我們可以大大簡化Triangle類。 三角形由三個點而不是六個整數的數組表示。

private final Point2D[] vertices = new Point2D[3];

public void setDomains(Scanner readVar) {
    readVar.nextLine();
    this.vertices[0] = new Point2D(readVar.nextInt(), readVar.nextInt());
    this.vertices[1] = new Point2D(readVar.nextInt(), readVar.nextInt());
    this.vertices[2] = new Point2D(readVar.nextInt(), readVar.nextInt());
}

所有其他方法看起來像這樣

public boolean compareTriangle(int i, int j, int k, Comparator<Point2D> comp) {  
    boolean hasDomain;                                                             
    boolean hasCodomain;                                                           
                                                                                   
    hasDomain = vertices[i].less(vertices[j], comp);                               
    hasCodomain = vertices[i].higher(vertices[k], comp);                           
    if (hasDomain && hasCodomain) {                                                
        return true;                                                               
    }                                                                              
                                                                                   
    hasDomain = vertices[i].higher(vertices[j], comp);                             
    hasCodomain = vertices[i].less(vertices[k], comp);                             
    return hasDomain && hasCodomain;                                               
}                                                                                  
                                                                                   
public boolean cutTriangle(int i, int j, int k) {                                  
    boolean halveTriangleX = compareTriangle(i, j, k, Point2D.byX);              
    boolean halveTriangleY = compareTriangle(i, j, k, Point2D.byY);              
    return halveTriangleX || halveTriangleY;                                       
}                                                                                  

我沒有測試過這段代碼(我認為它與您的代碼相同),因此您一定要檢查一下。

暫無
暫無

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

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