public class exereisel{ intx,y; public exercisel (){} public exercisel (int x,int y){ —————————————————— } public exercisel (exercisel p){ —————————————————— } public exercise1 getLocation ( ){ exercisel p=______; //实例exercise1 对象 p,其坐标是(x,y) _______//返回对象 p } public String toString (){ return " (" +x+"," +y+")"; } public static void main ( String ] args ){ exercisel p=___________; System. out. println ("x=”+p.x+" y=" +p.y); System. out. println (" Location is" +p. getlocation () ); }
时间: 2023-09-27 20:06:52 浏览: 59
这是一段 Java 代码,其中定义了一个名为 `exercisel` 的类,类中包含了三个构造方法和一个 `getLocation()` 方法以及一个 `toString()` 方法。
在其中第二个构造方法中,应该有两个参数 x 和 y,用于初始化对象的坐标。在第三个构造方法中,应该有一个参数 p,表示另一个 `exercisel` 对象,用于使用另一个对象的坐标初始化当前对象。
在 `getLocation()` 方法中,应该先创建一个 `exercise1` 对象 p,并将其坐标设置为当前对象的坐标。然后返回这个 `p` 对象。
在 `main` 方法中,应该创建一个 `exercisel` 对象 p,并将其坐标初始化为 (x,y)。然后分别打印出对象的坐标和位置信息。
相关问题
补全public class exereisel{ intx,y; public exercisel (){} public exercisel (int x,int y){ —————————————————— } public exercisel (exercisel p){ —————————————————— } public exercise1 getLocation ( ){ exercisel p=______; //实例exercise1 对象 p,其坐标是(x,y) _______//返回对象 p } public String toString (){ return " (" +x+"," +y+")"; } public static void main ( String ] args ){ exercisel p=___________; System. out. println ("x=”+p.x+" y=" +p.y); System. out. println (" Location is" +p. getlocation () ); }
好的,这里是完整的代码:
```
public class exercisel {
int x, y;
public exercisel() {}
public exercisel(int x, int y) {
this.x = x;
this.y = y;
}
public exercisel(exercisel p) {
this.x = p.x;
this.y = p.y;
}
public exercise1 getLocation() {
exercise1 p = new exercise1();
p.x = this.x;
p.y = this.y;
return p;
}
public String toString() {
return " (" + x + "," + y + ")";
}
public static void main(String[] args) {
exercisel p = new exercisel(1, 2);
System.out.println("x=" + p.x + " y=" + p.y);
System.out.println("Location is" + p.getLocation());
}
}
```
其中,`exercise1` 类没有给出,你需要根据上下文自行推测其定义。
public PrimeSplit(int x)怎么构造方法还可以这样?
`PrimeSplit` 是一个类名,`public PrimeSplit(int x)` 是它的构造方法。构造方法是一种特殊的方法,在创建对象时被调用,用于初始化对象的属性。它的定义通常像这样:
```java
public class ClassName {
public ClassName() {
// 构造方法体
}
}
```
其中,`ClassName` 是类名,与构造方法名相同,没有返回类型,可以带有参数。在你提到的 `PrimeSplit` 类中,它的构造方法带有一个 `int` 类型的参数 `x`,表示要分解质因数的数。因此,可以使用 `new PrimeSplit(10)` 的方式创建一个 `PrimeSplit` 的对象,并将参数 `10` 传递给它的构造方法,在构造方法中对对象进行初始化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)