1.先建一个长方形的类Rectangle
public class Rectangle { //类 private int heigth; private int width ; public int girth(){ //周长 //方法,有返回值,int/void return 2 * ( heigth + width ); } public int area(){ //面积 return heigth * width; } public Rectangle(){ //构造器,这是无参 this.heigth = 1; this.width = 1; } //原来谁的属性被私有化(private)了,后来就公开(public)谁的属性 //没有参数,从外界输入进来的值不能进入这里面 public Rectangle(int jieshouqi01){ //只能进去一个参数 this.heigth = jieshouqi01; this.width = jieshouqi01; } public Rectangle(int jieshouqi02 , int jieshouqi03){ //两个参数 this.heigth = jieshouqi02; this.width = jieshouqi03; } @Override public String toString() { return "Rectangle{" + "higth=" + heigth + ", width=" + width + '}'