timi先生 2020-06-03 20:27 采纳率: 50%
浏览 98
已采纳

小白不懂就问系列,求兄台教我!

创建 Rectangle 类。此类的数据为 top、left、width、height。
创建尽可能多的 initialize(...) 方法

下面是一些示例:

  • 指定 4 个参数:left、top、width 和 height
  • 未指定 width/height(二者皆为 0)
  • 未指定 height(它等于 width),我们将创建一个正方形
  • 创建作为参数传递的另一个矩形的副本

Requirements:
1. 程序不得从键盘读取数据。
2. Rectangle 类必须包含 int 变量 top、left、width 和 height。
3. 该类必须至少包含一个 initialize 方法。
4. 该类必须至少包含两个 initialize 方法。
5. 该类必须至少包含三个 initialize 方法。
6. 该类必须至少包含四个 initialize 方法
我的代码如下:
public class Rectangle {
public int top;
public int left;
public int width;
public int height;
public void initialize(int top){
this.top = top;
}
public void initialize( int left){
this.left = left;
}
public void initialize(int width){
this.width = width;
}
public void initialize(int height){
this.height = height;
}

public static void main(String[] args) {


}

}
总是错的,这个该咋改,或者问题出在了哪!

  • 写回答

3条回答 默认 最新

  • syyyyyyyyyyyyyyh 2020-06-03 21:06
    关注

    如果是方法名报错的话,是因为他们的方法名以及参数都是相同的,所以不构成重载。你可以仿照构造函数,设置他们的方法参数分别为 1 2 3 4个。

    public int top;
        public int left;
        public int width;
        public int height;
        public void initialize1(int top){
        this.top = top;
        }
        public void initialize(int top,int left){
            this.top = top;
            this.left = left;
        }
        public void initialize(int top,int left,int width){
            this.top = top;
            this.left = left;
            this.width = width;
        }
        public void initialize(int top,int left,int width,int height){
            this.top = top;
            this.left = left;
            this.width = width;
            this.height = height;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 php的curl不支持http2
  • ¥15 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多