1.java异常捕捉输出
public class 面试题一 {
public static String output= "";
public static void foo(int i){
try {
if(i == 1){
throw new Exception();
//注释这一行 代码不会执行,并且下面的代码也不会执行 throw相当于return
}
output += "1";
} catch (Exception e) {
output +="2";
return ;//加了 return 异常 finally后面的代码也不会执行
}finally{
output +="3";
}
output +="4";
}
public static void main(String[] args) {
foo(0);//134
foo(1);//13423
System.out.println(output);
}
}
2.简单设计一个单例
//工具类(输出一个类)
public class Face_Singlelon {
private static Apple apple = null;
private Face_Singlelon(){
System.out.println("单例模式的私有构造方法,不能被外部类实例化");
}
//对外提供一个实例,并且一个类只能产生一个实例化对象
public static Apple getSingleInstance(){
if(apple == null ){
//创造苹果
Apple a = new Apple();
Face_Singlelon.apple = a;
}
return apple;
}
}
//输出类
public class Apple {
public Apple(){
System.out.println("苹果" );
}
}