📜 ⬆️ ⬇️

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

image

Translation of the 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 designed in C # from the point of view of an AS3 developer (class inheritance, interface implementation and interface inheritance).

Let's start with class inheritance. Here is how it looks in AS3:
')
class Shape { } class Circle extends Shape { } 


And here is what it looks like in C #:

 class Shape { } class Circle : Shape { } 


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

Similarly, the situation with the implementation of interfaces. What it looks like in AS3:

 interface IHasArea { function GetArea(): int; } interface IRound { function GetSmoothness(): int; } class Shape { } class Circle extends Shape implements IHasArea, IRound { function GetArea(): int { return 33; // TODO  } function GetSmoothness(): int { return 44; // TODO  } } 


The same code structure, but in C #:

 interface IHasArea { int GetArea(); } interface IRound { int GetSmoothness(); } class Shape { } class Circle : Shape, IHasArea, IRound { int GetArea() { return 33; // TODO  } int GetSmoothness() { return 44; // TODO  } } 


In C #, the 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 the expansion of interfaces in AS3:

 interface ISuper { } interface ISub extends ISuper { } 


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

 interface ISuper { } interface ISub : ISuper { } 


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

 class Polygon { var numVertices:uint; function printDescription(): String { return "Polygon (numVertices=" + numVertices + ")"; } } class Triangle extends Polygon { var color:String; var polygonDescriptionAtConstructionTime:String; function Triangle(color:String) { this.color = color; polygonDescriptionAtConstructionTime = super.printDescription(); } function printDescription(): String { return "Triangle (color=" + color + ")"; } } 


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

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

And now the C # version:

 class Polygon { uint NumVertices; String PrintDescription() { return "Polygon (numVertices=" + numVertices + ")"; } } class Triangle : 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 #, the keyword base is used instead of the super keyword. In C #, base performs the same functions as super in AS3.

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

 class Polygon { var numVertices:uint; function Polygon(numVertices:uint) { this.numVertices = numVertices; } } class Triangle extends Polygon { var color:String; function Triangle(numVertices:uint, color:String) { super(numVertices); this.color = color; } } 


C # version:

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


Besides the fact that we replaced super with base again, you can see that the call takes place before executing the code in the constructor (before the curly braces {}). Also, the colon (:) is preceded by a call to the parent class constructor. Notice that there is no semicolon (;) at the end of the call line.

Let's take a look at how to make AS3 so that the class cannot be extended:

 final class FinalClass { } //      class DerviedClass extends FinalClass { } 


Similar functionality in C #:

 sealed class FinalClass { } //      class DerviedClass : FinalClass { } 


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

Finally, a comparison of the described features of C # and AS3 code:
 //////// // C# // //////// // Interface interface IShape { String GetDescription(); } // Sub-interface (one that extends another) interface IPolygon : IShape { uint GetNumEdges(); } // Class class Shape { String Name; Shape(String name) { Name = name; } } // Derived class (one that extends another) class Circle : Shape { int Radius; Circle(String name, int radius) : base(name) { Radius = radius; } } // Derived class that also implements an interface class Triangle : Shape, IPolygon { Triangle() : base("Triangle") { } uint GetNumEdges() { return 3; } String GetDescription() { return "A three-sided polygon"; } } // Class that can't be extended sealed class NoDerivatives { } 

 ///////// // AS3 // ///////// // Interface interface IShape { function getDescription(): String; } // Sub-interface (one that extends another) interface IPolygon extends IShape { function getNumEdges(): uint; } // Class class Shape { var name:String; function Shape(name:String) { this.name = name; } } // Derived class (one that extends another) class Circle extends Shape { var Radius:int; function Circle(name:String, radius:int) { super(name); Radius = radius; } } // Derived class that also implements an interface class Triangle extends Shape implements IPolygon { function Triangle() { super("Triangle"); } function GetNumEdges(): uint { return 3; } function GetDescription(): String { return "A three-sided polygon"; } } // Class that can't be extended final class NoDerivatives { } 


On this we are rounding 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 go to the nuances of C #, which have no analogues in AS3.

Source: https://habr.com/ru/post/246265/


All Articles