Java-继承的应用

class array
{
private int[] temp;
private int foot;
public array(int len)   //为temp数组规定大小,并开辟空间,申明类的方法
{
if(len>0)
{
this.temp=new int[len];
}
else
{
this.temp=new int[1];
}
}
public boolean add(int i)
{
if(this.foot<this.temp.length)
{
this.temp[foot]=i;
foot++;
return true;
}
else
{
return false;
}
}
public int[] getarray()
{
return this.temp;
}
}
class reservearray extends array   //定义反转类
{
public reservearray(int len)
{
super(len);         //调用父类中的构造方法,给反转数组赋值
}
public int[] getarray()   //覆写数组,反转数组返回
{
int t[]=new int[super.getarray().length];
int count=t.length-1;
for(int x=0;x<t.length;x++)   //利用for循环给反序数组赋值
{
t[count]=super.getarray()[x];
count–;
}
return t;   //返回数组首地址
}
}
class sortarray extends array
{
public sortarray(int len)
{
super(len);
}
public int[] getarray()
{
java.util.Arrays.sort(super.getarray());
return super.getarray();

}
}
public class test31 {
public static void main(String args[])
{
reservearray a=null;
a=new reservearray(5);
sortarray b=null;
b=new sortarray(5);
System.out.print(a.add(43)+”\t”);
System.out.print(a.add(23)+”\t”);
System.out.print(a.add(54)+”\t”);
System.out.print(a.add(18)+”\t”);
System.out.print(a.add(85)+”\t”);
System.out.println(“”);
print(a.getarray());
System.out.println(“”);
System.out.print(b.add(43)+”\t”);
System.out.print(b.add(23)+”\t”);
System.out.print(b.add(54)+”\t”);
System.out.print(b.add(18)+”\t”);
System.out.print(b.add(85)+”\t”);
System.out.println(“”);
print(b.getarray());
}
public static void print(int i[])
{
for(int x=0;x<i.length;x++)
{
System.out.print(i[x]+”、”);
}
}

}

标签