Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

关于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();
    }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

第一个你是打印了吗???


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...