Extend java.lang.String

前端 未结 5 919
甜味超标
甜味超标 2020-12-15 16:43

java.lang.String is declared as final, however are there any mechanisms available legitimate or otherwise to extend it and replace the equals(String other) method?

5条回答
  •  时光取名叫无心
    2020-12-15 17:15

    No, absolutely not. If you want some "other" kind of string, create another type which might contain a string:

    public final class OtherString {
        private final String underlyingString;
    
        public OtherString(String underlyingString) {
            this.underlyingString = underlyingString;
        }        
    
        // Override equals however you want here
    }
    

提交回复
热议问题