【问题标题】:Private constructors interfering with public method testing [closed]干扰公共方法测试的私有构造函数[关闭]
【发布时间】:2018-06-28 14:08:30
【问题描述】:

为什么不能正确编译?我添加了 main 方法行以尝试对其进行测试,但是我得到了很多错误,除了说它们不是语句之外,还说私有构造函数是表达式的非法开头以及公共构造函数。它还要求我在我认为不必要的地方添加分号,因为它们是方法的开始。我不希望有人为我重新输入代码,但至少有人能指出我正确的方向并告诉我哪里出错了吗?

import java.util.Arrays 
public class Book{
    public static void main (String[] args) {



    private String title;           
    private String authors[];       


    public Book() {
        title = "Test";     
        authors = null;             
    }

    public Book(String title, String[] authors) {   
        this.title = title;
        this.authors = authors;     
    }
    public String getterTitle() {       
        return title;
    }
    public void setterTitle(String title) { 
        this.title = title;
    }
    public String[] getterAuthors() {       
        return authors; 
    }
    public void setterAuthors(String[] authors) {       
        this.authors = authors;
    }
    public String bookToString() {
        return "" + getterTitle() + " by " + getterAuthors() + "";
    }
}

【问题讨论】:

  • 您的导入行末尾似乎缺少分号。
  • 为了澄清一些术语,您有私有的成员变量,也称为字段。这些不是构造函数。构造函数是public Book()...public Book(String title, String[] authors)...。您可以发现这些,因为它们与类同名。
  • 请注意,“表达式的非法开头”意味着错误位于错误指示的行之前
  • 在这种情况下,main() 的右大括号 } 将解决其中一个答案中所述的问题。

标签: java constructor private getter-setter


【解决方案1】:

java.util.Arrays 后缺少分号,main 没有闭合 } 大括号:

import java.util.Arrays;

public class Book {
    private String title;           
    private String authors[];

    public Book(String title, String[] authors) {   
        this.title = title;
        this.authors = authors;     
    }

    public String getterTitle() {       
        return title;
    }

    public void setterTitle(String title) { 
        this.title = title;
    }

    public String[] getterAuthors() {       
        return authors; 
    }

    public void setterAuthors(String[] authors) {       
        this.authors = authors;
    }

    public String bookToString() {
        return "" + getterTitle() + " by " + getterAuthors() + "";
    }

    public static void main (String[] args) {
         // Do something here
    }
}

【讨论】:

    【解决方案2】:

    我想我知道你的意思了。

    所以你想要做 2 个类。

    这个是主要的。

    TestBook.java

        package testbook;
    
        public class TestBook {
    
            public static void main(String[] args) {
                // TODO code application logic here
                Book bookClass = new Book();
    
            }
        }
    

    这是类。

    Book.java

    package testbook;
    
    public class Book {
    
        private String title;
        private String authors[];
    
        public Book() {
            title = "Test";
            authors = null;
        }
    
        public Book(String title, String[] authors) {
            this.title = title;
            this.authors = authors;
        }
    
        public String getterTitle() {
            return title;
        }
    
        public void setterTitle(String title) {
            this.title = title;
        }
    
        public String[] getterAuthors() {
            return authors;
        }
    
        public void setterAuthors(String[] authors) {
            this.authors = authors;
        }
    
        public String bookToString() {
            return "" + getterTitle() + " by " + getterAuthors() + "";
        }
    }
    

    【讨论】:

      【解决方案3】:
      import java.util.Arrays; // the semi - colon is missing on this line  
      public class Book{
      private String title;         
      private String authors[]; // to make these variables global you 
                               // need to declare them outside of a method  
      
      public static void main (String[] args) {   
       } // you need to close the method before beginning with others
      

      更正上述内容应该会为您修复代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-16
        • 2011-02-08
        • 2017-04-30
        • 2016-12-03
        • 1970-01-01
        • 2015-07-19
        • 2020-05-16
        相关资源
        最近更新 更多