Can I add new methods to the String class in Java?

后端 未结 15 2307
再見小時候
再見小時候 2020-11-27 06:36

I\'d like to add a method AddDefaultNamespace() to the String class in Java so that I can type \"myString\".AddDefaultNamespace() instead of

15条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 07:15

    Not possible, and that's a good thing. A String is a String. It's behaviour is defined, deviating from it would be evil. Also, it's marked final, meaning you couldn't subclass it even if you wanted to.

提交回复
热议问题