如何在 Dart 中定义接口?

在 Java 中,我可能有一个接口 IsSilly和一个或多个实现它的具体类型:

public interface IsSilly {
public void makePeopleLaugh();
}


public class Clown implements IsSilly {
@Override
public void makePeopleLaugh() {
// Here is where the magic happens
}
}


public class Comedian implements IsSilly {
@Override
public void makePeopleLaugh() {
// Here is where the magic happens
}
}

这个代码在 Dart 中的等价物是什么?

在仔细阅读了关于类的官方 医生之后,Dart 似乎没有本地 interface类型。那么,一般的达提斯人是如何完成这个接口隔离原则的呢?

70014 次浏览

In Dart there is a concept of implicit interfaces.

Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.

A class implements one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces.

So your example can be translate in Dart like this :

abstract class IsSilly {
void makePeopleLaugh();
}


class Clown implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}


class Comedian implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}

In Dart, every class defines an implicit interface. You can use an abstract class to define an interface that cannot be instantiated:

abstract class IsSilly {
void makePeopleLaugh();
}


class Clown implements IsSilly {


void makePeopleLaugh() {
// Here is where the magic happens
}


}


class Comedian implements IsSilly {


void makePeopleLaugh() {
// Here is where the magic happens
}


}

The confusion usually is because doesn't exist the word "interface" like java and another languages. Class declarations are themselves interfaces in Dart.

In Dart every class defines an implicit interface like others says.So then... the key is: Classes should use the implements keyword to be able to use an interface.

abstract class IsSilly {
void makePeopleLaugh();
}


//Abstract class
class Clown extends IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}


//Interface
class Comedian implements IsSilly {
void makePeopleLaugh() {
// Here is where the magic happens
}
}

abstract class ORMInterface {
void fromJson(Map<String, dynamic> _map);
}


abstract class ORM implements ORMInterface {


String collection = 'default';


first(Map<String, dynamic> _map2) {
print("Col $collection");
}
}


class Person extends ORM {


String collection = 'persons';


fromJson(Map<String, dynamic> _map) {
print("Here is mandatory");
}
}


This is a class or an interface depending on the situation.

abstract class A {
void sayHello() {
print("Hello");
}
void sayBye();
}

B class implements A interface, so it has to implements all methods of A.

class B implements A {
void sayHello() {
print("B say Hello");
}
void sayBye() {
print("B say Bye");
}
}

C class extends A class, so it has to implement all abstract methods of A. (Not all). C is inherited the sayHello() methods from A class.

class C extends A {
void sayBye() {
print("C say Bye");
}
}

The other answers do a good job at informing about interfaces with methods.

If you're looking for an interface with properties, you can use getters:

abstract class AppColors {
Color get primary;


Color get secondary;
}
class AppColorsImpl implements AppColors {


@override
Color get primary => Colors.red;


@override
Color get primary => Colors.blue;
}

And yes, you can combine an interface to have both properties and methods too.