Java构造方法,类与对象

img


public class Test {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(30, 40);
        double computCircum = rectangle.computCircum();
        System.out.println("computCircum = " + computCircum);
        double computArea = rectangle.computArea();
        System.out.println("computArea = " + computArea);
    }
}


class Rectangle {
    private double width;
    private double height;

    public Rectangle() {
    }

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double computCircum() {
        return (width + height) * 2;
    }

    public double computArea() {
        return width * height;
    }
}


class test  
{
    public static void main (String[] args) 
    {
        Rectangle rectangle = new Rectangle(30,40);
        rectangle.computCircum();
        rectangle.computArea();
    }
}
class Rectangle{
    private double width;
    private double height;
    
    public Rectangle(double _width,double _height){
        this.width=_width;
        this.height=_height;
    }
    
    public void computCircum(){
        System.out.println((width+height)*2);
    }
    public void computArea(){
        System.out.println(width*height);
    }
}

public class Rectangle {
 
 private double width;

 private double height;

Rectangle (double _width, double _height) {
   this.width = _width;
   this.height = _height;
}

Rectangle () {
}

public double computArea() {
    return width*height;
}

public double computCircum() {
    return (width + height)*2;
}

}

public class RectangleTest { 

   public static void main(String[] args) {
       Rectangle rectangle = new Rectangle (30, 40);
       // 计算面积
        double area = rectangle.computArea();
        System.out.println("面积为:"+ area);
       // 计算周长
       double circum= rectangle.computCircum();
       System.out.println("周长为:"+ circum);
    }

}