代码1:
import java.io.*;
class Fu
{
public void myVoid() throws IOException
{
System.out.println("父类");
}
}
public class LianXil extends Fu
{
public void myVoid() throws Exception
{
System.out.println("子类");
}
public static void main(String args[])
{
}
}
代码2:
import java.io.*;
class Fu
{
public void myVoid() throws IOException
{
System.out.println("父类");
}
}
public class LianXil extends Fu
{
public void myVoid(String s) throws Exception
{
System.out.println("子类");
}
public static void main(String args[])
{
}
}
代码1运行的时候出错,说是子类重写了父类的myVoid方法,父类抛出异常IOException而子类抛出异常Exception,是 违反重写规则。所以出错。
但是 代码2 我觉得跟代码1 应该是一样的啊,为什么代码2能正常运行? |