Home  >  Q&A  >  body text

java - 在这儿并没有申明FU为Demo的父类,为什么还是可以继承重写呢

//此处用了匿名内中类
    abstract class Fu
    {
        public abstract void sleep();
    }
    class Demo
    {    
        public void fun()
        {
        //创建匿名对象,可以使用匿名类中类
            new Fu()
            {
            public void sleep()
                {
                System.out.println("睡觉");
                }
            public void eat()
                {
                System.out.println("吃饭");
                }
            
            }
            .sleep();
    //创建匿名对象
            new Fu()
            {
            public void sleep()
                {
                System.out.println("睡觉");
                }
            public void eat()
                {
                System.out.println("吃饭");
                }
            
            }
            .eat();
            
        }
    }
    class Demonmnbl1 
    {
        public static void main(String[] args) 
        {
            Demo de1 = new Demo();
            de1.fun();
        }
    }
迷茫迷茫2743 days ago420

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:55:47

    It’s not rewritten by Demo, it’s rewritten by two anonymous subclasses inside. . Demo is just responsible for calling

    reply
    0
  • 阿神

    阿神2017-04-18 10:55:47

    Demo does not inherit abstract classes. Just contains instances of the Fu class.

    I have never used JAVA, how can an abstract class be instantiated? This is what's strange.

    I feel that new Fu() should be equivalent to something = new Fu(). An anonymous object that inherits Fu. This is polymorphism in OOP.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:55:47

    When you define an anonymous inner class, the JVM will automatically generate a class for you, which is said to be anonymous. In fact, for the JVM, of course it is not anonymous. The JVM knows this class, but the user does not. The user cannot access this class. This class is The subclass or implementation class of your new class or interface, its reference is the parent class or interface.

    reply
    0
  • Cancelreply