【问题标题】:Creating a Test Method for Main Method为 Main 方法创建测试方法
【发布时间】: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 中拥有演示类?如果不是,我会将其放在自己的文件中或使其成为内部类。

标签: java testing methods


【解决方案1】:

Demo.java

public class Demo {

    public static void main(String[] args) {

        Netflix p1 = new Netflix();
    }
}

class Netflix {
}

【讨论】:

  • 我试过了,不幸的是它在第一行给了我同样的错误
  • 对不起,我忘了更改文件名。成功了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多