C # for AS3 developers. Part 2: Extending Classes and Implementing Interfaces

Original author: Jackson Dunstan
  • Transfer
  • Tutorial
image

Translation of an article From AS3 to C #, Part 2: Extending Classes and Implementing Interfaces

This article continues the series “ C # for AS3 Developers, ” and today we will look at how classes are structured in C # from the perspective of an AS3 developer (inheriting classes, implementing interfaces, and inheriting interfaces).

Let's start with class inheritance. Here's what it looks like in AS3:

classShape{
}
classCircleextendsShape{
}


Here's what it looks like in C #:

classShape
{
}
classCircle : Shape
{
}


As you may have noticed, instead of the extends keyword, the following is used: (colon).

Things are similar with the implementation of interfaces. How it looks in AS3:

interfaceIHasArea{
    functionGetArea(): int;
}
interfaceIRound{
    functionGetSmoothness(): int;
}
classShape{
}
classCircleextendsShapeimplementsIHasArea, IRound{
    functionGetArea(): int
    {
        return33; // TODO рассчитать
    }
    functionGetSmoothness(): int
    {
        return44; // TODO рассчитать
    }
}


Same code structure, but in C #:

interfaceIHasArea
{
    intGetArea();
}
interfaceIRound
{
    intGetSmoothness();
}
classShape
{
}
classCircle : Shape, IHasArea, IRound
{
    intGetArea()
    {
        return33; // TODO рассчитать
    }
    intGetSmoothness()
    {
        return44; // TODO рассчитать
    }
}


In C #, a colon is used instead of the implements keyword, in the same way as with extends. The description of interfaces in C # is similar to the description of these in AS3.

An example of extending interfaces in AS3:

interfaceISuper{
}
interfaceISubextendsISuper{
}


Most likely, you already guess how this code looks in C # ...

interfaceISuper
{
}
interfaceISub : ISuper
{
}


Let's see how the relationships between parent and child classes / interfaces in C # are described. In AS3, you can use the this and super keywords in any non-static functions to refer to an instance of the current class (this) or to an instance of the parent class (super). For example:

classPolygon{
    var numVertices:uint;
    functionprintDescription(): String
    {
        return"Polygon (numVertices=" + numVertices + ")";
    }
}
classTriangleextendsPolygon{
    var color:String;
    var polygonDescriptionAtConstructionTime:String;
    functionTriangle(color:String){
        this.color = color;
        polygonDescriptionAtConstructionTime = super.printDescription();
    }
    functionprintDescription(): String
    {
        return"Triangle (color=" + color + ")";
    }
}


In this example, the super keyword refers exclusively to the variables and functions of the Polygon class. This method of accessing functions is extremely rare, but sometimes it can help to avoid ambiguities in the code regarding which of the printDescription functions will be called. If the word super were not used in this example, the printDescription function of the Triangle class would be called, because a search in the current class occurs before the parent class.

This keyword refers exclusively to variables and functions of the Triangle class. In the example, this is used so that the compiler understands that you need to refer to the color variable of the Triangle class, and not to the variable of the same name passed to the constructor.

And now the C # version:

classPolygon
{
    uint NumVertices;
    String PrintDescription()
    {
        return"Polygon (numVertices=" + numVertices + ")";
    }
}
classTriangle : Polygon
{
    String Color;
    String PolygonDescriptionAtConstructionTime;
    Triangle(String color)
    {
        this.Color = color;
        PolygonDescriptionAtConstructionTime = base.printDescription();
    }
    String printDescription()
    {
        return"Triangle (color=" + color + ")";
    }
}


These two examples are very similar, except that in C #, instead of the super keyword, the base keyword is used. In C #, the base performs the same functions as super in AS3.

Another example of using super in AS3 (calling the parent constructor):

classPolygon{
    var numVertices:uint;
    functionPolygon(numVertices:uint){
        this.numVertices = numVertices;
    }
}
classTriangleextendsPolygon{
    var color:String;
    functionTriangle(numVertices:uint, color:String){
        super(numVertices);
        this.color = color;
    }
}


C # version:

classPolygon
{
    uint NumVertices;
    Polygon(uint numVertices)
    {
        NumVertices = numVertices;
    }
}
classTriangle : Polygon
{
    String Color;
    Triangle(uint numVertices, String color)
        : base(numVertices)
    {
    }
}


In addition to replacing super with base again, you may notice that the call occurs before the code runs in the constructor (before the curly braces {}). Also, a colon (:) is preceded by a call to the constructor of the parent class. Please note that at the end of the call line, a semicolon (;) is not put.

Let's see how to make the class impossible to extend in AS3:

finalclassFinalClass{
}
// Этот класс не будет компилироватьсяclassDerviedClassextendsFinalClass{
}


Similar functionality in C #:

sealedclassFinalClass
{
}
// Этот класс не будет компилироватьсяclassDerviedClass : FinalClass
{
}


In this case, the differences between C # and AS3 are only in the name of the keywords. In C #, a class that cannot be extended is indicated by the keyword sealed, while in AS3 final is used for this, but the effect is exactly the same.

In conclusion, a comparison of the described features of C # and AS3 code:
////////// C# //////////// InterfaceinterfaceIShape
{
    String GetDescription();
}
// Sub-interface (one that extends another)interfaceIPolygon : IShape
{
    uintGetNumEdges();
}
// ClassclassShape
{
    String Name;
    Shape(String name)
    {
        Name = name;
    }
}
// Derived class (one that extends another)classCircle : Shape
{
    int Radius;
    Circle(String name, int radius)
        : base(name)
    {
        Radius = radius;
    }
}
// Derived class that also implements an interfaceclassTriangle : Shape, IPolygon
{
    Triangle()
        : base("Triangle")
    {
    }
    uintGetNumEdges()
    {
        return3;
    }
    String GetDescription()
    {
        return"A three-sided polygon";
    }
}
// Class that can't be extendedsealedclassNoDerivatives
{
}

/////////// AS3 ///////////// InterfaceinterfaceIShape{
    functiongetDescription(): String;
}
// Sub-interface (one that extends another)interfaceIPolygonextendsIShape{
    functiongetNumEdges(): uint;
}
// ClassclassShape{
    var name:String;
    functionShape(name:String){
        this.name = name;
    }
}
// Derived class (one that extends another)classCircleextendsShape{
    var Radius:int;
    functionCircle(name:String, radius:int){
        super(name);
        Radius = radius;
    }
}
// Derived class that also implements an interfaceclassTriangleextendsShapeimplementsIPolygon{
    functionTriangle(){
        super("Triangle");
    }
    functionGetNumEdges(): uint
    {
        return3;
    }
    functionGetDescription(): String
    {
        return"A three-sided polygon";
    }
}
// Class that can't be extendedfinalclassNoDerivatives{
}


On this we are rounded off today. In the next article, we will talk about getters and setters (getter / setter) and finish the description of the basics of classes. After that, we can move on to C # nuances, which have no analogues in AS3.

Also popular now: