2023-03-07 java 定义长方形类,含:属性:宽、高(整型);方法:求周长、面积;构造方法3个:(1)无参-宽、高默认值为1;(2)1个参数-宽、高相等;(3)2个参数-宽、高各为宽,高

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 +
                '}';
    }

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 +
                '}';
    }

2.使用自构建的长方形类 Rectangle

@Test
public void test01(){
    System.out.println("请输入高和宽:");
    Scanner number = new Scanner(System.in);
    int H = number.nextInt();
    int W = number.nextInt();

    Rectangle number01 = new Rectangle(); //新取的这个类叫number01
    System.out.println("无参的周长是:"+(number01.girth())+",面积是:"+(number01.area()));
    System.out.println();


    Rectangle number02 = new Rectangle(H); //新取的这个类叫number02
    System.out.println("一个参数的周长是:"+(number02.girth())+",面积是:"+(number02.area()));
    System.out.println();

    Rectangle number03 = new Rectangle(H,W); //新取的这个类叫number03
    System.out.println("两个参数的周长是:"+(number03.girth())+",面积是:"+(number03.area()));
    System.out.println();
}
@Test
    public void test01(){
        System.out.println("请输入高和宽:");
        Scanner number = new Scanner(System.in);
        int H = number.nextInt();
        int W = number.nextInt();

        Rectangle number01 = new Rectangle(); //新取的这个类叫number01
        System.out.println("无参的周长是:"+(number01.girth())+",面积是:"+(number01.area()));
        System.out.println();


        Rectangle number02 = new Rectangle(H); //新取的这个类叫number02
        System.out.println("一个参数的周长是:"+(number02.girth())+",面积是:"+(number02.area()));
        System.out.println();

        Rectangle number03 = new Rectangle(H,W); //新取的这个类叫number03
        System.out.println("两个参数的周长是:"+(number03.girth())+",面积是:"+(number03.area()));
        System.out.println();
    }

你可能感兴趣的:(java,开发语言)