Java学习之面向对象(方法的重写 overwrite,super的使用,继承中的构造方法)
1.方法的重写
(1)在子类中可以根据对从基类中继承的方法进行重写。
(2)重写方法必须和被重写方法具有相同方法名称,参数列表和返回类型。
(3)重写方法不能使用比被重写方法更严格的访问权限。
1 class Person{ 2 private String name; 3 private int age; 4 public void setName(String name){ 5 this.name=name; 6 } 7 public void setAge(int age){ 8 this.age=age; 9 } 10 public String getName(){ 11 return name; 12 } 13 public int getAge(){ 14 return age; 15 } 16 public String getInfo(){ 17 return "name"+"\t"+name+"\n"+"age"+"\t"+age; 18 } 19 } 20 class Student extends Person{ 21 private String school; 22 public void setSchool(String school){ 23 this.school=school; 24 } 25 public String getSchool(){ 26 return school; 27 } 28 public String getInfo(){ 29 return "name"+"\t"+ getName()+"\n"+"age"+"\t"+getAge()+"\n"+"school"+"\t"+school; 30 } 31 } 32 public class textoverwrite { 33 public static void main(String arg[]){ 34 Student student=new Student(); 35 Person person=new Person(); 36 person.setName("none"); 37 person.setAge(1000); 38 student.setName("John"); 39 student.setAge(18); 40 System.out.println(person.getInfo()); 41 System.out.println(student.getInfo()); 42 } 43 44 }
输出结果:
name none
age 1000
name John
age 18
school null
2.super的使用
(1)在Java类中使用super来引用基类(父类)的成分。
(2)注意:与this的区别(this是对当前对象的引用)。
1 class FatherClass{ 2 public int value; 3 public void f(){ 4 value=100; 5 System.out.println("FatherClass.value="+value); 6 } 7 } 8 class ChildClass extends FatherClass{ 9 public int value; 10 public void f(){ 11 super.f(); 12 value=200; 13 System.out.println("ChildClass.value="+value); 14 System.out.println("value="+value); 15 System.out.println(super.value); 16 } 17 } 18 public class Textinberit { 19 public static void main(String arg[]){ 20 ChlidClass cc=new ChlidClass(); 21 cc.f(); 22 23 } 24 25 }
输出结果:
FatherClass.value=100
ChildClass.value=200
200
100
内存图如下:
3.继承中的构造方法规则:
(1)子类的构造的过程中必须调用其基类的构造方法。
(2)子类可以在自己的构造方法中使用super(argument_list)调用基类的构造方法。
注意:a.使用this(argument_list)调用本类的另外的构造方法。
b.如果调用super,必须写在子类构造方法的第一行。
(3)如果子类的构造方法中没有显示的调用基类构造方法,则系统默认调用基类无参数的构造方法。
(4)如果子类构造方法中既没有显示调用基类构造方法,而基类中又没有无参数构造方法,则编译出错。
1 class SuperClass{ 2 private int n; 3 SuperClass(){ 4 System.out.println("SuperClass()"); 5 } 6 SuperClass(int n){ 7 System.out.println("SuperClass("+n+")"); 8 this.n=n; 9 } 10 class Subclass extends SuperClass{ 11 private int n; 12 Subclass(int n){ 13 14 //super(); 15 System.out.println("SubClass("+n+")"); 16 this.n=n; 17 } 18 19 } 20 Subclass(){ 21 super(300); //对super的调用必须是构造函数的第一个语句。 22 System.out.println("SubClass"); 23 } 24 } 25 26 public class TextSupersub { 27 public static void main(String arg[]){ 28 SubrClass sc1=new SubClass(); 29 SubClass sc2=new SubClass(400); 30 } 31 }
输出结果:
SuperClass(300)
SubClass
SuperClass() [反映了规则(3)]
SubClass(400)
1 class Person{ 2 private String name; 3 private String location; 4 Person(String name){ 5 location="beijing"; 6 this.name=name; 7 } 8 Person(String name,String location){ 9 this.name=name; 10 this.location=location; 11 } 12 public String info(){ 13 return "name"+"\t"+name+"\t"+"location"+"\t"+location; 14 } 15 } 16 class Teacher extends Person{ 17 private String capital; 18 Teacher(String name, String capital) { 19 this(name,"beijing",capital); 20 } 21 Teacher(String name,String location,String capital){ 22 super(name,location); 23 this.capital=capital; 24 } 25 public String info(){ 26 return super.info()+"\t"+"capital"+"\t"+capital; 27 } 28 29 30 } 31 public class TextTeacher { 32 public static void main(String[] args){ 33 Person p1=new Person("A"); 34 Person p2=new Person("B","shanghai"); 35 Teacher t1=new Teacher("C1","professor"); 36 Teacher t2=new Teacher("C2","shanghai","professor"); 37 System.out.println(p1.info()); 38 System.out.println(p2.info()); 39 System.out.println(t1.info()); 40 System.out.println(t2.info()); 41 } 42 } 43 44
输出结果:
name A location beijing
name B location shanghai
name C1 location beijing capital professor
name C2 location shanghai capital professor