[教材P105 3-36、3-37(给出java代码和运行效果示意图)] 实现颜色类和像素类,要求像素类重写toString方法,以 [x,y,(r,g,b)]格式返回像素对象的信息
时间: 2024-02-11 21:06:57 浏览: 110
颜色类:
```java
public class Color {
private int red;
private int green;
private int blue;
public Color(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed() {
return red;
}
public int getGreen() {
return green;
}
public int getBlue() {
return blue;
}
}
```
像素类:
```java
public class Pixel {
private int x;
private int y;
private Color color;
public Pixel(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Color getColor() {
return color;
}
@Override
public String toString() {
return "[" + x + "," + y + "," + "(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ")]";
}
}
```
测试代码:
```java
public class TestPixel {
public static void main(String[] args) {
Color color = new Color(255, 0, 0);
Pixel pixel = new Pixel(10, 20, color);
System.out.println(pixel.toString());
}
}
```
输出结果:
```
[10,20,(255,0,0)]
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)