-
Notifications
You must be signed in to change notification settings - Fork 89
Constructing Classes
NN--- edited this page Apr 18, 2012
·
3 revisions
- Category: Defining Types
- Description: Classes are custom types containing data (known as 'fields'), methods, and properties ( methods without parameters). Classes are reference types and are stored on the Heap. Classes must be constructed and instantiated to use. Unlike C#, no default constructor is provided, one must be defined.
- Code:
using System;
using System.Console;
using Nemerle;
using Nemerle.Utility;
[Record]
class Circle
{
public radius : double {get;}
public Area : double
{
get
{
Math.PI * radius * radius
}
}
public this() {this(1.0)}
public this(text : string)
{
when (text == null) throw System.ArgumentException("text");
mutable r;
def success = double.TryParse(text, out r);
when (!success) throw System.ArgumentException("text");
this(r);
}
public override ToString() : string
{
$"a circle with radius $radius"
}
}
def ClassesSupport()
{
def c1 = Circle();
def area1 = c1.Area;
def c2 = Circle(6.0);
def area2 = c2.Area;
def c3 = Circle("3");
def area3 = c3.Area;
WriteLine($"a $c1, area1 = $area1");
WriteLine($"a $c2, area2 = $area2");
WriteLine($"a $c3, area3 = $area3");
}
ClassesSupport()
- Execution Result:
a a circle with radius 1, area1 = 3,14159265358979
a a circle with radius 6, area2 = 113,097335529233
a a circle with radius 3, area3 = 28,2743338823081
[Copyright ©](Terms of use, legal notice)