1 package com.hanqi;
 2 
 3 //父类
 4 public class Father {
 5 
 6 //    public Father()
 7 //    {
 8 //        
 9 //    }
10 //    
11     public Father(String name)
12     {
13         this.name = name;
14         System.out.println("调用了父类的构造方法");
15     }
16     
17     //名字
18     
19     private String name;
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28     
29     //工作
30     
31     public void work()
32     {
33         System.out.println("我是"+name+"   我劳动,我光荣!");
34     }
35     
36     private void siFangQian()
37     {
38         System.out.println("存点私房钱");
39     }
40     
41     
42     
43     
44 }

 

 

 

 1 package com.hanqi;
 2 
 3 //子类 继承了父类的方法
 4 public class Son extends Father{
 5 
 6     
 7     public Son(String name)
 8     {
 9         //自动调用父类的构造方法
10         //如果父类没有默认构造方法,就要显示调用
11         super(name); // this代表自己 super代表上一级
12         
13         
14         System.out.println("调用了子类的构造方法");
15     }
16     
17     //唱歌
18     public void Sing()
19     {
20         System.out.println("喜欢唱歌");
21     }
22     
23     //重写 覆盖
24     @Override
25     public void work()
26     {
27         super.work();
28         //System.out.println("我不喜欢上班,我想唱歌。");
29         System.out.println("边上班边唱歌");
30         
31     }
32     
33     public void work(String str)
34     {
35         
36     }
37     
38     

测试

 

 1 package com.hanqi;
 2 
 3 public class Test14 {
 4 
 5     public static void main(String[] args) {
 6 
 7 
 8         Son s = new Son("儿子");
 9         
10         //s.setName("儿子");
11         
12         s.work();
13 
14         s.Sing();
15     }
16 
17 }

 

 

posted on 2016-05-23 15:53  周帝  阅读(483)  评论(0编辑  收藏  举报