1 public class car { 2 String color; 3 String pinpai; 4 double size; 5 }
1 class dianfanbao { 2 String pinpai; 3 double daxiao; 4 String color; 5 }
1 class student { 2 String name; 3 int age; 4 String sex; 5 }
1 class zonghe { 2 public static void main(String[] args) { 3 student stu = new student(); 4 stu.name = "張三"; 5 stu.age = 13; 6 stu.sex = "男"; 7 dianfanbao dfb = new dianfanbao(); 8 dfb.pinpai = "九陽"; 9 dfb.daxiao = 1.8; 10 dfb.color = "紅色"; 11 car ca = new car(); 12 ca.pinpai = "大衆"; 13 ca.color = "黑色"; 14 ca.size = 18; 15 System.out.println("學生的性別是:"+stu.sex); 16 System.out.println("學生的姓名是:"+stu.name); 17 System.out.println("學生的年齡是:"+stu.age); 18 System.out.println("車的品牌是:"+ca.pinpai); 19 System.out.println("車的顏色是:"+ca.color); 20 System.out.println("車的大小是:"+ca.size); 21 System.out.println("電飯煲品牌:"+dfb.pinpai); 22 System.out.println("電飯煲大小:"+dfb.daxiao); 23 System.out.println("電飯煲顏色:"+dfb.color); 24 } 25 }
以上實例封裝了3個類,而後在一個main方法內傳值並調用,由於咱們本身建立的類是在同一個文件夾下,因此沒有導入包這個步驟,若是調用的是java提供的類,就須要先導入包,而後在調用。java