📜 ⬆️ ⬇️

C # for AS3 developers. Part 3: get, set, sealed, using, const, readonly

image

Translation of the article From AS3 to C #, Part 3: AS3 Class Parity

Today, we’ll finish dealing with classes in C # (from the point of view of an AS3 developer) and prepare for the next article in which we can get acquainted with concepts that have no analogues in AS3. In the current article we will look at the implementation method:
- getters / setters (getter / setter)
- functions and classes that cannot be overridden / inherited (final)
- constant
- packages

')
Get / set functions

Let's start with the getters and setters. To refresh your memory, here’s how they looked in AS3:

class Person { private var _name:String; public function get name(): String { return _name; } public function set name(n:String): void { _name = n; } } 


And here is the equivalent in C #:

 class Person { private String _name; public String Name { get { return _name; } set { _name = value; } } } 


As we can see, in C #, instead of declaring 2 functions, the getter and setter are declared as a single variable. However, this variable does not end with a semicolon (;), but contains a block in the form of curly braces, inside which there are 2 parts: get {} and set {}. These parts are getter and setter functions. Inside the setter, you will have access to the new keyword value, it is the value that needs to be set and you do not need to declare it, as you had to do in AS3. If you do not take into account this difference in syntax, getters and setters work the same way in C # and AS3.

C # provides the ability to use abbreviated notation for such functions, allowing you to avoid writing extra code for simple functions:

 class Person { public String Name { get; set; } } 


Using this construct will automatically create the _name variable and the getter / setter function, as in the example above. But what if you don't need a setter function? Just remove it:

 class Person { public String Name { get; } } 


Most likely, you will need to at least give the Person class the ability to set the values ​​of this variable. For this, the setter can be declared private:

 class Person { public String Name { get; private set; } } 


Also, some C # terminology: class fields that have get and / or set functions (Name) are called “properties”. Variables that are used to store this data (_name) are called “auxiliary fields”.

Final / sealed features

And now, let's talk about final functions that cannot be overridden (AS3):

 class Person { final function print(): void { trace("I'm a Person"); } } class MyPerson extends Person { // illegal override function print(): void { trace("I'm not just a Person, I'm a MyPerson too"); } } 


C # provides identical functionality using the word sealed:

 class Person { sealed void Print() { Debug.Log("I'm a Person"); } } class MyPerson extends Person { // illegal override void Print() { Debug.Log("I'm not just a Person, I'm a MyPerson too"); } } 


Packages and namespace

Packages in AS3:

 package com.jacksondunstan.examples { class Person { } } 


In C #, packages are called namespace, that's all:

 namespace com.jacksondunstan.examples { class Person { } } 


To access the package inside the .as file, you can import the package:

 import com.jacksondunstan.examples; class Printer { function print(person:Person): void { trace("person has name: " + person.name); } } 


In C #, you use the using keyword for namespace:

 using com.jacksondunstan.examples; class Printer { void Print(Person person) { Debug.Log("person has name: " + person.name); } } 


Constants and readonly variables

And finally, let's talk about constants. Here's how a field can be created in AS3 that cannot be changed after the declaration:

 class Person { private static const MIN_TEEN_AGE:int = 13; private static const MAX_TEEN_AGE:int = 19; var age:int; function IsTeen(): Boolean { return age >= MIN_TEEN_AGE && age <= MAX_TEEN_AGE; } } 


Here's how to do this in C #:

 class Person { private const int MinTeenAge = 13; private const int MaxTeenAge = 19; int Age; bool IsTeen() { return Age >= MinTeenAge && Age <= MaxTeenAge; } } 


An important caveat: in C #, you can use only basic types (for example, int) and strings for constants. This is necessary because such a field will not actually exist, instead, wherever it is used, the values ​​of this field will be substituted. Also, this means that such fields automatically become static, and they do not need to be declared as static separately. After compilation, the Person class in C # will look like this:

 class Person { int Age; bool IsTeen() { return Age >= 13 && Age <= 19; } } 


In AS3, the keyword const was used exclusively for checking at the compilation stage and did not lead to changes / substitutions in the code. If you want to achieve similar behavior in C # or you need to use a non-base data type (for example, Person), then you need to use the readonly keyword:

 class Person { static readonly Person Newborn = new Person(0); readonly int Age; Person(int age) { Age = age; } } 


In the example above, there are 2 readonly fields:

1) Newborn is a field that was declared with the static keyword (readonly fields, unlike constants, are not necessarily static). This field will be initialized at the place where it is declared (by analogy with constants in AS3).

2) Age is a field that is readonly but not static. It will be initialized in the constructor.

Finally, a comparison of the described features of C # and AS3 code:
 //////// // C# // //////// // Collection of classes being used by this file using org.apache.coolstuff; // Collection of classes namespace com.jacksondunstan.examples { class Person { // Complex getters and setters private String _name; String Name { get { return _name; } set { _name = value; } } // Simple getters and setters int Age { get; set; } // Read-only field readonly int ID; // Inlined field const int AdultAge = 18; // Function that can't be overridden sealed void Print() { Debug.Log("Name: " + name + ", ID: " + ID); } } } 

 ///////// // AS3 // ///////// // Collection of classes being used by this file import org.apache.coolstuff; // Collection of classes package com.jacksondunstan.examples { class Person { // Complex getters and setters private var _name:String; function get name(): String { return _name; } function set name(value:String): void { _name = value; } // Simple getters and setters // {not supported. use complex instead.} // Read-only field const var ID:int; // Inlined field // {not supported. use compile-time constants via command-line instead.} // Function that can't be overridden final function print(): void { trace("Name: " + name + ", ID: " + ID); } } } 



In the next article, we finally begin to consider concepts that have no analogues in AS3.

Do not switch the channel!

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


All Articles