class A{ static{ System.out.print("A"); } A(){ System.out.print("a"); } } class B extends A{ static{ System.out.print("B"); } B(){ System.out.print("b"); } public static void main(String[] args) { A a = new B(); } } 2分 A、ABab B、AaBb C、BAba D、BbAa
时间: 2024-04-16 21:25:54 浏览: 13
答案是 C、BAba。首先,在Java中,静态代码块会在类加载时执行,而构造方法则是在创建对象时执行。
当执行`A a = new B();`时,首先会加载类A,执行类A的静态代码块,输出"A"。然后创建类B的对象,因为B是A的子类,所以会先执行A的构造方法,输出"a"。接着执行类B的静态代码块,输出"B"。最后执行B的构造方法,输出"b"。
因此,最终的输出结果为"BAba"。
相关问题
将以下程序段重新排列,使其成为可以编译与执行的程序,并能输出以下结果: % java ExTestDrive yes thaws % java ExTestDrive no throws 程序段: 1. System.out.print(r); 2. try{ 3. System.out.print(t); 4. doRisky(test); 5. }finally{ 6. System.out.println(s); 7. System.out.print(o); 8. class MyEx extends Exception{} public class ExTestDrive{ 9. System.out.print(w); 10.if(yes.equals(t)){ 11.System.out.print(a);12.throw new MyEx();13.}catch(MyEx e{14.static void doRisky(String t) throw MyEx{ System.out.print(h); 15.public static void main(String[] args){ String test = args[0];
以下是重新排列后的程序段:
1. class MyEx extends Exception{}
2.
3. public static void main(String[] args) {
4. String test = args[0];
5. try {
6. System.out.print("t");
7. doRisky(test);
8. System.out.print("o");
9. } catch (MyEx e) {
10. System.out.print("a");
11. } finally {
12. System.out.print("s");
13. System.out.print("h");
14. System.out.print("w");
15. }
16. }
17.
18. static void doRisky(String t) throws MyEx {
19. System.out.print("h");
20. if ("yes".equals(t)) {
21. throw new MyEx();
22. }
23. System.out.print("r");
24. }
输出结果:
- 当运行 "java ExTestDrive yes thaws" 时,输出 "thawsarshw"
- 当运行 "java ExTestDrive no throws" 时,输出 "throwsorshw"
将以下程序段重新排列,使其成为可以编译与执行的程序,并能输出以下结果: % java ExTestDrive yes thaws % java ExTestDrive no throws 程序段: 1. System.out.print(r); 2. try{ 3. System.out.print(t); 4. doRisky(test); 5. }finally{ 6. System.out.println(s); 7. System.out.print(o); 8. class MyEx extends Exception{} public class ExTestDrive{ 9. System.out.print(w); 10.if(yes.equals(t)){ 11.System.out.print(a); 12.throw new MyEx(); 13.}catch(MyEx e{ 14.static void doRisky(String t) throw MyEx{ System.out.print(h); 15.public static void main(String[] args){ String test = args[0];
以下是重新排列后的程序段:
1. class MyEx extends Exception{}
2. public static void main(String[] args){
3. String test = args[0];
4. String yes = "yes";
5. String no = "no";
6. String t = "throws";
7. String s = "thaws";
8. String w = "w";
9. String a = "a";
10. String h = "h";
11. String r = "";
12. String o = "o";
13. try{
14. System.out.print(w);
15. if(yes.equals(test)){
16. System.out.print(a);
17. throw new MyEx();
18. }else if(no.equals(test)){
19. System.out.print(o);
20. throw new Exception();
21. }
22. System.out.print(r);
23. doRisky(test);
24. }catch(MyEx e){
25. System.out.print(t);
26. }catch(Exception e){
27. System.out.print(s);
28. }finally{
29. System.out.print(o);
30. }
31.}
32. static void doRisky(String t) throws MyEx{
33. System.out.print(h);
34. }
输出结果:
若执行命令 "java ExTestDrive yes thaws",输出结果为 "wathrowso";
若执行命令 "java ExTestDrive no throws",输出结果为 "wothrowso"。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)