class Rectangle1{
private double width;
public void setWidth(double width) {
this.width = width;
}
public void setHeight(double height) {
this.height = height;
}
private double height;
private int count;
double C(){
double C=(width+height)*2;
return C;
}
double S(){
double S=width*height;
return S;
}
Rectangle1(){
count++;
}
Rectangle1(double width,double height){
this.width=width;
this.height=height;
count++;
}
void show(){
System.out.println(this.width);
System.out.println(this.height);
}
public int getCount(){
return count;
}
static void count(){
Rectangle1 R=new Rectangle1();
System.out.println("对象个数为"+R.getCount());
}
Rectangle1 compare(Rectangle1 rectangle1,Rectangle1 rectangle2){
if (rectangle2.S()> rectangle1.S()){
return rectangle2;
}else {
return rectangle1;
}
}
}
public class newRectangle {
public static void main(String[] args) {
Rectangle1 R=new Rectangle1(2,6);
Rectangle1 R1=new Rectangle1();
R1.setHeight(24);
R1.setWidth(28);
System.out.println(R.S());
System.out.println(R1.S());
System.out.println(R.C());
System.out.println(R1.C());
System.out.println("面积大的为"+R.compare(R, R1));
System.out.println(R.getCount()+R1.getCount());
}
}
该题分别采用了有参构造法和无参构造法,调用时需要单独调用
this关键字的作用:可以用于指定访问当前对象的成员变量,成员方法