调用 super() 必须是方法内的第一条语句消息

这是我的基类:


@Service

public class BasicUserManagerService implements UserManager {

    @Autowired

    private UserRepository UserRepository;


    private Logger logger = LoggerFactory.getLogger(UserManagerPasswordController.class);


    @Override

    public void createUser(User User) {

        if (UserRepository.findByEmail(User.getEmail()) != null)

            throw new InvalidDataException("User already registered with this email address");

        UserRepository.save(User);

        logger.info("Created user: {}", User.getEmail());

    }

}

我正在尝试按如下方式扩展这个类:


@Service

public class UserManagerService extends BasicUserManagerService implements UserManager {

    @Override

    public void createUser(User User) {

        super().createUser(User);

    }

}

但是我不断收到错误,即对 super() 的调用必须是构造函数体中的第一条语句。如您所见,我没有构造函数,即使添加它,也会出现相同的错误。为什么会发生这种情况,我该如何避免?


绝地无双
浏览 161回答 3
3回答

MYYA

改变这种super用法。super()是父的构造函数。super是对父类的引用。@Servicepublic class UserManagerService extends BasicUserManagerService implements UserManager {    @Override    public void createUser(ProxyCircuitUser proxyCircuitUser) {        super.createUser(proxyCircuitUser);    }}

慕莱坞森

super()是对父类构造函数的调用;这根本不是你想要的。相反,您希望调用该createUser方法的父类实现。代码是:super.createUser(user)

慕的地10843

以下是Java中Super关键字的各种用法:super 与变量的使用当派生类和基类具有相同的数据成员时,就会发生这种情况。在这种情况下,JVM 可能存在歧义。/* Base class vehicle */class Vehicle {     int maxSpeed = 120; } /* sub class Car extending vehicle */class Car extends Vehicle {     int maxSpeed = 180;     void display()     {         /* print maxSpeed of base class (vehicle) */        System.out.println("Maximum Speed: " + super.maxSpeed);     } } /* Driver program to test */class Test {     public static void main(String[] args)     {         Car small = new Car();         small.display();     } } 输出:Maximum Speed: 120使用 super with 方法这在我们要调用父类方法时使用。因此,每当父类和子类具有相同的命名方法时,我们就使用 super 关键字来解决歧义。/* Base class Person */class Person {     void message()     {         System.out.println("This is person class");     } } /* Subclass Student */class Student extends Person {     void message()     {         System.out.println("This is student class");     }     // Note that display() is only in Student class     void display()     {         // will invoke or call current class message() method         message();         // will invoke or call parent class message() method         super.message();     } } /* Driver program to test */class Test {     public static void main(String args[])     {         Student s = new Student();         // calling display() of Student         s.display();     } } 输出:This is student classThis is person class将 super 与构造函数一起使用super 关键字也可用于访问父类构造函数。更重要的一点是,“super”可以根据情况调用参数和非参数构造函数。/* superclass Person */class Person {     Person()     {         System.out.println("Person class Constructor");     } } /* subclass Student extending the Person class */class Student extends Person {     Student()     {         // invoke or call parent class constructor         super();         System.out.println("Student class Constructor");     } } /* Driver program to test*/class Test {     public static void main(String[] args)     {         Student s = new Student();     } }输出:Person class ConstructorStudent class Constructor由于super()会调用父类的构造函数,所以它应该是子类构造函数中要执行的第一条语句。如果要调用父类的方法,请使用super而不是super()。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java