Wiki

Changes between Initial Version and Version 1 of Interface

Show
Ignore:
Timestamp:
05/14/10 06:58:02 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Interface

    v1 v1  
     1= Interface Definition = 
     2 
     3An interface defines a feature set or related functionality as a small group of  
     4properties, methods, and events. 
     5 
     6A class or struct '''implementing''' an interface provides a concrete implementation for  
     7all of the members defined by that interface.   
     8 
     9An Interfaces members are automatically public. 
     10 
     11The interface itself provides no functionality (implementation) that a class or struct  
     12can inherit the way that base class functionality can be inherited.  
     13(Instead see [wiki:Mixins mixins])[[BR]] 
     14However, if a base class implements an interface, any derived class inherits that 
     15implementation (and thus continues to implement that interface). 
     16 
     17    * An interface is like an abstract base class:[[BR]]  
     18      any non-abstract type inheriting the interface must implement all its members. 
     19    * An interface cannot be instantiated directly. 
     20    * Interfaces contain no implementation of methods or properties. 
     21    * An Interface cannot contain fields.   
     22    * Classes and structs can inherit from more than one interface. 
     23    * An interface can itself inherit from multiple interfaces. 
     24 
     25 
     26In Cobra, Interfaces are declared using the '''interface''' keyword followed by the  
     27interface name. [[BR]] 
     28The interface  may have any of the usual [wiki:AccessModifiers access modifiers] and 
     29interface hierarchies (!SuperInterfaces) can be specified using '''inherits''' 
     30 
     31Interfaces can have attributes and may be declared as generic with constraints. 
     32 
     33Use of a docstring on Interfaces is encouraged. 
     34 
     35 
     36=== Interface Grammar === 
     37{{{ 
     38# The grammar elements below are ordered for simplicity but the compiler will accept  
     39# those elements in any reasonable order. 
     40 
     41interface <InterfaceName>   
     42    [is <AccessModifiers>] 
     43    [has <Attributes>] 
     44    [where <GenericParam> must be <GenericConstraints>] 
     45    [inherits <BaseInterfaceName> [, <BaseInterfaceName>]... ] 
     46    [<DocString>] 
     47 
     48    [  
     49        [<Properties>] 
     50        [<Methods> 
     51        [<Events>] 
     52}}} 
     53 
     54== Example == 
     55 
     56This example declares an interface, IDimensions, and a class, Box,[[BR]] 
     57which explicitly implements the interface members getLength and getWidth. 
     58 
     59The members are accessed through the interface instance dimensions. 
     60{{{ 
     61interface IDimensions 
     62    """ Interface for Dimensions of a 2D object""" 
     63    def getLength as float 
     64    def getWidth as float 
     65 
     66class Box implements IDimensions 
     67    var lengthInches as float 
     68    var widthInches as float 
     69 
     70    cue init length as float, width as float) 
     71        .lengthInches = length 
     72        .widthInches = width 
     73 
     74    # Explicit interface member implementation:  
     75    def getLength as float 
     76        return .lengthInches 
     77         
     78    def getWidth as float 
     79        return .widthInches 
     80 
     81    def main is shared 
     82        #// Declare a class instance box1: 
     83        box1 = Box(30.0f, 20.0f) 
     84 
     85        #// Declare an interface instance dimensions: 
     86        dimensions = box1 to IDimensions 
     87 
     88        #// Print out the dimensions of the box by calling the methods  
     89        #// from an instance of the interface: 
     90        print 'Length: [dimensions.getLength]' 
     91        print 'Width:  [dimensions.getWidth]' 
     92    } 
     93} 
     94/# Output: 
     95    Length: 30 
     96    Width: 20 
     97#/ 
     98 
     99}}} 
     100 
     101 
     102 
     103Using generics 
     104{{{ 
     105interface IEquatable<of T> 
     106    """Interface for an object that can tell if it is equal to other objects of the same type""" 
     107    def equals(obj as T) as bool 
     108 
     109 
     110class Car implements  IEquatable<of Car> 
     111    pro make as String 
     112    pro model as String 
     113    pro year as String 
     114 
     115    #// Implementation of IEquatable<of T> interface 
     116    def equals(car as Car) as bool 
     117        if .make == car.make and .model == car.model and .year == car.year 
     118            return true 
     119        return false 
     120}}} 
     121 
     122 
     123== Notes ==