@Override重写

 1 package com.wisezone.f;
 2 //父类
 3 public class Person
 4 {
 5     //姓名
 6     private String name;
 7     //年龄
 8     private int age;
 9     
10     //空构造
11     public Person()
12     {
13         
14     }
15     
16     public String getName()
17     {
18         return name;
19     }
20     public void setName(String name)
21     {
22         this.name = name;
23     }
24     public int getAge()
25     {
26         return age;
27     }
28     public void setAge(int age)
29     {
30         this.age = age;
31     }
32     
33     //重写
34     public void eat(){
35         System.out.println("父哈哈吃");
36     }
37     
38 }    
 1 package com.wisezone.s;
 2 //子类
 3 import com.wisezone.f.Person;
 4 
 5 public class Student extends Person
 6 {
 7     //姓名
 8     private String name;
 9     //成绩
10     private double score;
11     
12     //提供一个空构造
13     public Student()
14     {
15         
16     }
17     
18     public String getName()
19     {
20         return name;
21     }
22     public void setName(String name)
23     {
24         this.name = name;
25     }
26     public double getScore()
27     {
28         return score;
29     }
30     public void setScore(double score)
31     {
32         this.score = score;
33     }
34     
35     //子类重写父类的属性和行为
36     @Override
37     public void eat(){
38         System.out.println("子哈哈吃");
39     }
40     
41 }
 1 package com.wisezone.test;
 2 //测试类
 3 import com.wisezone.s.Student;
 4 
 5 public class Test
 6 {
 7     public static void main(String[] args)
 8     {
 9         Student s1 = new Student();
10         s1.eat();
11     }
12 }

结论:子类重写父类的方法。

 

注意:属性不会重写。

注意:以下方法不会重写

  1、静态方法不会重写,父类是static修饰的,子类必须是static。

  2、final修饰的方法不会重写。

  3、私有方法不会重写。

 

posted @ 2017-04-15 19:54  程序员的世界。。。  阅读(207)  评论(0编辑  收藏  举报