简体   繁体   English

Java中的对象初始化-术语

[英]Object initialization in Java - terminology

I came across this blog, and they used term Anonymous object instantiation 我遇到了这个博客,他们使用了“ 匿名对象实例化”一词。

Is it really an "official term" or not? 它真的是一个“官方术语”吗?

In any case, what is the official terminology for that kind of code: calling a method without explicitly creating object with its name like in this example (UPDATE: I am referring to explicitly giving a name to object not explicitly creating object) : 无论如何,这种代码的正式术语是什么:调用一个方法而不像本例中那样显式地创建带有其名称的对象(UPDATE:我指的是显式地给对象命名而不是显式地创建对象):

public class Predictions3Client {
    public static void main(String[]  args) {
        new Predictions3Client().demo();
    }

    private void demo() {
        Localhost_Predictions3ResourcesP.Root root = Localhost_Predictions3ResourcesP.root();
        Localhost_Predictions3ResourcesP.Root.Xml xmlGetter = root.xml();
        String xml = xmlGetter.getAsXml(String.class); 
        System.out.println("The raw XML:\n" + xml);   
    }
}
new Predictions3Client().demo();

vs

Predictions3Client p3c = new Predictions3Client ();
p3c.demo();

I don't consider that as "method chaining" or "Fluent interface" as it is object related not method related. 我不认为它是“方法链接”或“ Fluent接口”,因为它与对象相关,与方法无关。

In Java Anonymous objects are objects that don't have class names or have abstract class names. 在Java中,匿名对象是没有类名或抽象类名的对象。 In short, you must fill in the "missing" methods to build the class, and those "missing" methods aren't tied to a class name. 简而言之,您必须填写“ missing”方法来构建类,并且这些“ missing”方法不与类名绑定。

   Runnable runnable = new Runnable() {
      public void run() {
         System.out.println("Hi, I'm anonymous!");
      }
   }

is an example. 是一个例子。 If it was a fully named class, it would look like 如果它是一个全名的类,它将看起来像

 public class HelloRunnable implements Runnable {
      public void run() {
         System.out.println("Hi, I'm not anonymous, I'm a HelloRunnable!");
      }
 }

Note that anonymous classes are still constructed, but they are constructed with the missing methods as parameters (they're supplied between the curly braces). 请注意,匿名类仍在构造,但是它们是使用缺少的方法作为参数来构造的(它们在花括号之间提供)。 So, technically you are still "creating" a class, and you are still "creating an instance" of a class, but that class has no single, uniquely identifiable class name. 因此,从技术上讲,您仍在“创建”一个类,并且仍在“创建一个类的实例”,但是该类没有单个可唯一识别的类名。

Hence the "anonymous object instantiation" which I would have referred to by the simpler phrase "anonymous object creation" 因此,我会用更简单的短语“匿名对象创建”来指代“匿名对象实例化”

Keep in mind that the first example's class name is not properly referred to as a "Runnable" class because not all runnables will print out "Hi, I'm anonymous" as demonstrated in the example below. 请记住,第一个示例的类名未正确地称为“ Runnable”类,因为并非所有可运行对象都会打印出“嗨,我是匿名的”,如以下示例所示。

   Runnable runnable = new Runnable() {
      public void run() {
         System.out.println("Hi, I'm anonymous!");
      }
   }
   runnable = new Runnable() {
      public void run() {
         System.out.println("Hi, I'm anonymous's cousin!");
      }
   }

However, in the example above, it is proper to call both classes a Runnable, because they both implement the runnable interface. 但是,在上面的示例中,将两个类都称为Runnable是适当的,因为它们都实现了runnable接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM