子类用super操作被隐藏的成员变量和方法
Example5_7.java
class Sum { int n; float f() { float sum=0; for(int i=1;i<=n;i++) sum=sum+i; return sum; } } class Average extends Sum { int n; float f() { float c; super.n=n; c=super.f(); return c/n; } float g() { float c; c=super.f(); return c/2; } } public class Example5_7 { public static void main(String args[]) { Average aver=new Average(); aver.n=100; float resultTwo=aver.g(); float resultOne=aver.f(); System.out.println("resultOne="+resultOne); System.out.println("resultTwo="+resultTwo); } }
使用super调用父类的构造方法
Example5_8.java
class Student { int number;String name; Student() { } Student(int number,String name) { this.number=number; this.name=name; System.out.println("我的名字是:"+name+ "学号是:"+number); } } class UniverStudent extends Student { boolean 婚否; UniverStudent(int number,String name,boolean b) { super(number,name); 婚否=b; System.out.println("婚否="+婚否); } } public class Example5_8 { public static void main(String args[]) { UniverStudent zhang=new UniverStudent(9901,"何晓林",false); } }