1 package a1; 2 3 public class Father { 4 /*在包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有); 5 具有功能:工作(公有)、开车(公有)。 6 在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名; 7 具有功能:玩(私有)、学习(公有)。 8 最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。*/ 9 10 public String Name; 11 private int age; 12 13 public int getAge() { 14 return age; 15 } 16 17 public void setAge(int age) { 18 this.age = age; 19 } 20 21 public Father(String name, int age) { 22 super(); 23 Name = name; 24 this.age = age; 25 } 26 //具有功能:工作(公有)、开车(公有)。 27 public void job() 28 { 29 System.out.println("父亲在工作"); 30 } 31 public void kaiche() 32 { 33 System.out.println("父亲在开车"); 34 } 35 public static void main(String[] args) { 36 //最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。 37 Father baba=new Father("李毅",48); 38 baba.setAge(48); 39 System.out.println("爸爸的年龄是:"+baba.getAge()); 40 baba.job(); 41 baba.kaiche(); 42 } 43 44 }
1 package a1; 2 3 public class Son { 4 // 在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名; 5 //具有功能:玩(私有)、学习(公有)。 6 protected int age; 7 String name; 8 private void wan() 9 { 10 System.out.println("儿子在玩"); 11 } 12 public void study() 13 { 14 System.out.println("儿子在学习"); 15 } 16 17 public Son(int age, String name) { 18 super(); 19 this.age = age; 20 this.name = name; 21 } 22 public static void main(String[] args) { 23 //最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。 24 Son son=new Son(18,"李易"); 25 son.name="李易"; 26 son.age=18; 27 son.wan(); 28 son.study(); 29 } 30 31 }