谁能解释以下代码的工作原理...?
谁能解释以下代码的工作原理...?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当你打印出一个对象时,它会调用 toString() 方法。在第一个语句中,您创建新对象并覆盖名为 toString 的方法。因此,当打印对象时会调用这个 toString() 方法。
在第二条语句中,您还创建了一个对象,但没有重写 toString() 方法,以便它将使用 Object 类的 toString() 方法。
对于新问题,这个链接有很好的解释供您理解:
匿名类
以下是解释的副本:
此表达式从未命名且先前未定义的类实例化一个新对象,该类自动扩展名为 className 的类,并且无法显式实现任何接口。新类的主体由 classBody 给出。
执行该表达式的结果是定义了一个扩展className的新类,实例化了一个新类的新对象,并且表达式被替换为对新对象的引用。
此表达式从未命名且先前未定义的类实例化一个新对象,该类自动实现名为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:
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.
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?
在第一个
println()
上,您将重写匿名myAbstractClass
实例中的toString()
方法,因此您将获得重写返回的字符串toString()
方法,这是println()
函数的默认行为。在第二个
println()
上,您没有重写toString()
方法,因此println()
使用默认方法(继承自 <代码>对象)。附带说明一下,尝试正确格式化代码,这样更容易阅读和理解。
On the first
println()
you are overriding thetoString()
method from the anonymousmyAbstractClass
instance, therefore you get the string returned by your overridentoString()
method, that's the default behavior of theprintln()
function.On the second
println()
, you are not overriding thetoString()
method, soprintln()
uses the default one (inherited fromObject
).On a side note, try formatting your code correctly, is much easier to read and understand.
myAbstractClass 是一个最小的类。它继承自对象。
main 类从 myAbstractClass 构造两个匿名内部类,并打印它们的 toString 输出。
myAbstractClass is a minimal class. It inherits from object.
Class main constructs two anonymous inner classes from myAbstractClass, and prints their toString output.
在这两种情况下,您都已打印该对象。所以它会调用对象的
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 thetoString()
method, so it is printingmyAbstractClass toString
. In the second scenario, since it was not overridden, it calls the defaulttoString()
implemented in theObject
class.I think you are expecting function call in the 2nd scenario which is wrong. You have just overridden it but never called.