English 中文(简体)
Java有静态“NUL”物体?
原标题:Having a static "NULL" object in Java?
  • 时间:2011-10-28 20:42:23
  •  标签:
  • java
  • null

在通过一些法典浏览图书馆Im时,我穿过这部法规:

public class SomeClass {

     private static final class Null {
        /* ... */
     }

     public static final Object NULL = new Null();

}

是否这种共同做法是,对于一个被使用而不是Java scode>null的类别而言,要有一个专门的NUL物体? 不是使用在<代码>null上修建的Javas,而是做些什么?

I m可治愈的主要原因是,在使用图书馆时,我一直在检查一些地图集是否无效,而没有认识到它们正在使用一个特别的民族解放军标语来表示某些地图集的无效物体。

http://www.un.org。 对于那些想不到这一点的人来说,来文方的确切法典是:

public class JSONObject {
     private static final class Null {
        protected final Object clone() {
            return this;
        }
        public boolean equals(Object object) {
            return object == null || object == this;
        }
        public String toString() {
            return "null";
        }
    }

    public static final Object NULL = new Null();
}
最佳回答

null Object patterns有时会是一种有用的做法,因此,你不使用一字,而是使用一种具有中立行为的实际物体。 然而,鉴于此,你已经执行:

  • Null doesn t extend SomeClass, which I d expect it to
  • NULL is declared as Object, not SomeClass.

因此,我可以说,法典是无用的。 但在适当执行时,这种模式可能有用。

问题回答

This is the "null object pattern". The advantage is that you don t have to explicitly check for null, avoiding NPEs.

他们的班次最好的名称是 Empty或类似。 采用java关键词的变数并不是一个好的想法,而是导致混淆......和这些问题。

此外,空物体通常属于特殊类别:

public class SomeClass {

     public static final SomeClass EMPTY = new SomeClass("parameters that make it empty");
}

这取决于使用。 例如,可以开设“NUL”课程,它代表的是Null数据类型,NOT与java数据一样。 这方面的一个例子是:

否则,在民族解放军的物体中总结出无效价值并非属实。

Null object pattern.

我在我的项目中曾一度使用过这个词,因为那是在<斯特隆>上播音的。





相关问题
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 ...

热门标签