English 中文(简体)
对物体的新功能
原标题:Apply new function on an object
  • 时间:2011-03-21 17:34:19
  •  标签:
  • java

我有一席之地,我希望一劳永逸地(它将是更换方)履行职务。 如果插图称作<条码>,,则我想做些什么,而我的职能是<条码>。 我应如何宣布<代码>color(? 我是否应该把它放在一个延伸到<编码>的类别中? 我是否应该放弃这一点,而是应该把<编码>color(string)?

最佳回答

String is a final class in java and so you cannot extend and add method to String class
You should do color(String) instead.
If you re dealing with bunch of replacement in String consider StringBuilder instead.

问题回答

Just do public 斯特林(String){......}

由于您可以修改<代码>String(即可再改动),你将重新从这两种办法中恢复新的<代码>String。 只要情况证明有理由使用像继承或组成这样的更为复杂的东西,那么远见就更直截了当。

仅凭一项说明——你的建议(级延长:<编码>标准,或接口)都不允许你避免<代码>color(String):

  • String is a final class, so you can t define a subclass of it with your own operations on.
  • You can create any interfaces you like, but you can t make String implement them - so the method on the interface would need to take a String as an argument, and then you re back to color(String) again.

Consider composition (e.g. a simple wrapper for String):

public class EnhancedString {
    private String str;
    public EnhancedString(final String str) {
        this.str = str;
    }
    public EnhancedString color() {
        // do some stuff and return the result
        return this;
    }
    public EnhancedString anotherReplacement() {
        // more stuff
        return this;
    }
}

这种办法的ice之处在于,它允许你在后来(例如:贱民式)进行职业链式:

EnhancedString estr = new EnhancedString("testing");
estr.color().anotherReplacement();

页: 1

仅彩色。

努力是最终的,是不可调和的,因此,你将无法扩大这一努力,不能成为一个具有新方法的子类别,例如肤色。

一种做法是建立一个有色体法定义的强食类,即:

public static String color(String someString) {
    // Your logic that creates a new "colored" String and returns this new String
}

那么,你就可以说:

String coloredString = StringUtil.color(someString);

此外,如果你在你的彩色方法上开展许多行动(如分类),而且你只能把“斯特林”方法的结果归还给StingBuilder。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签