谁能解释以下代码的工作原理...?

发布于 2024-09-04 11:46:53 字数 606 浏览 6 评论 0原文

谁能解释以下代码的工作原理...?

interface myInterface{}

public class Main {

    public static void main(String[] args) {

        System.out.println(new myInterface(){public String toString(){return "myInterfacetoString";}});

        System.out.println(new myInterface(){public String myFunction(){return "myInterfacemyFunction";}});
    }
}

输出是...

myInterfacetoString
primitivedemo.Main$2@9304b1

所有答案都说 println() 语句中的 myInterface 是匿名类。但由于我已经将其声明为接口,为什么它允许我创建同名的匿名类......?

再次...如果这些是匿名类,那么 main 类应该允许我为这些匿名类指定任何名称。但是如果尝试这样做...我会收到编译错误

Can anybody explain the working of following code...?

interface myInterface{}

public class Main {

    public static void main(String[] args) {

        System.out.println(new myInterface(){public String toString(){return "myInterfacetoString";}});

        System.out.println(new myInterface(){public String myFunction(){return "myInterfacemyFunction";}});
    }
}

Output is...

myInterfacetoString
primitivedemo.Main$2@9304b1

All answers saying that myInterface in println() statement is anonymous class. But as I already declared it as an interface, why does it allow me to create anonymous class of same name....?

again...if these are anonymous classes then class main should allow me to give any name to these anonymous classes..But if try to do so..I'm getting compilation error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小苏打饼 2024-09-11 11:46:53

当你打印出一个对象时,它会调用 toString() 方法。在第一个语句中,您创建新对象并覆盖名为 toString 的方法。因此,当打印对象时会调用这个 toString() 方法。

在第二条语句中,您还创建了一个对象,但没有重写 toString() 方法,以便它将使用 Object 类的 toString() 方法。

对于新问题,这个链接有很好的解释供您理解:
匿名类

以下是解释的副本:

new className(optional argument list){classBody}

此表达式从未命名且先前未定义的类实例化一个新对象,该类自动扩展名为 className 的类,并且无法显式实现任何接口。新类的主体由 classBody 给出。

执行该表达式的结果是定义了一个扩展className的新类,实例化了一个新类的新对象,并且表达式被替换为对新对象的引用

new interfaceName(){classBody}

此表达式从未命名且先前未定义的类实例化一个新对象,该类自动实现名为interfaceName的接口,并自动扩展名为 Object 的类。该类可以显式实现一个且仅一个接口,并且不能扩展除Object 之外的任何类。新类的主体再次由 classBody 给出。

现在你清楚了吗?

When you print out an object, it will call the toString() method. In the first statement, you create new object and also override method called toString. Therefore, this toString() method is called when object is printed.

In the second statement, you also create an object but you don't override the toString() method so that it will use the toString() method of the Object class.

For the new question, this link has a good explanation for your understanding:
Anonymous Classes

Here is a copy of the explanation:

new className(optional argument list){classBody}

This expression instantiates a new object from an unnamed and previously undefined class, which automatically extends the class named className, and which cannot explicitly implement any interfaces. The body of the new class is given by classBody.

The result of executing this expression is that a new class that extends className is defined, a new object of the new class is instantiated, and the expression is replaced by a reference to the new object.

new interfaceName(){classBody}

This expression instantiates a new object from an unnamed and previously undefined class, which automatically implements the interface named interfaceName, and automatically extends the class named Object. The class can explicitly implement one, and only one interface, and cannot extend any class other than Object. Once again, the body of the new class is given by classBody.

Is it clear for you now?

紙鸢 2024-09-11 11:46:53

在第一个 println() 上,您将重写匿名 myAbstractClass 实例中的 toString() 方法,因此您将获得重写返回的字符串toString() 方法,这是 println() 函数的默认行为。

在第二个 println() 上,您没有重写 toString() 方法,因此 println() 使用默认方法(继承自 <代码>对象)。

附带说明一下,尝试正确格式化代码,这样更容易阅读和理解。

abstract class myAbstractClass{}

public class Main { 
    public static void main(String[] args) { 
        System.out.println(new myAbstractClass(){
            public String toString(){
                return "myAbstractClass toString";
            }
        });

        System.out.println(new myAbstractClass(){
            public String myFunction(){
                return "myAbstractClass myFunction";
            }
        }); 
    } 
}

On the first println() you are overriding the toString() method from the anonymous myAbstractClass instance, therefore you get the string returned by your overriden toString() method, that's the default behavior of the println() function.

On the second println(), you are not overriding the toString() method, so println() uses the default one (inherited from Object).

On a side note, try formatting your code correctly, is much easier to read and understand.

abstract class myAbstractClass{}

public class Main { 
    public static void main(String[] args) { 
        System.out.println(new myAbstractClass(){
            public String toString(){
                return "myAbstractClass toString";
            }
        });

        System.out.println(new myAbstractClass(){
            public String myFunction(){
                return "myAbstractClass myFunction";
            }
        }); 
    } 
}
微凉徒眸意 2024-09-11 11:46:53

myAbstractClass 是一个最小的类。它继承自对象。

main 类从 myAbstractClass 构造两个匿名内部类,并打印它们的 toString 输出。

  1. 内部类重写了 toString 方法,您可以看到它的输出。
  2. 内部类添加了一个方法,并且您使用默认的 toString 定义。

myAbstractClass is a minimal class. It inherits from object.

Class main constructs two anonymous inner classes from myAbstractClass, and prints their toString output.

  1. The inner class overrides the toString method and you see its output.
  2. The inner class get a method added, and you the the default toString definition.
北凤男飞 2024-09-11 11:46:53

在这两种情况下,您都已打印该对象。所以它会调用对象的toString()方法。在第一个场景中,由于您已经重写了 toString() 方法,因此它正在打印 myAbstractClass toString 。在第二种情况下,由于它没有被重写,因此它调用 Object 类中实现的默认 toString()

我认为您期望在第二种情况下进行函数调用,这是错误的。您刚刚覆盖了它但从未调用过。

In both scenarios, you have printed the object. So it will call toString() method of the object. In the first scenario, since you have overridden the toString() method, so it is printing myAbstractClass toString. In the second scenario, since it was not overridden, it calls the default toString() implemented in the Object class.

I think you are expecting function call in the 2nd scenario which is wrong. You have just overridden it but never called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文