How to define interfaces in Dart?

前端 未结 4 1660
耶瑟儿~
耶瑟儿~ 2021-02-01 12:52

In Java, I might have an interface IsSilly and one or more concrete types that implement it:

public interfa         


        
4条回答
  •  悲哀的现实
    2021-02-01 13:14

    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
      }
    }
    

提交回复
热议问题