JAVA程序设计 阅读程序写出运行结果

阅读下列程序 写出运行结果 public class Fruit {
Fruit() {
System.out.println("Hello,This is a fruit!");
}
}

public class Apple extends Fruit {
Apple() {
System.out.println("Hello,The is an apple!");
}
public static void main(String[] args) {
Apple apple = new apple();
}

}
还有一题public class ExcSwqDcmo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="test";
boolean flag1=str.equals("malc");
boolean flag2=str.equals("fcmalc");
try{
System.out.println("T0");
if(!flag1||!flag2)
throw new GenderException();
System.out.println("T1");

}catch(GenderException e){
System.out.println("gender is wrong");
}catch(Exception){
System.out.println("T2");
}finally{
System.out.println("T3");
}
System.out.println("T4");
}

}

第一问
Hello,This is a fruit!
Hello,The is an apple!
因为子类构造方法中
Apple() {
System.out.println("Hello,The is an apple!");
}
在没有特定声明时等价于
Apple() {
super();//这里将执行父类的构造方法
System.out.println("Hello,The is an apple!");
}

第二问题
执行结果是
T0
gender is wrong
T3
T4
try{
System.out.println("T0");// 1这里始终会执行,一进到try就执行
if(!flag1||!flag2)
throw new GenderException();// 2、这里判断得到,当两个都为false时抛出异常,但没有打印信息
System.out.println("T1");

}catch(GenderException e){
System.out.println("gender is wrong");//3,步骤2抛出的异常在这里被截取,所以打印出来
}catch(Exception){
System.out.println("T2");
}finally{
System.out.println("T3");//4,try catch下 finally始终会执行,也打印
}
System.out.println("T4");// 5、try catch 外,继续执行
}

}
温馨提示:答案为网友推荐,仅供参考
相似回答