java定义一个类Point,Point包含两个整型成员变量x,y,初值为0,两个成员方法 set()方法和print()方法。set()方法的作用是给成员变量x,y赋值,print()方法的作用是将x,y的值输出到屏幕。set()方法和 print()均没有返回值。
时间: 2024-04-08 22:34:11 浏览: 16
好的,以下是定义一个类Point的Java代码,其中包含两个整型成员变量x和y,初始值为0,以及set()和print()方法:
```java
public class Point {
private int x;
private int y;
public Point() {
this.x = 0;
this.y = 0;
}
public void set(int newX, int newY) {
x = newX;
y = newY;
}
public void print() {
System.out.println("x = " + x + ", y = " + y);
}
}
```
你可以通过创建Point类的对象并调用其set()和print()方法来设置和打印成员变量的值。例如:
```java
Point p = new Point();
p.set(2, 3);
p.print(); // 输出结果:x = 2, y = 3
```
请注意,这里的代码是根据你的要求编写的,如果你有其他需求或者想要添加更多功能,请告诉我。
相关问题
用java 定义一个新类“Point”,包含两个整型字段成员“x”和“y”。
好的,以下是定义一个新类“Point”的Java代码:
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
定义一个新类“Point”,包含两个整型字段成员“x”和“y”。
好的,以下是定义一个新类“Point”的代码:
```python
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
```
这个类包含两个整型字段成员“x”和“y”,并且有一个构造函数,可以用来初始化这两个字段。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)