class Super1 { public int I = 0;
public Super1(String text) { I = 1; } }
public class Sub extends Super1 { public Sub(String text){ I= 2; }
public static void main (String args[]) { Sub sub = new Sub ("Hello"); System.out.println(sub.I); } }
译时出现如下错误: Sub.java:10: Super1(java.lang.String) in Super1 cannot be applied to () public Sub(String text){
我知道是错在子类在掉用父类的构造函数的时候找不到默认的构造函数,可是我想请问
子类一定要调用父类的函数么?这道题里面什么地方调用父类的构造函数?
下面这个程序不是也没调用父类的构造函数么?为什么编译时不会出错呢?
import java.applet.*;
import java.awt.*;
class Father
{
private int money;
float weight,height;
String head;
String speak(String s)
{
return s;
}
}
class Son extends Father
{
String hand,foot;
}
public class Example4_6 extends Applet
{
Son boy;
public void init()
{
boy=new Son();
boy.weight=1.80f;boy.height=120f;
boy.head="一个头";boy.hand="两只手";boy.foot="两只脚";
}
public void paint(Graphics g)
{
g.drawString(boy.speak("我是儿子"),5,20);
g.drawString(boy.hand+boy.foot+boy.head+boy.weight+boy.height,5,40);
}
}