Advertisement
KuoHsiangYu

Java語言_以類別為藍圖建立物件程式碼

May 19th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. package com.sample;
  2.  
  3. import static java.lang.System.out;
  4.  
  5. class Boy {
  6.    
  7.     public Boy() {
  8.         out.println("Boy constructor one");
  9.     }
  10.  
  11.     public Boy(int n) {
  12.         out.println("Boy constructor two, n = " + n);
  13.     }
  14.  
  15.     public void hello(String name) {
  16.         out.println(name + " say hello method.");
  17.     }
  18. }
  19.  
  20. public class JavaProject21 {
  21.    
  22.     public static void main(String[] args) {
  23.        
  24.         Boy boy1 = new Boy();
  25.         boy1.hello("boy1");
  26.         out.print("\n");
  27.        
  28.         Boy boy2 = new Boy(16);
  29.         boy2.hello("boy2");
  30.         out.print("\n");
  31.        
  32.         Boy boy3 = null;
  33.         boy3 = new Boy(20);
  34.         boy3.hello("boy3");
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement