Java数据结构---基于数组的表__教程 |
|
日期:2007-5-20 0:37:03 人气:88 [大 中 小] |
|
|
|
IntElement newElement=new IntElement(); newElement.num=this.num; return newElement; } public String toString(){ return String.valueOf(num); } }
StringElement:
public class StringElement extends DataElement {
/** * */ private String str; //constructors public StringElement() { str=null; } public StringElement(String string){ str=string; } public StringElement(StringElement otherElement){ str=otherElement.str; } //get-set Methods public void setStr(String string){ str=string; } public String getStr(){ return str; }
/* (non-Javadoc) * @see DataElement#equals(DataElement) */ public boolean equals(DataElement otherElement) { // TODO Auto-generated method stub StringElement newe=(StringElement)otherElement; return (str==newe.str); }
/* (non-Javadoc) * @see DataElement#compareTo(DataElement) */ public int compareTo(DataElement otherElement) { // TODO Auto-generated method stub StringElement newe=(StringElement)otherElement; return (str.compareTo(newe.str)); }
/* (non-Javadoc) * @see DataElement#makeCopy(DataElement) */ public void makeCopy(DataElement otherElement) { // TODO Auto-generated method stub StringElement newe=(StringElement)otherElement; str=newe.str; }
/* (non-Javadoc) * @see DataElement#getCopy() */ public DataElement getCopy() { // TODO Auto-generated method stub StringElement othere=new StringElement(); othere.str=str; return othere; }
public String toString(){ return str; } }
已经定义好了数据类型,所以list的数据类型我们就可以定义为DateElement[]了,这样就可以包括所以你想要的了,只要你在用的时候定义一个DataElement的子类就行了,这正是java继承的精髓所在.我们接着定义ArrayListClass类:
protected int length; protected int maxSize; |
|
出处:本站原创 作者:佚名 |
|
|