【问题标题】:Implementing a Demo/Tester Program实施演示/测试程序
【发布时间】:2017-03-05 16:28:48
【问题描述】:

我的 Java 编程课程有一个项目。

说明是我们要创建一个简单的类和一个测试器类,并且该类必须包含一个Default构造函数;具有三个参数(品牌、型号和价格)的参数化构造函数;访问器方法调用 getMake() 来返回 make;访问器方法调用getModel()返回模型;访问器方法调用 getPrice() 返回价格; Mutator方法setMake(String newMake)设置make; Mutator方法setModel(String newModel)设置模型;和一个 Mutator 方法 setPrice( double newPrice ) 来设置价格..

我已经创建了我的类和测试程序,我的类可以完美编译。 当我尝试运行它时,虽然得到了没有主方法的错误。现在,我按照教授的示例进行了测试程序,但我遇到了几个错误。如果有人能给我一个正确方向的指针,我将不胜感激。

我的问题是:如何实现我的测试程序?我需要创建一个 zip 文件吗?我试过这样做,但似乎没有多大帮助......

以下是我的课程代码:

public class Automobile
{ 
    private String make;    
    private String model;
    private double price;   

    public Automobile()
    {
        make = "Lexus2017";
        model = "RX";
    }

    public Automobile(String initMake, String initModel, double initPrice)
    {
        make = initMake;
        model = initModel;
        price = initPrice; 
    }

    public String getMake()
    {
        return make;
    }

    public String getModel()
    {
        return model;
    }

    public double getPrice()
    {
        return price;
    }   

    public void setMake(String newMake)
    {
        make = newMake;
    }

    public void setModel(String newModel)
    {
        model = newModel;
    }

另外,下面是我的测试类(有很多错误的):

public class AutomobileTester
{
    public static void main(String[] args)
    {
        Automobile make = new Automobile("Lexus 2017");
        System.out.println("The car is " + make.getMake());

        Automobile model = new Automobile("RX");
        System.out.println("The car is " + Automobile.getModel());

        Automobile price = new Automobile("43020"); 
        System.out.println("The car is " + Automobile.getPrice());  

        // Use the mutator to change the make variable
        Automobile.setMake("Lexus 2017");
        System.out.println("The car is " + backDoor.getState());

        // Use the mutator to change the model variable
        Automobile.setModel("RX");
        System.out.println("The car is called " + backDoor.getName());

        Automobile.setPrice("43020");
        System.out.println("The car is " + price.getPrice());
    }
}

这是我第一次使用构造函数,而且我对 Java 还很陌生,所以对于任何明显的错误,我深表歉意。提前感谢您的时间和帮助。

【问题讨论】:

  • Parameterized constructor with three parameters (make, model and price); 不匹配 new Automobile("Lexus 2017");
  • 不要使用类名来访问方法...这里有很多错误。您是否希望我们将它们全部修复并为您完成任务?
  • @cricket_007 不。那太荒谬了,我不会那样学任何东西。我只是想了解如何实现一个类,因为我已经将我的章节通读了三遍,听了讲义,但有些东西不适合我。我只想将我的代码放入上下文中。
  • 好的。我已经回答了我看到的直接问题。汽车类看起来不错
  • @ScaryWombat 那么我会通过摆脱汽车年来解决它吗?

标签: java methods constructor implementation mutators


【解决方案1】:

您在构造函数调用中有错误。 您的构造函数采用三个参数(品牌、型号和价格),但是当您调用该方法时只发送一个。那是一个错误。 默认情况下,Java 类构造函数不接受任何参数(在您的情况下,这将是“new Automobile ()”)。 要实现测试器,您有两个选择。 首先使用不带参数的构造函数创建汽车,然后设置参数:

Automobile auto = new Automobile();
auto.setMake("Lexus 2017");
auto.setModel("RX");
auto.setPrice(43020);

汽车汽车品牌=新汽车();

另一种选择是使用您自己的构建器并传递参数:

Automobile auto2 = new Automobile("Lexus 2017", "RX", 43020);

Automobile.java:

public class Automobile {
private String make;
private String model;
private double price;

public Automobile() {
}

public Automobile(String make, String model, double price) {
    this.make = make;
    this.model = model;
    this.price = price;
}

public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}
}

