如下代码class Super { publiclntegergetLenght(){ return new Integer(4); )}public class Sub extends Super (publicLonggetLenght()( return new Long(5); 3public static void main(Stringl args)(Super sooper = new Super();Sub sub = new Sub();System.out.println(sooper.getLenght().toString() + ".sub.getLenght().toString() )}}输出是什么
时间: 2023-11-30 14:02:33 浏览: 60
代码存在语法错误,需要进行修正,修正后的代码如下:
```
class Super {
public Integer getLength() {
return new Integer(4);
}
}
public class Sub extends Super {
public Long getLength() {
return new Long(5);
}
public static void main(String[] args) {
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(sooper.getLength().toString() + ", " + sub.getLength().toString());
}
}
```
输出结果为:`4, 5`。因为 `sooper.getLength()` 调用的是父类 `Super` 的 `getLength()` 方法,返回值为 `4`;`sub.getLength()` 调用的是子类 `Sub` 的 `getLength()` 方法,返回值为 `5`。
相关问题
class Super{ public Integer getLenght() { return new Integer(4); } } public class Sub extends Super{ public static void main(String[] args) { Super sooper = new Super(); Sub sub = new Sub(); System.out.println(sooper.getLenght().toString() + "," + sub.getLenght().toString()); } public Long getLenght() { return new Long(5); } }
这段代码中,父类Super定义了一个返回Integer类型的getLength()方法,而子类Sub重写了这个方法并返回了Long类型。在main方法中,创建了一个Super类对象sooper和一个Sub类对象sub,并分别调用它们的getLength()方法并输出结果。由于Java的多态性,调用sooper.getLenght()方法时会执行父类的方法并返回Integer类型的4,而调用sub.getLenght()方法时会执行子类的方法并返回Long类型的5。因此,输出结果为"4,5"。
class Super { public int getLength() { return 4; } } public class Sub extends Super { public long getLength() { return 5; } public static void main (String[]args) { Super sooper = new Super (); Super sub = new Sub(); System.out.printIn(sooper.getLength()+ “,” + sub.getLength() }; }
这段代码有语法错误,应该将最后一行的 "}" 改为 ")",并将 printIn 改为 println。另外,在 Sub 类中,getLength 方法的返回值类型应该与父类的 getLength 方法的返回值类型一致,即都为 int 或都为 long。如果返回值类型不一致,会编译出错。如果将 Sub 类中的 getLength 方法的返回值类型改为 int,则输出结果为 "4,4";如果将其改为 long,则输出结果为 "4,5"。这是因为在 main 方法中,sooper.getLength() 调用的是 Super 类中的 getLength 方法,其返回值为 4;而 sub.getLength() 调用的是 Sub 类中的 getLength 方法,其返回值为 5。