设计一个名为MyInteger类。这个类:4 一个名为 value的int 型成员变量,存储这个对象表示的 int 值: 一个为指定的int 值创建MyInteger 对象的构造方法; 一个返回int值 的gt方法: 如果值为偶数,则isEven()返回true, 如果值为奇数,则isOdd ()返回true, 否则返回 false;否则返回 false:4重写equals(MyInteger i),如果该对象的值与参数相等,则返回true,否则返回false。
时间: 2024-02-05 12:11:06 浏览: 36
下面是一个实现MyInteger类的代码示例:
```
public class MyInteger {
private int value;
public MyInteger(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public boolean isEven() {
return value % 2 == 0;
}
public boolean isOdd() {
return value % 2 != 0;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MyInteger) {
MyInteger other = (MyInteger) obj;
return this.value == other.value;
}
return false;
}
}
```
这个类有一个value成员变量,存储这个对象表示的int值。构造方法接受一个int值,创建一个新的MyInteger对象。getValue方法返回这个对象的int值。isEven方法检查值是否为偶数,isOdd方法检查值是否为奇数。equals方法重写了Object类的equals方法,比较两个MyInteger对象的值是否相等。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)