1 package a;
2
3 public class Father
4 {
5 private int age;
6 public String name;
7
8 public Father(int age,String name)
9 {
10 this.age=age;
11 this.name=name;
12 }
13
14 public int getage()
15 {
16 return this.age;
17 }
18
19 public int setage(int age)
20 {
21 return this.age=age;
22
23 }
24
25 public void gongzuo()
26 {
27 System.out.println("爸爸工作");
28 }
29
30 public void kaiche()
31 {
32 System.out.println("爸爸开车");
33 }
34
35
36
37 }
1 package a;
2
3 public class Son
4 {
5 protected int age;
6 public String name;
7
8 private void wan()
9 {
10 System.out.println("儿子玩");
11 }
12
13 public void xuexi()
14 {
15 System.out.println("儿子学习");
16 }
17 }
1 package b;
2
3 import a.Father;
4
5 public class Text {
6
7 public static void main(String[] args)
8 {
9 Father father1=new Father(40,"大明");
10
11 father1.gongzuo();
12 father1.kaiche();
13 father1.setage(9);
14 System.out.println(father1.getage());
15 System.out.println(father1.name);
16 }
17
18 }
19 /*
20 在包a中编写一个类Father,具有属性:年龄(私有)、姓名(公有);
21 具有功能:工作(公有)、开车(公有)。
22 在包a中编写一个子类Son,具有属性:年龄(受保护的)、姓名;
23 具有功能:玩(私有)、学习(公有)。
24 最后在包b中编写主类Test,在主类的main方法中测试类Father与类Son。
25 */