- public class ch72 //主類別
- {
- public static void main(String args[]) //主方法(主函式)
- {
- Family man=new Family(); //於Family類別下,新增man這個物件
- Family woman=new Family();
- Family boy=new Family();
- Family girl=new Family();
-
- man.name="爸爸";
- man.age=40;
- man.hobby="爬山";
- man.income=50000;
-
- woman.name="媽媽";
- woman.age=37;
- woman.hobby="逛街";
- boy.name="男孩";
- boy.age=10;
- boy.hobby="打電動";
-
- girl.name="女孩";
- girl.age=8;
- girl.hobby="看書";
-
- System.out.println(man.name+"今年"+man.age+"歲,喜歡"+man.hobby+",月收入"+man.income+"元");
- System.out.println(woman.name+"今年"+woman.age+"歲,喜歡"+woman.hobby);
- System.out.println(boy.name+"今年"+boy.age+"歲,喜歡"+boy.hobby);
- System.out.println(girl.name+"今年"+girl.age+"歲,喜歡"+girl.hobby);
- }
- }
- class Family //定義 Family 類別, 並宣告該類別中的變數(屬性)
- {
- int age;
- String name;
- String hobby;
- int income;
- }
複製代碼 |