有下列程序,试指出该程序的错误之处。 class AA{ AA(int a){ System.out.println("a="+a); } } class BB extends AA{ BB(String s){ System.out.println("s = "+s); } } public class ConstructorDemo{ public static void main(String[] args){ var b = new BB("hello"); } }
时间: 2023-11-27 21:54:28 浏览: 41
该程序的错误在于子类 BB 没有显式地调用父类 AA 的构造方法,而父类 AA 又没有无参构造方法,因此编译器会自动添加一个无参构造方法,在运行时会报错。要解决这个问题,可以在子类 BB 的构造方法中使用 super 调用父类 AA 的构造方法。修正后的程序如下:
```
class AA {
AA(int a) {
System.out.println("a=" + a);
}
}
class BB extends AA {
BB(String s) {
super(0); // 调用父类 AA 的构造方法
System.out.println("s = " + s);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
var b = new BB("hello");
}
}
```
相关问题
public class WrapperTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int aa = sc.nextInt(); String bb = sc.next(); int c = sc.nextInt(); String str11 = sc.next(); String str22 = sc.next(); // 包装类中“==”与equals的用法比较 // 值得注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址, // 而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。 /********* Begin *********/ if(aa==bb) System.out.println("true"); else System.out.println("false"); if(aa==c) System.out.println("true"); else System.out.println("false"); if(bb==c) System.out.println("true"); else System.out.println("false"); if(aa.equals(bb)) System.out.println("true"); else System.out.println("false"); if(str11==str22) System.out.println("true"); else System.out.println("false"); if(str11.equals(str22)) System.out.println("true"); else System.out.println("false"); /********* End *********/ } }改正代码
代码中有几个错误,以下是改正后的代码:
```
public class WrapperTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int aa = sc.nextInt();
String bb = sc.next();
int c = sc.nextInt();
String str11 = sc.next();
String str22 = sc.next();
// 包装类中“==”与equals的用法比较
// 值得注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址,
// 而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。
/********* Begin *********/
if(aa==Integer.parseInt(bb)) // 需要将字符串转换为int类型再进行比较
System.out.println("true");
else
System.out.println("false");
if(aa==c)
System.out.println("true");
else
System.out.println("false");
if(Integer.parseInt(bb)==c) // 同样需要将字符串转换为int类型再进行比较
System.out.println("true");
else
System.out.println("false");
if(Integer.toString(aa).equals(bb)) // 需要将int类型转换为字符串再进行比较
System.out.println("true");
else
System.out.println("false");
if(str11==str22)
System.out.println("true");
else
System.out.println("false");
if(str11.equals(str22))
System.out.println("true");
else
System.out.println("false");
/********* End *********/
}
}
```
class A{ int m; int getM(){ return m; } int seeM() { return m; } } class B extends A { int m; int getM() { return m+ 80; } }public class E { public static void main(String[] args) i Bb = new B(); b.m = 20; System.out.print1n(b.getM());Aa = b; a.m = -100; system.out.println(a.getM());system.out.println(b.seeM()); } }运行结果
代码存在一些语法错误,我先对代码进行修正,修改后的代码如下:
```
class A {
int m;
int getM() {
return m;
}
int seeM() {
return m;
}
}
class B extends A {
int m;
int getM() {
return m + 80;
}
}
public class E {
public static void main(String[] args) {
B b = new B();
b.m = 20;
System.out.println(b.getM());
A a = b;
a.m = -100;
System.out.println(a.getM());
System.out.println(b.seeM());
}
}
```
运行结果为:
```
100
-100
20
```
解析:
1. 创建一个 B 类的实例 b,将其 m 属性赋值为 20。
2. 调用 b 对象的 getM() 方法,由于 B 类重写了 getM() 方法,因此调用的是 B 类中的 getM() 方法。该方法返回 m + 80 = 20 + 80 = 100。
3. 将 B 类的实例 b 赋值给 A 类的引用 a,由于 b 是 B 类的实例,因此 a 引用的是 B 类的实例。
4. 修改 a 对象的 m 属性的值为 -100。
5. 调用 a 对象的 getM() 方法,由于 B 类重写了 getM() 方法,但是 a 引用的是 B 类的实例,因此调用的是 B 类中的 getM() 方法。该方法返回 m + 80 = -100 + 80 = -20。
6. 调用 b 对象的 seeM() 方法,该方法返回 m 属性的值,即 20。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)