Dart代码报错:非抽象类Vehicle未实现info成员方法
Dart编译错误:非抽象类'Vehicle'未实现'info'成员
你遇到的错误提示明确指出,Vehicle类作为非抽象类,必须实现info成员。以下是四种解决方案的具体实现方式,你可以根据需求选择:
方案1:直接在Vehicle类中实现info方法
在Vehicle类里添加info方法的具体逻辑,比如打印车辆信息:
class Vehicle { String brand = ""; String model = ""; int horsePower = 0; Vehicle(this.brand, this.model, this.horsePower); // 添加info方法实现 void info() { print("品牌: $brand,型号: $model,马力: $horsePower"); } }
方案2:通过父类或混入继承info实现
如果info方法的实现来自某个父类或混入类,直接让Vehicle继承或混入即可。比如有一个包含info实现的混入类:
mixin VehicleInfo { String get brand; String get model; int get horsePower; void info() { print("品牌: $brand,型号: $model,马力: $horsePower"); } } // Vehicle类混入该类 class Vehicle with VehicleInfo { @override String brand = ""; @override String model = ""; @override int horsePower = 0; Vehicle(this.brand, this.model, this.horsePower); }
方案3:将Vehicle标记为抽象类
把Vehicle改为抽象类,这样就无需自己实现info方法,转而让子类(Car、Motorcycle)去实现:
// 改为抽象类 abstract class Vehicle { String brand = ""; String model = ""; int horsePower = 0; Vehicle(this.brand, this.model, this.horsePower); // 声明抽象方法,由子类实现 void info(); } // 子类Car实现info方法 class Car extends Vehicle { Car(String brand, String model, int horsePower) : super(brand, model, horsePower); @override void info() { print("轿车 - 品牌: $brand,型号: $model,马力: $horsePower"); } } // 子类Motorcycle实现info方法 class Motorcycle extends Vehicle { Motorcycle(String brand, String model, int horsePower) : super(brand, model, horsePower); @override void info() { print("摩托车 - 品牌: $brand,型号: $model,马力: $horsePower"); } }
方案4:实现noSuchMethod处理未定义方法
通过重写noSuchMethod来捕获info方法的调用,避免编译错误:
class Vehicle { String brand = ""; String model = ""; int horsePower = 0; Vehicle(this.brand, this.model, this.horsePower); @override dynamic noSuchMethod(Invocation invocation) { // 判断调用的是info方法 if (invocation.memberName == #info) { print("info方法未实现,当前车辆信息:品牌$brand,型号$model,马力$horsePower"); return null; } // 其他未定义方法交给父类处理 return super.noSuchMethod(invocation); } }
修改后的完整代码示例(以方案1为例)
// Higher order and Lexure Closure Function plusWith(num x) { return (num y) => x + y; } class Vehicle { String brand = ""; String model = ""; int horsePower = 0; Vehicle(this.brand, this.model, this.horsePower); // 实现info方法 void info() { print("品牌: $brand,型号: $model,马力: $horsePower"); } } class Car extends Vehicle { Car(String brand, String model, int horsePower) : super(brand, model, horsePower); } class Motorcycle extends Vehicle { Motorcycle(String brand, String model, int horsePower) : super(brand, model, horsePower); } main(List<String> args) { //Lambda Functions var func1 = (int v1) => v1 * 3; print(func1(5)); // Lexure Closure Cont; var addNum1 = plusWith(5); var addNum2 = plusWith(7); print(addNum1(2)); // Output 5 + 2 = 7 print(addNum2(3)); // Output 7 + 3 = 10 Vehicle car = Car(brand: "Golf", model: "VW", horsePower: 150); Vehicle car2 = Car(model: "A4", brand: "Audi", horsePower: 150); Vehicle moto = Motorcycle(brand: "MT-25", model: "Yamaha", horsePower: 37); Vehicle moto2 = Motorcycle(brand: "Super-Tenere", model: "Yamaha", horsePower: 127); List<Vehicle> allVehicle = [car, car2, moto, moto2]; var listOf = List<Vehicle>.of(allVehicle.whereType<Car>()); print(listOf); // 可以调用info方法测试 car.info(); }
内容的提问来源于stack exchange,提问作者Yiğit Yıldırım
相关产品推荐
相关产品推荐

