0

三角形方法有 3 个实例变量。诠释侧1,侧2,侧3;

他们给了我需要制作为公共三角形的方法(int s1,int s2,int s3)

我是否将其声明为:

   public class triangle {

private int s1;
private int s2;
private int s3;

}

或者

  public class triangle (int s1, int s2, int s3) {

}

谢谢

我把这个和构造函数混在一起了。想通了,谢谢各位。

4

3 回答 3

1

首先,如果方法名与类名相同,constructor则在创建该类的新对象时调用它。

public class Triangle {
    private int s1; // This are the private variable which
    private int s2; // are accessed by only object of
    private int s3; // class triagnle.

    public class Triangle (int s1, int s2, int s3) // This is a constructor which is called
    {                                              // when you create a object with new keyword
        this.s1 = s1;                              // like Triangle t = new Triangle(1,2,3);
        this.s2 = s2;
        this.s3 = s3;
    }
}
于 2013-10-09T04:28:30.510 回答
0
public class triangle {

    int s1;
    int s2;
    int s3;

    public triangle (int s1, int s2, int s3) {
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;

    }
}

希望这可以帮助。谢谢

于 2013-10-09T04:23:54.243 回答
0

最佳做法是

public class Triangle {
 //atributes
 private int s1;
 private int s2;
 private int s3;

 //encapsulation
 public int getS1() {
        return s1;
    }
 public int getS2() {
        return s2;
    }
 public int getS3() {
        return s3;
    }
 public void getS1(int value){
        this.s1 = value;
    }
 public void getS2(int value){
        this.s2 = value;
    }
 public void getS3(int value){
        this.s3 = value;
    }
 //constructor
 public Triangle (int s1, int s2, int s3)
 {
  this.s1 = s1;
  this.s2 = s2;
  this.s3 = s3;
  //do something
 }
}
于 2013-10-09T04:30:41.927 回答