写一个测试public class Flight { private String aircraftType; private String flightNumber; private int capacity; private int actualPassengers; public Flight(String aircraftType, String flightNumber, int capacity, int actualPassengers) { this.aircraftType = aircraftType; this.flightNumber = flightNumber; this.capacity = capacity; this.actualPassengers = actualPassengers; } public void input() { Scanner scanner = new Scanner(System.in); System.out.print("请输入机型:"); this.aircraftType = scanner.nextLine(); System.out.print("请输入班次:"); this.flightNumber = scanner.nextLine(); System.out.print("请输入额定载客数:"); this.capacity = scanner.nextInt(); System.out.print("请输入实际载客数:"); this.actualPassengers = scanner.nextInt(); } public void output() { System.out.println("机型:" + this.aircraftType); System.out.println("班次:" + this.flightNumber); System.out.println("额定载客数:" + this.capacity); System.out.println("实际载客数:" + this.actualPassengers); System.out.println("载客率:" + this.getLoadFactor()); } public double getLoadFactor() { return (double) this.actualPassengers / this.capacity; } @Override public String toString() { return "Flight{" + "aircraftType='" + aircraftType + '\'' + ", flightNumber='" + flightNumber + '\'' + ", capacity=" + capacity + ", actualPassengers=" + actualPassengers + '}'; }}的代码
时间: 2024-03-23 21:36:32 浏览: 63
我自己写的一些测试代码
可以写一个FlightTest类来测试Flight类的各个方法,例如:
```
import java.util.Scanner;
public class FlightTest {
public static void main(String[] args) {
// 创建Flight对象
Flight flight = new Flight("空客A320", "CA1234", 180, 120);
// 输出Flight对象的属性值
flight.output();
// 更新Flight对象的属性值
flight.input();
// 再次输出Flight对象的属性值
flight.output();
}
}
```
在上面的代码中,我们首先创建了一个Flight对象,并调用其output方法输出Flight对象的属性值。然后,调用其input方法更新Flight对象的属性值,最后再次调用output方法输出更新后的Flight对象的属性值。这样就可以测试Flight类的各个方法是否能够正常工作了。
阅读全文