放在哪里超()或()这个在我的课 [英] Where to put super() or this() in my classes

查看:130
本文介绍了放在哪里超()或()这个在我的课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Servlet类我做处理功能,我不希望每次的Servlet我有重复的。我还在上(即,它仍然只加载的index.jsp而不是其他的文件)。

I have a Servlet class I made to handle functions I don't want to repeat on every Servlet I have. I'm still working on it (i.e. it still only loads index.jsp and not other files).

public class MyServlet extends HttpServlet {
    public MyServlet () {
        super();
    }

    public void loadView (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");

        response.setContentType("text/html;charset=UTF-8");

        System.out.println("MyServlet::LoadView() success");

        dispatcher.forward(request, response);
    }
}

我的servlet如下:

My Servlet is as follows

@WebServlet(name = "EditServlet", urlPatterns = {"/content/edit"})
public class EditServlet extends Library.MyServlet {
    public EditServlet () {
        super();
    }

    public void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("EditServlet loaded");
    }
}

不过,我,无法编译我的code:

I am however, unable to compile my code:

SEVERE: Exception while deploying the app [ContentManagement] : (class: contentmanagement/content/EditServlet, method: <init> signature: ()V) Constructor must call super() or this()

更新

好吧,删除无效我的构造函数,并调用超(); 得到了必须先打电话部分消失,但它仍然说我必须调用超级尽管它已经被调用。

Ok, removing void on my constructors and calling super(); got the must call first portion to go away, but it's still saying I have to call super despite that it's already being called.

更新

我不明白下面的反应....他们一直说把超()的构造函数中,当我的例子已经表明,正在做的,它是code的第一行。任何其他的建议是AP preciated。

I don't understand the responses below.... they keep saying to put super() within the constructor, when my examples already show that being done, AND it is the first line of code. Any other advice would be appreciated.

有什么想法?

现在这是一个非问题。我不知道有什么解决这个问题,但有多个变化,从地面重建我的应用程序了,我不再遇到此问题

推荐答案

您的问题是:

方法:其中,初始化&GT;签名:()V)构造函数必须调用超()或本()

看起来的VerifyError 症状。清理并重建项目应该可以解决这个问题,参见:

It seems like VerifyError symptom. Cleaning and rebuilding the project should resolve the problem, see also:

此外,您可以用超()单行删除您的构造函数。在JVM会为你使用一个默认值(隐式隐藏)构造函数。因此,清除这些不必要的行:

Furthermore, you can remove your constructors with a super() single-line. The JVM does it for you with a default (implicitly hidden) constructor. So, clear these unnecessary lines:

public MyServlet () {
    super();
}

public EditServlet () {
    super();
}

您将必须显式调用超()如果你需要写在构造函数中额外的业务线。

You would have to explicitly call super() if you needed to write additional business lines in the constructor.

这篇关于放在哪里超()或()这个在我的课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