class hello {
public static void main(String[] args) {
b b = new b();
b.s(3,3,h);
}
}
class h {
public void print(int n) {
System.out.println(n);
}
public void say() {
print(5);
System.out.println("我是最帅的啊");
}
}
class b{
String name;
public void s(int col,int row,char c){
for(int i=0;i<row;i++){
for(int j=0;j System.out.print(c);
}
}
}
该回答引用ChatGPT
在代码 b.s(3,3,h); 中,你尝试将一个类 h 传递给了 s 方法,但是 s 方法的第三个参数期望的是一个 char 类型,而不是一个类。
如果你想传递一个实例 h,你需要先创建一个 h 对象,然后将其传递给 s 方法。你可以像下面这样修改代码:
class hello {
public static void main(String[] args) {
h h = new h(); // 创建一个 h 对象
b b = new b();
b.s(3, 3, 'h'); // 将字符 'h' 传递给 s 方法
h.say(); // 调用 h 对象的 say 方法
}
}
现在,代码会输出以下内容:
hhh
我是最帅的啊
其中,第一行是由 b.s 方法输出的,而第二行是由 h.say 方法输出的。
报什么错误?