String类的声明如下:
public final class String
他是final的,是不能被继承的,因此你无法向其中添加方法。

解决方案 »

  1.   

    自己写个String类,类名换一个,接口实现
      

  2.   

    这个设想很有创意 stonefeng(浩)的说法没错 所以关注
      

  3.   

    String 对象是 final 的,不能被继承,所以不能添加新的方法。
    如果是 方法 被限定为 final,这个方法就不能被重载了。
    ____________________________________
    www.elingke.com 提供J2EE快速开发平台
      

  4.   

    还真没注意过是个final,那用个笨方法,自己做个类,声明和string一样的方法,实际上就是调用string的方法,最后再加上你自己的方法实现。
      

  5.   

    呵呵楼主怎么会有这样的需求呢?
    如果觉得这样的需求很具有普遍意义,那么就给sun发封mail吧!
    请他们在JDK1.6中给加上就可以了啊!
    呵呵  笑谈,笑谈
      

  6.   

    String类的声明如下:
    public final class String
    他是final的,是不能被继承的,因此你无法向其中添加方法。
      

  7.   

    自己写个String类,类名换一个,接口实现public final class String
    extends Object
    implements Serializable, Comparable, CharSequence
      

  8.   

    找源码中的String类,加上自己的方法..然后重编译..替换jre中的String类..
      

  9.   

    zez(思恩 闭关练功ing...)  强!!!!!
      

  10.   

    zez(思恩 闭关练功ing...),这方法你也想得出,哈哈,如果大家都像你说的这样做,那么sun公司就真的有前途了
      

  11.   

    最近看了一下数据结构,感觉楼主的这种做法在数据结构方面很重要,因为在数据结构中经常要自己定义数据类型,对相应的数据进行操作,原有的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.
    }自己来随时扩展!!
      

  12.   

    myth822(枫红一刀) 的这个如何使用? 我怎么不大明白?
    另外“所以很多国外的编程人员都自己定义一个ADT”里的“ADT”是什么意思?
      

  13.   

    如果是没有 final 的类,是否可以给它写一个 myMethod() 呢?要如何写?
      

  14.   

    ADT(abstract data type)抽象数据类型
    扩展一个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;
        }
    }
      

  15.   

    cuixiping(无心) 没有final的类可以被继承,这时你就可以增加你自己想要的方法了,像上面 myth822(枫红一刀) 给的例子一样,可以自己写个类然后在写一个来继承
      

  16.   

    回复人:zez(思恩 闭关练功ing...) ( 两星(中级)) 信誉:176  2004-12-15 12:49:00  得分:0

    找源码中的String类,加上自己的方法..然后重编译..替换jre中的String类..zez估计没这样干过,java.lang.开头的类你是没办法编译的
      

  17.   

    String我没编译过:),不过我以前写过一个类,这样定义的
    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
      

  18.   

    如果是个接口就好办了,但是String是个class还真有点难办。关注中...
      

  19.   

    还是写个算法类,里面实现这个方法比较好:
    public class Util{
      public static String myMethod(String arg){
        ...
      }
    }
    这样不用对系统做任何更改。
      

  20.   

    回复人: charlie0895(命—love alice crazy) ( ) 信誉:100  2004-12-15 13:23:00  得分: 0  
     
     
       zez(思恩 闭关练功ing...),这方法你也想得出,哈哈,如果大家都像你说的这样做,那么sun公司就真的有前途了
      
     
    虽然是可行,不过好像不推荐在生产系统中这么做
    用来学习是很不错的
      

  21.   

    回复人: thinkingjava() ( ) 信誉:100  2004-12-16 16:57:00  得分: 0  
     
     
       回复人:zez(思恩 闭关练功ing...) ( 两星(中级)) 信誉:176  2004-12-15 12:49:00  得分:0

    找源码中的String类,加上自己的方法..然后重编译..替换jre中的String类..zez估计没这样干过,java.lang.开头的类你是没办法编译的
      
     
    其实你可以脱离jb试试的,jjhou在他的《java反射机制》一文里介绍了如何“改动Java标准库源码”
      

  22.   

    myth822(枫红一刀)说的应该是正确的,通过自己的类在扩展方法