Java 一个类不会导入另一个类如何解决此问题?
我创建了一个包含常量的类,如图所示,所有类都位于默认的src文件夹中Java 一个类不会导入另一个类如何解决此问题?,java,constants,Java,Constants,我创建了一个包含常量的类,如图所示,所有类都位于默认的src文件夹中 public final class SectionType { public static final String LABS = "LABS"; public static final String LAB_REPORTS = "LABREPORTS"; public static final String HOMEWORK = "HOMEWORK"; public static final
public final class SectionType {
public static final String LABS = "LABS";
public static final String LAB_REPORTS = "LABREPORTS";
public static final String HOMEWORK = "HOMEWORK";
public static final String EXAM01 = "EXAM 1";
public static final String EXAM02 = "EXAM 2";
public static final String FINAL = "FINAL";
}
我现在需要在另一个类似这样的类中使用这些常量
import static SectionType.*;
public class Student {
public void addGrade(SectionType type, double grade) {
if(type.equals(EXAM01)) {
this.exam01 = grade;
}
if(type.equals(EXAM02)) {
this.exam02 = grade;
}
if(type.equals(FINAL)) {
this.finalExam = grade;
}
if(type.equals(LABS)) {
this.labSum += grade;
this.labCount++;
}
if(type.equals(LAB_REPORTS)) {
this.labRSum += grade;
this.labRCount++;
}
if(type.equals(HOMEWORK)) {
this.hwSum += grade;
this.hwCount++;
}
}
}
什么是实现此功能的最佳方法?如果您正在寻找导入,则只需像导入任何其他包一样导入该类即可
import path.where.the.package.is.className;
不过最好将类复制到您正在处理的项目中,然后导入它
还有一些建议是:
static
,即publicstaticfinal类SectionType
,因此使用该类时不需要创建该类的实例SectionType不是一个对象,但您正在尝试按原样使用它。它只是一个具有静态成员的类,没有一个类的不同实例的概念,每个实例都有自己的值 要使现有代码正常工作,您可以将代码更改为为为节类型传入字符串,如下所示:
import static SectionType.*;
public class Student {
public void addGrade(String type, double grade) {
if(type.equals(EXAM01)) {
this.exam01 = grade;
}
if(type.equals(EXAM02)) {
this.exam02 = grade;
}
if(type.equals(FINAL)) {
this.finalExam = grade;
}
if(type.equals(LABS)) {
this.labSum += grade;
this.labCount++;
}
if(type.equals(LAB_REPORTS)) {
this.labRSum += grade;
this.labRCount++;
}
if(type.equals(HOMEWORK)) {
this.hwSum += grade;
this.hwCount++;
}
}
}
但最好将SectionType设置为枚举:
public enum SectionType {
LABS, LAB_REPORTS, HOMEWORK, EXAM01, EXAM02, FINAL
}
这里特别提到了SectionType的概念,其中每个值都有一个SectionType实例(LABS是一个SectionType,LAB_REPORTS是一个不同的SectionType,等等),这就是代码希望使用它的方式
(使用枚举的优点是,您可以利用编译时类型检查。如果传入无效的字符串值,则代码将在不执行任何操作的情况下失败。但使用枚举的代码不会发生这种情况,您必须先将输入值转换为有效的枚举,然后才能调用代码。输入字符串值时,它将使用SectionType.valueOf将转换为枚举,如果该值无效,将引发异常。)
听起来你被要求做的是实现过时的,比如:
这与枚举非常相似,只是更详细,更容易出错。此模式的问题是创建枚举的动机。有关实现类型安全枚举不是一个好主意的原因列表,请参阅。addGrade类更改为以下内容
public void addGrade(String type, double grade) {
if(type.equals(SectionType.EXAM01)) {
this.exam01 = grade;
}
if(type.equals(SectionType.EXAM02)) {
this.exam02 = grade;
}
if(type.equals(SectionType.FINAL)) {
this.finalExam = grade;
}
if(type.equals(SectionType.LABS)) {
this.labSum += grade;
this.labCount++;
}
if(type.equals(SectionType.LAB_REPORTS)) {
this.labRSum += grade;
this.labRCount++;
}
if(type.equals(SectionType.HOMEWORK)) {
this.hwSum += grade;
this.hwCount++;
}
}
请查看页面。您遇到的实际问题是什么?您只需执行以下操作:
导入您的.package.SectionType.LAB_报表(例如;
)是的,这就是导入常量以在类似这样的另一个类中使用的方式。但出于好奇,您是否考虑过对SectionType使用枚举?尝试时发生了什么?(您是否收到错误?哪一个?@x80486这是在默认软件包中我是否需要更改任何内容类位于我尝试的根文件夹中。/and root,两者都不起作用您可以指定类所在的路径吗???@exedition:确定。顺便说一句,我添加了听起来像你的课程想要的方法,以及如果你在现实生活中这样做的问题警告。
public void addGrade(String type, double grade) {
if(type.equals(SectionType.EXAM01)) {
this.exam01 = grade;
}
if(type.equals(SectionType.EXAM02)) {
this.exam02 = grade;
}
if(type.equals(SectionType.FINAL)) {
this.finalExam = grade;
}
if(type.equals(SectionType.LABS)) {
this.labSum += grade;
this.labCount++;
}
if(type.equals(SectionType.LAB_REPORTS)) {
this.labRSum += grade;
this.labRCount++;
}
if(type.equals(SectionType.HOMEWORK)) {
this.hwSum += grade;
this.hwCount++;
}
}