gml520 发表于 2013-2-5 02:17:43

Super关键字的几点应用

Super关键字的几点应用

最近在学习java时的一点心得,有错的地方希望大家指正。

在Java中,如果子类中定义的成员变量和超类(父类)中的成员变量同名时,则父类中的成员变量不能被继承,此时称子类的成员变量隐藏了父类的成员变量。当子类中定义了一个方法,并且这个方法的名字,返回类型,及参数个数和类型和父类的某个方法完全相同时,父类的这个方法将被隐藏,既不能被子类继承下来。如果我们在子类中想使用被子类隐藏的父类的成员变量或方法就可以使用关键字super。
1.使用super调用超类(父类)的构造行函数
子类不继承父类的构造方法,因此,子类如果想使用父类的构造方法,必须在子类的构造方法中使用,并且必须使用关键字super来表示,而且super必须是子类构造方法中的头一条语句。例如1所示:
例子 1
class Student {
int number;
String name;
Student(int number, String name){
this.nember=number;
this.name=name;
System.out.println(“I am ”+name+”my number is ”+number);
}
}
class Univer_Student extends Student{
boolean 婚否;
Univer_Student(int number, String name; boolean b){
       Super(number,name);
      婚否=b;
System.out.println(“婚否=”+婚否);
}
}

public class Example4_24{
   public static void main(String args[]){
Univer_Student zhang=new Univer_Student(9901,”和晓玲”,false);
}
}

运行结果:
I am 和晓玲 my number is 9901
婚否=false.

需要注意的是:如果在子类的构造方法中,没有显示地使用super关键字调用父类地某个构造方法,那么默认地有:super(); 语句,即调用父类地不带参数地构造方法。如果父类没有提供参数地构造方法,就会出现错误。

2.使用super操作被隐藏的成员变量和方法
如果我们在子类中想使用被子类隐藏了的父类的成员变量或方法就可以使用super关键字。比如 super.x, super.play(),就是被子类隐藏的父类的成员变量x,和方法play。
例子2
class sum{
int n;
float f(){
float sum=0;
for(int i=1;i<=n;i++)
sum=sum+I;
return sum;
}
}
class Average sxtends 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 Example2{
public static void main(String args[] ){
Average aver=new Average();
Aver.n=100;
float result-1=aver.f();
float result_2=aver.g();
System.out.println(“result_1”+result_1);
System.out.println(“result_2”+result_2);
}
}

运行结果:
result_1=50.50
result_2=2525.0

super 的另一种用法
super 的另一种种形式的用法有点像this,只是它总是引用它所在子类的父类。这种用法的一般形式如下:
super.member
其中member可以是一个方法,也可以是一个实例变量。
super的第二种形式多用于子类成员名被超类中同样的成员名隐藏的情况。考虑下面这个简单的类层次:
class A{
int i;
}
class B extends A{
int i;
B(int a,int b){
super.i=a;
i=b;
}
void show(){
System.out.println("i in superclasses: "+super.i);
System.out.println("i in superclass: "+i);
}
}
class UseSuper{
public static void main (String[] args) {
B subob=new B(1,2);
subob.show();
}
}

该程序的输出如下:
    i in superclasses: 1
i in superclass: 2
尽管在B中的实例变量i隐藏了A中的i,但是super允许访问在父类中定义的i。当然super也可以调用被子类隐藏的方法。
    让我们来回顾一下super()的关键概念:当一个超类调用super()时,就是调用其直接父类的构造函数,因此super总是引用调用类之上的父类,在多级层次中也是如此。
创造多级层次结构
到目前为止,我们一直在使用由一个父类和一个子类组成的简单的类层次。其实,你可以构建包含所需多层继承的层次结构,可以用一个子类做为另一个子类的父类。例如:现在有三个类A,B和C,C是B的子类,B是A的子类。每个子类继承了其父类中的所有特性,即C继承了B和A的所有特性。要弄清一个多级层次结构的功能,考虑下面的程序(例子有点长):
class Box{
private double width;
private double hight;
private double length;

//construct clone of an object
Box(Box ob){//pass object to constuctor
width=ob.width;
hight=ob.hight;
length=ob.length;
}
// construct used when all dimensions specified
Box(double w,double h , double l){
width=w;
hight=h;
length=l;
}
//construct used when no dimensions specified
Box(){
width=-1;
hight=-1;
length=-1;
}
//construct used when cube is created
Box(double len){
width=hight=length=len;
}
//compute and return volume
double volume(){
return width*hight*length;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(BoxWeight ob){
super(ob);
weight=ob.weight;
}
BoxWeight(double w,double h,double l, double m){
//width=w;
//hight=h;
//length=l;
super(w,h,l);
weight=m;
}
BoxWeight(){
super();
weight=-1;
}
BoxWeight(double len,double m){
super();
weight=m;
}

}
class Shipment extends BoxWeight{
double cost;
Shipment(Shipment ob){
super(ob);
cost=ob.cost;
}
Shipment(double w,double h,double l,double m,double c){
super(w,h,l,m);
cost=c;
}
Shipment(){
super();
cost=-1;
}
Shipment(double len, double m,double c){
super(len,m);
cost=c;
}
}
public class DemoShipment {

public static void main(String[] args) {
   Shipment shipment1=new Shipment(10,20,15,10,3.41);
         Shipment shipment2=new Shipment(2,3,4,0.76,1.28);
         double vol;
         vol=shipment1.volume();
         System.out.println("Volume of shipment1 is "+vol);
         System.out.println("Weight of shipment1 is "+shipment1.weight);
         System.out.println("Shipping cost: $ "+shipment1.cost);
         System.out.println();
         vol=shipment2.volume();
         System.out.println("Volume of shipment2 is "+vol);
         System.out.println("Weight of shipment2 is "+shipment2.weight);
         System.out.println("Shipping cost: $ "+shipment2.cost);
         //System.out.println();
}
}
该程序的输出结果如下:
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost: $ 3.41

Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost: $ 1.28

因为继承关系,shipment能够利用前面定义的Box和BoxWeight类,仅为自己和特殊应用程序添加所需的额外的信息。这体现了继承的部分价值:它允许重用代码。
该示例展示了另一个重要的知识点:super()总是引用最近父类中的构造函数。Shipment中的super()调用BoxWeight中的构造函数,Boxweight中的super()调用Box中的构造函数。在一个类层次中如果一个父类构造函数要求参数,那么所有的子类必须“向上”传递那些参数,而不管子类是否需要参数。
页: [1]
查看完整版本: Super关键字的几点应用