在java中重写匿名类的方法

k4ymrczo  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(424)

在下面的例子中(在注解块中),我试图覆盖 jump() 方法,但出现编译错误。有人能帮我理解这里出了什么问题吗?

class Animal {
    public void bark() {
        System.out.println("Inside bark");
    }

    public void jump() {
        System.out.println("Inside jump");
    }
}

public class AnonymousClassExample001 {
    public static void main(String[] args) {
        Animal animal = new Animal() {
            public void bark() {
                System.out.println("Subclass bark");
            }
        }; /* {
            public void jump() {
                System.out.println("Subclass jump");
            }
        };*/

        /**
        * Question is: Why can't we override by extending an anonymous class
        * as attempted (and commented) above?
        * */
        animal.bark(); // Subclass bark
        animal.jump(); // Trying to print "Subclass jump", by overriding (the subclass of Animal) above
    }
}

编辑:这是我在ide中遇到的编译错误- ';' expected .
从评论来看,似乎有些人不明白我的问题。在上面的例子中,我想了解是否可以重写匿名类的方法?。
同样,主要目的是了解(并理解)为什么java编译器允许通过启动 {} 块后跟 new Animal() ,并且不允许进一步出现相同的行为(链接 {} 允许创建更多子类的块)

ff29svar

ff29svar1#

我想应该是这样

public class AnonymousClassExample001 {
public static void main(String[] args) {
    Animal animal = new Animal() {
        public void bark() {
            System.out.println("Subclass bark");
        }

        public void jump() {
            System.out.println("Subclass jump");

        }
    };
    /**
     * Question is: Why can't we override by extending an anonymous class
     * as attempted (and commented) above?
     * */
    animal.bark(); // Subclass bark
    animal.jump(); // Trying to print "Subclass jump", by overriding (the subclass of Animal) above
}
}
uurity8g

uurity8g2#

不要结束并重新启动块,在一个块中重写两个方法,就像在常规子类中一样:

class AnonymousClassExample001 {
    public static void main(String[] args) {
        Animal animal = new Animal() {
            @Override
            public void bark() {
                System.out.println("Subclass bark");
            }
            @Override
            public void jump() {
                System.out.println("Subclass jump");
            }
        };

        animal.bark(); // Subclass bark
        animal.jump(); // Subclass jump
    }
}

更新
问题的更新说明您正在尝试为匿名类创建子类,例如,您正在尝试执行以下操作:

class AnonymousClassExample001 {
    public static void main(String[] args) {
        Animal barkingAnimal = new Animal() {
            @Override
            public void bark() {
                System.out.println("Subclass bark");
            }
        };
        Animal jumpingAnimal = <subclass barkingAnimal> { // Can't be done
            @Override
            public void jump() {
                System.out.println("Subclass jump");
            }
        };

        barkingAnimal.bark(); // Subclass bark
        barkingAnimal.jump(); // Inside jump

        jumpingAnimal.bark(); // Subclass bark
        jumpingAnimal.jump(); // Subclass jump
    }
}

不能这样做,因为无法将未命名的匿名类标识为基类。但是,您可以使用本地类来实现这一点,这些类类似于命名的匿名类,听起来很矛盾:

class AnonymousClassExample001 {
    public static void main(String[] args) {
        class BarkingAnimal extends Animal {
            @Override
            public void bark() {
                System.out.println("Subclass bark");
            }
        };
        class JumpingAnimal extends BarkingAnimal {
            @Override
            public void jump() {
                System.out.println("Subclass jump");
            }
        };
        Animal barkingAnimal = new BarkingAnimal();
        Animal jumpingAnimal = new JumpingAnimal();

        barkingAnimal.bark(); // Subclass bark
        barkingAnimal.jump(); // Inside jump

        jumpingAnimal.bark(); // Subclass bark
        jumpingAnimal.jump(); // Subclass jump
    }
}

或者, jumpingAnimal 也可以使用匿名类语法,而不是本地类语法。

相关问题