子类继承的成员方法

Student3.java *

Student3.java                                                             2012-7-2221:43

 

1publicclassPerson3

2{

3     protectedStringname;//保护成员变量

4     protectedintage;

5     voidsetdata(Stringn1,inta1)

6     {

7         name=n1;

8         age=a1;

9     }

10     publicvoidprint()

11     {

12         System.out.println("姓名:"+name+",年龄:"+age);

13     }

14

15}

16

17/**子类继承的成员方法

18*   不能继承声明为private的方法;

19*   不能继承超类的构造方法;

20*   如果子类方法与超类方法同名,则不能继承,而是覆盖,复写.

21*/

22

23

24

25classStudent3extendsPerson3

26{

27     protectedStringdept;

28     publicvoidprint()                    //覆盖父类的方法,重写.

29     {

30         System.out.println("姓名:"+name+",年龄:"+age+",专业:"+dept);

31     }

32     publicstaticvoidmain(Stringargs[])

33     {

34         Person3p1=newPerson3();

35         p1.setdata("cquptzx",22);

36         p1.print();

37

38         Student3s1=newStudent3();

39         s1.setdata("cqupttx",23);          //调用超类的成员方法

40         s1.dept="telecommunications";     //访问本类的成员变量;

41         s1.print();                        //----调用的不是超类的方法

42                                             //-----而是本类重写过的方法;

43     }

44}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<![if !vml]><![endif]>11

原文地址:https://www.cnblogs.com/xilifeng/p/2604119.html