声明一个像素类继承point java
时间: 2024-06-01 18:10:51 浏览: 12
public class Pixel extends Point {
private int color;
public Pixel(int x, int y, int color) {
super(x, y);
this.color = color;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
相关问题
java定义一个泛型类point
下面是一个定义了泛型类型参数的 Point 类的 Java 代码:
```java
public class Point<T> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {
return x;
}
public T getY() {
return y;
}
public void setX(T x) {
this.x = x;
}
public void setY(T y) {
this.y = y;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
```
这个类通过使用类型参数 `T` 来表示 `x` 和 `y` 的类型。在创建 `Point` 对象时,需要指定类型参数的具体类型,例如:
```java
Point<Integer> intPoint = new Point<>(1, 2);
Point<Double> doublePoint = new Point<>(3.4, 5.6);
```
这样,`intPoint` 对象的 `x` 和 `y` 都是整数类型,而 `doublePoint` 对象的 `x` 和 `y` 都是双精度浮点数类型。
java定义一个点类point
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double distance(Point other) {
double dx = this.x - other.x;
double dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)