System.out.println(\ }
{ System.out.println(\ static { System.out.println(\}
public class HelloB extends HelloA { public HelloB() {
System.out.println(\ }
{ System.out.println(\ static { System.out.println(\ public static void main(String[] args) { new HelloB(); } }
结果: static A static B I'm A class HelloA I'm B class HelloB
5. 阅读下列程序,请写出运行结果。 class Father{ public void show(){ System.out.println(\ } }
class son extends Father{ public void show(){ System.out.println(\ } }
public class Sample1 { public static void main(String[] args) { Father f1 = new son(); Father f2 = new Father(); f1.show(); f2.show(); } }
结果: sun show...
father show...
6. 阅读下列程序,请写出运行结果。 public class Sample2 { String str = new String(\ char[] ch = { 'a', 'b', 'c' }; public static void main(String[] args) { Sample2 s2 = new Sample2(); s2.change(s2.str, s2.ch);
System.out.print(s2.str + \ System.out.print(s2.ch); } public void change(String str, char ch[]) { str = \ ch[0] = 'g'; } }
结果: ok and gbc
7. 阅读下列程序,请写出运行结果。 public class StaticTest { static int x=1; int y; StaticTest() { y++; } public static void main(String[] args) { StaticTest st=new StaticTest(); System.out.println(\ System.out.println(\ st=new StaticTest(); System.out.println(\ } static { x++;} }
结果: x=2 st.y=1 st.y=1
8. 阅读下列程序,请写出结果: class Meal{ Meal(){System.out.println(\}
class Bread{ Bread(){System.out.println(\
}
class Lunch extends Meal{ Lunch(){System.out.println(\}
class PortableLunch extends Lunch{ PortableLunch(){System.out.println(\}
public class Sandwich extends PortableLunch{ private Bread b = new Bread(); public Sandwich(){ System.out.println(\ } public static void main(String[] args) { new Sandwich(); } }
结果: Meal() Lunch()
PortableLunch() Bread() Sandwich()
9. 阅读下列程序,请写出结果: abstract class Base{ public Base(int i){ System.out.println(\ } public abstract void f(); }
public class AnonymousConstructor { public static Base getBase(int i){ return new Base(i){ public void f() { System.out.println(\ } }; } public static void main(String[] args) { Base base = getBase(40); base.f(); } }
结果:
Base(),i=40 f()
10. 阅读下列程序,请写出结果:
class ThreeException extends Exception{}; public class SampleException { static int count = 0; public static void main(String[] args) { while(true){ try{ if(count++ == 0) throw new ThreeException(); System.out.println(\ }catch(ThreeException ex){ System.out.println(\ }finally{ System.out.println(\ if(count == 2) break; } } } }
结果:
ThreeException finally
No Exception. finally
三、编程题 1)
? 创建两个带有默认构造方法(空参数列表)的类ClassA和类ClassB。 ? 从ClassA中继承产生一个类名为ClassC的新类,并在ClassC内创建一个ClassB
类的成员。
? 不要给ClassC编写构造方法。创建一个ClassC类的对象。
2)
? 编写具有两个(重载)构造方法的类,并在第一个构造方法中通过this调用第二个
构造方法。
? 类名为Employee(员工)。
? 可以依员工名创建Employee对象。
? 可以依员工名、联系方式创建Employee对象。 ? main方法里实例化Employee对象。
3)
? 创建一个类,然后创建一个用你的类的对象进行过初始化的List.
? 通过使用subList()方法,创建你的List的子集,然后在你的List中移除这个子集。 4)
? 编写一个类,在其main()方法里try块里抛出一个Exception类的对象。传递一个
字符串参数给Exception的构造方法。在catch里捕获此异常对象,并且打印字符串参数。增加一个finally子句,打印一条信息以证明这里确实得到了执行。