Home  >  Q&A  >  body text

java - 关于Object匿名内部类的一些问题

class Demo1
{
    public void fun()
    {
        
        System.out.println(new Object(){
        int age = 23;
        String name = "李四";
        public String toString()
        {
            return name+","+age;
        }
        });
        
    }
}
class Demonmnbl2 
{
    public static void main(String[] args) 
    {
        Demo1 de = new Demo1();
        de.fun();
    }
}

这个Object的匿名内部类有输出!
同理下面的代码
class Demo1

{
    public void fun()
    {        
        new Object()
        {    String name1 = "搜索";
            public String toString()
            {
            return name1+",";
            }
        }
        .toString();
    }
}
class Demonmnbl2 
{
    public static void main(String[] args) 
    {
        Demo1 de = new Demo1();
        de.fun();
    }
}

这个代码没有了输入这是怎么回事呢??

PHP中文网PHP中文网2713 days ago453

reply all(2)I'll reply

  • 阿神

    阿神2017-04-18 10:56:04

    Did you print the first one? ? ?

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:56:04

    The second piece of code is missing System.out.println, how can there be output.

    reply
    0
  • Cancelreply