枚举测试类中怎么调用接口
时间: 2024-01-23 11:03:24 浏览: 33
在测试类中调用接口,需要先创建接口的实例对象,然后通过实例对象来调用接口的方法。具体步骤如下:
1. 导入接口类
在测试类中导入接口类,例如:
```java
import com.example.api.ExampleApi;
```
2. 创建接口实例对象
在测试类中创建接口实例对象,例如:
```java
ExampleApi api = new ExampleApi();
```
3. 调用接口方法
通过实例对象来调用接口的方法,例如:
```java
// 调用接口方法并获取返回值
String result = api.exampleMethod(param1, param2);
// 断言返回值是否符合预期
assertEquals(expectedResult, result);
```
其中,`param1`、`param2` 是调用接口方法所需要的参数,`expectedResult` 是预期的返回值,`assertEquals` 是断言方法,用于判断实际返回值是否等于预期返回值。
相关问题
1. 编写程序实现以下逻辑。 ① 抽象类Animal具有一个抽象方法walk。 ② 接口Flyable具有一个方法fly。 ③ 类Bird继承Animal并实现Flyable。 编写测试类BirdTest,在main方法中构造一个Bird对象并调用其walk和fly方法(重写这2个方法时,打印一行用于模拟的字符串即可)。 2. 接口MobilePhone定义了以下方法: void on(); // 开机 void off(); // 关机 void charge(); // 充电 分别采用如下两种方式创建两个实现MobilePhone接口的类的对象,并在测试类中分别对这两个对象调用上述方法。 ① 编写类IPhone实现MobilePhone接口。 ② 使用实现了MobilePhone接口的匿名内部类。 ③ 使用实现了MobilePhone接口的Lambda表达式。 3. 编写程序模拟用以表示直线的Line类,具体包含以下逻辑: ① 表示直线起点和终点的嵌套类Point,其包含描述横纵坐标的x、y字段。 ② 表示直线方向的嵌套枚举Direction,其包含描述若干方向的枚举常量。 在Line类中编写必要的代码,然后在测试类LineTest中构造几个Line对象并输出每个对象的有关信息。用Java代码写出上边的1、2、3
1.
```java
abstract class Animal {
public abstract void walk();
}
interface Flyable {
public void fly();
}
class Bird extends Animal implements Flyable {
@Override
public void walk() {
System.out.println("Bird is walking.");
}
@Override
public void fly() {
System.out.println("Bird is flying.");
}
}
public class BirdTest {
public static void main(String[] args) {
Bird bird = new Bird();
bird.walk();
bird.fly();
}
}
```
2.
```java
interface MobilePhone {
public void on();
public void off();
public void charge();
}
class IPhone implements MobilePhone {
@Override
public void on() {
System.out.println("IPhone is turned on.");
}
@Override
public void off() {
System.out.println("IPhone is turned off.");
}
@Override
public void charge() {
System.out.println("IPhone is charging.");
}
}
public class MobilePhoneTest {
public static void main(String[] args) {
MobilePhone phone1 = new IPhone();
phone1.on();
phone1.charge();
phone1.off();
MobilePhone phone2 = new MobilePhone() {
@Override
public void on() {
System.out.println("Anonymous phone is turned on.");
}
@Override
public void off() {
System.out.println("Anonymous phone is turned off.");
}
@Override
public void charge() {
System.out.println("Anonymous phone is charging.");
}
};
phone2.on();
phone2.charge();
phone2.off();
MobilePhone phone3 = () -> {
System.out.println("Lambda phone is turned on.");
System.out.println("Lambda phone is charging.");
System.out.println("Lambda phone is turned off.");
};
phone3.on();
phone3.charge();
phone3.off();
}
}
```
3.
```java
class Line {
static class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
enum Direction {
HORIZONTAL,
VERTICAL,
DIAGONAL
}
Point start;
Point end;
Direction direction;
public Line(Point start, Point end) {
this.start = start;
this.end = end;
if (start.x == end.x) {
direction = Direction.VERTICAL;
} else if (start.y == end.y) {
direction = Direction.HORIZONTAL;
} else {
direction = Direction.DIAGONAL;
}
}
@Override
public String toString() {
return "Line{" +
"start=" + start.x + "," + start.y +
", end=" + end.x + "," + end.y +
", direction=" + direction +
'}';
}
}
public class LineTest {
public static void main(String[] args) {
Line line1 = new Line(new Line.Point(0, 0), new Line.Point(0, 10));
Line line2 = new Line(new Line.Point(0, 0), new Line.Point(10, 0));
Line line3 = new Line(new Line.Point(0, 0), new Line.Point(10, 10));
System.out.println(line1);
System.out.println(line2);
System.out.println(line3);
}
}
```
Java •抽象类Vehicle,非抽象子类Car类,均实现接口ProhibitDriving • ProhibitDriving方法:boolean prohibitDriving(enum Day) • Vehicle属性:车牌号; -实现prohibitDriving方法:比如周一限行尼号0,5: • Car继承父类 Vehicle 一若干构造方法(Car属性自定义),getter、setter 写测试类carTest, 声明对象nyCar,设置车牌:声明接口变量goOut;(输入今日周几)用goOut判断是否限行
下面是题目的Java代码实现:
```java
// ProhibitDriving接口
public interface ProhibitDriving {
boolean prohibitDriving(Day day);
}
// Day枚举类
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
// 抽象类Vehicle
public abstract class Vehicle implements ProhibitDriving {
private String licensePlate;
public Vehicle(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
}
// 非抽象子类Car类
public class Car extends Vehicle {
private String brand;
private int year;
public Car(String licensePlate, String brand, int year) {
super(licensePlate);
this.brand = brand;
this.year = year;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public boolean prohibitDriving(Day day) {
if (day == Day.MONDAY && (getLicensePlate().endsWith("0") || getLicensePlate().endsWith("5"))) {
return true;
}
return false;
}
}
// 测试类carTest
import java.util.Scanner;
public class carTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入今天是星期几(用英文表示):");
String dayStr = scanner.nextLine();
Day day = Day.valueOf(dayStr.toUpperCase());
Car nyCar = new Car("浙A12345", "Toyota", 2022);
System.out.println("设置车牌号为:" + nyCar.getLicensePlate());
ProhibitDriving goOut = nyCar;
if (goOut.prohibitDriving(day)) {
System.out.println("今天是" + dayStr + ",车牌号为" + nyCar.getLicensePlate() + "的车辆限行!");
} else {
System.out.println("今天是" + dayStr + ",车牌号为" + nyCar.getLicensePlate() + "的车辆可以行驶!");
}
}
}
```
在上面的代码中,我们定义了一个ProhibitDriving接口,并且在抽象类Vehicle和非抽象子类Car类中都实现了该接口的prohibitDriving方法。在Car类中,我们重写了prohibitDriving方法,实现了根据车牌号和星期几的限行规则。最后,在测试类carTest中,我们创建了一个Car对象nyCar,并设置了车牌号。然后,我们将nyCar对象的引用赋值给接口变量goOut,并调用goOut.prohibitDriving(day)方法来判断该车辆是否限行。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)