Advertisement
KuoHsiangYu

OCA16_2.java

Feb 5th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. class A {
  2.  
  3.     static String s = "AAA";
  4.  
  5.     A() {
  6.         System.out.println("#5 class A constructor");
  7.     }
  8.  
  9.     static {
  10.         System.out.println("#1 class A static {}");
  11.         s = s + "BBB";
  12.     }
  13.  
  14.     {
  15.         System.out.println("#2 class A {}");
  16.         s = "AAABBB";
  17.         //s = "Hello world";
  18.     }
  19. }
  20.  
  21. class B extends A {
  22.  
  23.     B() {
  24.         System.out.println("#6 class B constructor");
  25.     }
  26.  
  27.     static {
  28.         System.out.println("#3 class B static {}");
  29.         s = s + "BBBAAA";
  30.     }
  31.  
  32.     {
  33.         System.out.println("#4 class B {}");
  34.         System.out.println("s = " + s);
  35.     }
  36. }
  37.  
  38. public class OCA16 {
  39.  
  40.     public static void main(String[] args) {
  41.         B b = new B();
  42.     }
  43. }
  44.  
  45. /*
  46. NetBeans IDE 8.2
  47. run:
  48. #1 class A static {}
  49. #3 class B static {}
  50. #2 class A {}
  51. #5 class A constructor
  52. #4 class B {}
  53. s = AAABBB
  54. #6 class B constructor
  55. BUILD SUCCESSFUL (total time: 0 seconds)
  56. */
  57.  
  58. /*
  59. S M Athiqur Rahman‎C , C++ , C# , Java, SQL , VB. net, Python , MVC , Web Programming
  60. 11 小時 ·
  61.  
  62. Why answer is : AAABBB? any explanation from experts...
  63. https://www.facebook.com/photo.php?fbid=2340845999536149&set=gm.2489663694438490&type=3&theater&ifg=1
  64.  
  65. 郭翔宇
  66. 6 小時
  67. 大家好,小弟是程式初學者。
  68. 最近在其他facebook社團看見有人詢問Java相關問題。
  69.  
  70. 設一個 類別A,再設一個 類別B,
  71. 在 類別A 裡面宣告一個 static變數,
  72. 然後分別在 static{} 跟 {} 裡面修改變數值。
  73. 接著 類別B 繼承 類別A。
  74.  
  75. 1.
  76. 請問各位,
  77. 程式執行出來的結果 "AAABBB" 是怎麼生出來的?
  78.  
  79. 2.
  80. static{} 跟 {} 到底差在哪裡?
  81. 查詢這些東西的 google搜尋關鍵字 是?
  82.  
  83. 附上 Java程式碼
  84. https://www.facebook.com/groups/1403852566495675/permalink/2263345963879660/
  85.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement