“构造方法调用必须是构造方法中的第一个语句”问题在Java [英] "Constructor call must be the first statement in a constructor" issue in Java

查看:231
本文介绍了“构造方法调用必须是构造方法中的第一个语句”问题在Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我想在Java中有构造函数链。例如,使用第一个构造函数,我有一个字符串作为参数,并调用第二个构造函数,因为我从参数字符串创建一个对象。

I'd like to have constructor chain in Java. For example, with the first constructor I have a string as a parameter, and call the second constructor as I create an object from the parameter string.

public class IMethodFinder {
    public IMethodFinder(String projectName, String methodName,
        int numberOfParameters) {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        IJavaProject javaProject = JavaCore.create(project);
        this(javaProject, methodName, numberOfParameters);
    }

    public IMethodFinder(IJavaProject javaProject, String methodName,
        int numberOfParameters) {
        ... 
    }
}

但是,我得到一个错误构造函数调用必须是构造函数中的第一个语句

However, I got an error "Constructor call must be the first statement in a constructor" error.

>

我创建了一个在两个构造函数之间共享的公共代码,但我不确定这是绕过问题的唯一解决方案。

I made a common code that is shared between the two constructors, but I'm not sure this is the only solution to bypass the issue.

public class IMethodFinder {
    public IMethodFinder(IJavaProject javaProject, String methodName,
            int numberOfParameters) {
        dosomething(javaProject, methodName, numberOfParameters);
    }

    public IMethodFinder(String projectName, String methodName,
            int numberOfParameters) {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        IJavaProject javaProject = JavaCore.create(project);
        dosomething(javaProject, methodName, numberOfParameters);
    }

    private void dosomething(IJavaProject javaProject, String methodName,
            int numberOfParameters)
    {
       ...  
    }

}




  • 为什么Java需要构造函数调用第一条语句?

  • 对于我的案例,Java的约定是什么?调用通用方法是一个很好的方法吗?

  • 推荐答案

    Java没有内在的原因,无法扩展为允许在构造函数之前访问 this 。但是,这将增加语言的复杂性和使用时掩盖代码(特别是当你认为调用可能是隐式的)。

    There is no intrinsic reason why Java could not be extended to allow statements that do not access this before the constructor. However, that would add to the language complexity and obscure the code when used (particularly when you consider the call may be implicit).

    通常你想保持构造函数简单尽可能。 init()方法是一个坏主意,因为它们阻止使用 final 。看起来代码正在访问一个可变的静态,这是一个很糟糕的主意。

    Generally you want to keep constructors as simple as possible. init() methods are a bad idea as they prevent the use of final. It appears that the code is accessing a mutable static which is a really bad idea.

    对于你的具体代码,你可以写:

    For your specific code, you can write:

        public IMethodFinder(String projectName, String methodName,
            int numberOfParameters) {
            this(
                JavaCore.create(
                    ResourcesPlugin.getWorkspace().getRoot().getProject(projectName)
                ),
                methodName,
                numberOfParameters
            );
        }
    

    更通用的黑客是调用构造函数:

    A more general hack is to call a static method within the call to the constructor:

    public class IMethodFinder {
        public IMethodFinder(String projectName, String methodName,
            int numberOfParameters) {
            this(createProject(projectName), methodName, numberOfParameters);
        }
    
        public IMethodFinder(IJavaProject javaProject, String methodName,
            int numberOfParameters) {
            ... 
        }
    
        private static IJavaProject createProject(String projectName) {
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            IJavaProject javaProject = JavaCore.create(project);
            return javaProject;
        }
    }
    

    这篇关于“构造方法调用必须是构造方法中的第一个语句”问题在Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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