最近看了一下数据结构,感觉楼主的这种做法在数据结构方面很重要,因为在数据结构中经常要自己定义数据类型,对相应的数据进行操作,原有的String已经不够用了,而且它是fianl的,无法扩展,所以很多国外的编程人员都自己定义一个ADT public abstract class DataElement { public abstract boolean equals(DataElement otherElement); //Method to determine whether two objects contain the //same data. //Postcondition: Returns true if this object contains the // same data as the object otherElement; // otherwise, it returns false.
public abstract int compareTo(DataElement otherElement); //Method to compare two objects. //Postcondition: Returns a value < 0 if this object is // less than the object otherElement; // Returns 0 if this object is the same as // the object otherElement. // Returns a value > 0 if this object is // greater than the object otherElement.
public abstract void makeCopy(DataElement otherElement); //Method to copy otherElement into this object. //Postcondition: The data of otherElement is copied into // this object.
public abstract DataElement getCopy(); //Method to return a copy of this object. //Postcondition: A copy of this object is created and // a reference of the copy is returned. }自己来随时扩展!!
如果是 方法 被限定为 final,这个方法就不能被重载了。
____________________________________
www.elingke.com 提供J2EE快速开发平台
如果觉得这样的需求很具有普遍意义,那么就给sun发封mail吧!
请他们在JDK1.6中给加上就可以了啊!
呵呵 笑谈,笑谈
public final class String
他是final的,是不能被继承的,因此你无法向其中添加方法。
extends Object
implements Serializable, Comparable, CharSequence
public abstract class DataElement
{
public abstract boolean equals(DataElement otherElement);
//Method to determine whether two objects contain the
//same data.
//Postcondition: Returns true if this object contains the
// same data as the object otherElement;
// otherwise, it returns false.
public abstract int compareTo(DataElement otherElement);
//Method to compare two objects.
//Postcondition: Returns a value < 0 if this object is
// less than the object otherElement;
// Returns 0 if this object is the same as
// the object otherElement.
// Returns a value > 0 if this object is
// greater than the object otherElement.
public abstract void makeCopy(DataElement otherElement);
//Method to copy otherElement into this object.
//Postcondition: The data of otherElement is copied into
// this object.
public abstract DataElement getCopy();
//Method to return a copy of this object.
//Postcondition: A copy of this object is created and
// a reference of the copy is returned.
}自己来随时扩展!!
另外“所以很多国外的编程人员都自己定义一个ADT”里的“ADT”是什么意思?
扩展一个public class StringElement extends DataElement
{
protected String str; //default constructor
public StringElement()
{
str = null;
}
//constructor with a parameter
public StringElement(String s)
{
str = s;
}
//copy constructor
public StringElement(StringElement otherString)
{
str = otherString.str;
}
//Method to set the value of the instance variable str.
//Postcondition: str = x;
public void setString(String x)
{
str = x;
} public boolean equals(DataElement otherElement)
{
StringElement temp = (StringElement) otherElement;
return (str.compareTo(temp.str) == 0);
} public int compareTo(DataElement otherElement)
{
StringElement temp = (StringElement) otherElement;
return (str.compareTo(temp.str));
} public void makeCopy(DataElement otherElement)
{
StringElement temp = (StringElement) otherElement;
str = new String(temp.str);
} public DataElement getCopy()
{
StringElement temp = new StringElement(str);
return temp;
} public String toString()
{
return str;
}
}
?
找源码中的String类,加上自己的方法..然后重编译..替换jre中的String类..zez估计没这样干过,java.lang.开头的类你是没办法编译的
package java.lang;public class Debug {
public static void hello(String[] args){
System.out.println("hello.");
}
}
编译是可以,但我另一个地方调用的时候
package untitled1;public class a {
public static void main(String[] args) {
Debug.hello();
}
}
这个类就无法编译,不是说找不到类,也不是说找不到方法,莫名其秒的一个错,所以我就想java.lang开头的类可能有一定保护措施,
有兴趣可以试一下,我是用Jbuilder
public class Util{
public static String myMethod(String arg){
...
}
}
这样不用对系统做任何更改。
zez(思恩 闭关练功ing...),这方法你也想得出,哈哈,如果大家都像你说的这样做,那么sun公司就真的有前途了
虽然是可行,不过好像不推荐在生产系统中这么做
用来学习是很不错的
回复人:zez(思恩 闭关练功ing...) ( 两星(中级)) 信誉:176 2004-12-15 12:49:00 得分:0
?
找源码中的String类,加上自己的方法..然后重编译..替换jre中的String类..zez估计没这样干过,java.lang.开头的类你是没办法编译的
其实你可以脱离jb试试的,jjhou在他的《java反射机制》一文里介绍了如何“改动Java标准库源码”