java Dart 中的抽象基类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33904494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Abstract base class in Dart
提问by Tomasito665
I have been programming in Java for nearly two years but I am now more shifting to web programming and thus to Javascript, or in my case to Dart. For a project I'm working on I would like to have abstract base classes, just I would have in Java. I have been looking on the internet but I can't find anything on abstract classes at all for Dart. I only found this article from the dartlang site on mixins, that in an example uses the abstract keyword for a class. But I don't really understand the mixins principle.
我已经用 Java 编程近两年了,但我现在更多地转向 Web 编程,因此转向 Javascript,或者在我的情况下转向 Dart。对于我正在从事的项目,我想要抽象基类,就像我在 Java 中一样。我一直在互联网上寻找,但我根本找不到有关 Dart 抽象类的任何内容。我只从 dartlang 站点上的 mixins 上找到了这篇文章,在一个示例中,该文章使用了抽象关键字作为一个类。但是我真的不明白mixins的原理。
Could somebody translate this easy Java abstract base class example to Dartso that I can have a basic understanding on how it would be done in Dart? The example covers abstract base class(ofcourse, with abstract methods), polymorphism, casting objects, method overloading(in this case it is the constructor), calling super-constructorand calling overloaded own constructor.
有人可以将这个简单的 Java 抽象基类示例翻译成 Dart,以便我对它在 Dart 中的实现方式有一个基本的了解吗?该示例涵盖了抽象基类(当然,带有抽象方法)、多态性、转换对象、方法重载(在这种情况下是构造函数)、调用超级构造函数和调用重载的自己的构造函数。
// Abstract base class
abstract class Vehicle {
protected final int maxSpeed;
protected int speed;
Vehicle() {
this(0);
}
Vehicle(int maxSpeed) {
this.maxSpeed = maxSpeed;
speed = 0;
}
public int getMaxSpeed() {
return maxSpeed;
}
abstract void accelerate();
abstract void brake();
}
// Subclass of Vehicle, the abstract baseclass
class Car extends Vehicle {
public final int doors;
Car(int maxSpeed, int doors) {
super(maxSpeed);
this.doors = doors;
}
@Override
void accelerate() {
if (speed>maxSpeed) {
speed = maxSpeed;
return;
}
speed += 2;
}
@Override
void brake() {
if (speed - 2 < 0) {
speed = 0;
return;
}
this.speed -= 2;
}
}
And how would this easy implementation look like?
这个简单的实现会是什么样子?
// Polymorphism
Vehicle car = new Car(180, 4);
// Casting
int doors = ((Car)car).doors;
// Calling abstract method
car.accelerate();
回答by Pacane
I would take a look at the Language tour, there's a whole section on abstract classes
我会看一下语言之旅,有一整节是关于抽象类的
Key points:
关键点:
- Abstract classes in Dart have to be marked as
abstract
. - An abstract class can have "abstract methods", you just have to omit the body
- A concrete class can mark itself as "implementing" the abstract class' contract with the keyword
implements
. This will force you to implement all the expected behavior on the concrete class, but it won't inherit the abstract class' provided methods' implementation. - You can extend an abstract class with the keyword
extends
, and the concrete class will inherit all possible behavior of the abstract class.
- Dart 中的抽象类必须标记为
abstract
. - 抽象类可以有“抽象方法”,你只需要省略主体
- 一个具体的类可以用关键字 将自己标记为“实现”抽象类的契约
implements
。这将强制您在具体类上实现所有预期行为,但它不会继承抽象类的提供方法的实现。 - 您可以使用关键字扩展抽象类,
extends
具体类将继承抽象类的所有可能行为。
That's pretty much it!
差不多就是这样!
Also, you might want to take a look at mixins, there's a section below the one I have you in the Language tour.
此外,您可能想看看 mixin,在我在语言之旅中介绍的部分下方有一个部分。
回答by alextk
Actually it does become simpler in dart (https://dartpad.dartlang.org/37d12fa77834c1d8a172)
实际上它在 dart 中确实变得更简单(https://dartpad.dartlang.org/37d12fa77834c1d8a172)
// Abstract base class
abstract class Vehicle {
final int maxSpeed;
int speed = 0;
Vehicle([this.maxSpeed = 0]);
void accelerate();
void brake();
}
// Subclass of Vehicle, the abstract baseclass
class Car extends Vehicle {
final int doors;
Car(int maxSpeed, this.doors) : super(maxSpeed);
@override
void accelerate() {
if (speed > maxSpeed) {
speed = maxSpeed;
return;
}
speed += 2;
}
@override
void brake() {
if (speed - 2 < 0) {
speed = 0;
return;
}
this.speed -= 2;
}
}
main() {
// Polymorphism
Vehicle car = new Car(180, 4);
// Casting
int doors = (car as Car).doors;
// Calling abstract method
car.accelerate();
}