研马原 2021-10-15 16:38 采纳率: 57.1%
浏览 53
已结题

Java构造方法,类与对象

img

  • 写回答

3条回答 默认 最新

  • 关注
    
    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;
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • FlyYant 2021-10-15 16:53
    关注
    
    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);
        }
    }
    
    评论
  • STRANG-P 2021-10-15 16:58
    关注
    
    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);
        }
    
    }
    
    
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月15日

悬赏问题

  • ¥20 Java的kafka错误unknowHostException
  • ¥20 gbase 8a没有lisense,需要获取一个lisense
  • ¥15 前端的3d饼图不知道用啥框架做的
  • ¥15 算法问题 斐波那契数 解答
  • ¥15 VS2019 SPY++ 获取句柄操作
  • ¥15 Facebook 获取广告
  • ¥15 PID算法的输出结果如何转换成pwm
  • ¥15 java文本解密算法
  • ¥15 有没有办法等sql查询完成后执行下一步操作
  • ¥15 android8以下机子自动连接指定wifi热点