gpt4 book ai didi

java - 私有(private)构造函数干扰公共(public)方法测试

转载 作者:行者123 更新时间:2023-12-01 19:52:02 25 4
gpt4 key购买 nike

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

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() + "";
}
}

最佳答案

您在 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
}
}

关于java - 私有(private)构造函数干扰公共(public)方法测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51074073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com