AutomobileTester.java:

public class AutomobileTester {

public static void main(String[] args) {

    Automobile auto = new Automobile();
    auto.setMake("Lexus 2017");
    auto.setModel("RX");
    auto.setPrice(43020);
    System.out.println("The car1 is " + auto.getMake() + " " + auto.getModel() + " " + auto.getPrice());

    Automobile auto2 = new Automobile("Lexus 2017", "RX", 43020);
    System.out.println("The car2 is " + auto2.getMake() + " " + auto2.getModel() + " " + auto2.getPrice());

}

}

【讨论】:

  • 不会汽车汽车制造 = new();是多余的吗?另外,我知道当您希望方法中的参数等于实例变量时,您会使用此参数。您什么时候不想使用它们?
  • 是的,这是一个错误。它将是汽车 auto = new Automobile();如果您只想使用多个参数,您可以实现多个构造函数。例如,如果您只想使用价格: public Automobile(double price) { this.price = price;调用将是汽车 auto = new Automobile(1000);或者如果你想使用价格和型号: public Automobile(String model, double price) { this.model = model; this.price = 价格;调用将是: Automobile auto = new Automobile("Rx", 1000);
【解决方案2】:

你做对了。您使用Automobile 类的make 实例变量访问了该方法。

(旁注:对于汽车实例来说,make 是个坏名字,不如叫它 car1 之类的)

Automobile make = new Automobile("Lexus 2017");
System.out.println("The car is " + make.getMake());

现在,在您使用Automobile.someMethod() 的其他任何地方,这是不对的,因为您需要设置或获取类的一个实例 上的数据,而不是整个类。

然后,最后,您需要使用该类中的三个参数来测试构造函数。

【讨论】:

  • 谢谢。 “make”是我的教授希望我们使用的实例变量,但我必须创建两个汽车对象,所以 car1、car2 可以工作吗?我会使用类似 setMake(String newMake) 的东西吗?对每个实例变量都这样做吗?
  • 我不是在谈论 String make,我是在谈论 Automatible make = new Automobile(...),因为那是一辆有品牌的汽车,是的,但不是品牌本身
【解决方案3】:

第一个问题是您没有使用正确数量的参数来调用构造函数,在 Java(和大多数编程语言)中,您必须向方法/函数/构造函数提供所有必需的参数一次通话。您的代码的修复方法是使用:

Automobile car = new Automobile("Lexus 2017", "RX", 43020.0D);

此外,当您打印出汽车信息时,您首先使用instance 调用,然后使用static 调用,我不会过多介绍两者之间的区别,但基本上instance 调用需要您实例化一个对象,而 static 没有。解决此问题的方法是:

System.out.println("The car is a " + car.getMake() + ", the brand is " + car.getModel() + ", the price is $" + car.getPrice());

至于改变你应该使用的变量:

car.setMake("My New Car Make");

代替:

Automobile.setMake("My New Car Make");

对于staticinstance 之间的区别,您可以查看hereherehere

【讨论】:

  • 当您有'Automobile car = new Automobile("Lexus 2017", "RX", 43020.0D);'时,43020 后面的D 是否将价格识别为双变量?另外,我的作业说要使用实例变量,而不是静态的。直到下一章才会出现。
  • 类名呢?那些还好吗?汽车和汽车测试仪?通过这些名称,程序是否知道要实现该类?
  • dD 字符表示该号码的类型为 double,您可以找到有关这些类型标识符 here 的更多信息。至于您对静态变量和实例变量的使用,我指出您在应该使用实例调用的地方使用静态调用。至于名称,您可以使用任何您想要的名称,只要它们的输入完全相同,它应该能够正确实例化该类,IDE 可以帮助解决这个问题。
  • 感谢您抽出宝贵时间提供帮助。我真的很感激。
猜你喜欢
  • 1970-01-01
  • 2021-07-20
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2020-02-17
  • 1970-01-01
相关资源
最近更新 更多