没有参数的构造函数

时间:2014-05-12 18:23:29

标签: java netbeans constructor arguments

我有这个类,但我需要以构造函数(public StemmingAndLemmatization(TurkishMorphParser parser))没有参数的方式编写它(public StemmingAndLemmatization())!我该怎么转换它?任何想法?

    public class StemmingAndLemmatization {
        TurkishMorphParser parser;

        public StemmingAndLemmatization(TurkishMorphParser parser) {
            this.parser = parser;
        }

        public void parse(String word) {
            System.out.println("Word = " + word);

            System.out.println("Parses: ");
            List<MorphParse> parses = parser.parse(word);
            for (MorphParse parse : parses) {
                System.out.println(parse.formatLong());
                System.out.println("\tStems = " + parse.getStems());
                System.out.println("\tLemmas = " + parse.getLemmas());
            }
        }

        public static void main(String[] args) throws IOException {
            TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();
            new StemmingAndLemmatization(parser).parse("kitabımızsa");
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要使用将接收解析器的方法替换构造函数的功能,或者您可以让构造函数创建自己的解析器。

public class StemmingAndLemmatization {
    TurkishMorphParser parser;

    public void setParser(TurkishMorphParser parser) {
       this.parser = parser;
    }

public StemmingAndLemmatization(TurkishMorphParser) {
     this.parser = TurkishMorphParser.createWithDefaults();
 }