【发布时间】:2018-04-26 04:12:10
【问题描述】:
我正在尝试创建满足以下要求的测试方法:
*创建一个名为 Demo.java 的类。这个类将包含你的主要方法
使用默认构造函数创建类的实例。
调用你所有的对象集合方法来给你的对象赋值
调用对象的显示方法,打印出它的值
使用参数化构造函数创建您的类的另一个实例
调用对象的显示方法,打印出它的值。*
**以下是我的代码,但是第一行出现错误。编译器告诉我必须将名为“Demo”的类重命名为其他名称,但我需要将其命名为 Demo,所以我不知道如何从这里开始。
public class Demo {
public static void main(String[] args) {
Netflix p1 = new Netflix();
p1.setPrice(11.99);
p1.setTitle("Expert");
p1.setTypeNumber(2000);
p1.display();
Netflix p2 = new Netflix(100, 7.99, "Novice");
p2.display();
}
}
public class Netflix {
private int typeNumber;
private double price;
private String title;
public Netflix() {
}
public Netflix(int typeNumber, double price, String title) {
super();
this.typeNumber = typeNumber;
this.price = price;
this.title = title;
}
public void display() {
System.out.println( "Netflix [price=" + price + ", title=" + title + ", typeNumber="
+ typeNumber + "]");
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getTypeNumber() {
return typeNumber;
}
public void setTypeNumber(int typeNumber) {
this.typeNumber = typeNumber;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
【问题讨论】:
-
您的要求有误。
-
您的代码所在文件的名称是什么?
-
@BinaryBuilder Netflix.java
-
类
Demo应该在文件Demo.java中。此文件中应仅包含一个public类(即在您的Demo.java中从Netflix类中删除public) -
您的要求是否表明您必须在 Netflix.java 中拥有演示类?如果不是,我会将其放在自己的文件中或使其成为内部类。