This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImmutablePerson1(firstName, lastName) { | |
shared String firstName; | |
shared String lastName; | |
//below we override the super method Object.string which has a default implementation | |
shared actual String string => "Person(``firstName`` ``lastName``)"; | |
} | |
/** | |
this is shortcut syntax but this is not the preferred way when you have more than | |
2 class parameters (constructor parameters) | |
**/ | |
class ImmutablePerson2(shared String firstName, shared String lastName) { | |
} | |
/** | |
We can mutate firstName and lastName of instances of MutablePerson | |
**/ | |
class MutablePerson(firstName, lastName) { | |
shared variable String firstName; | |
shared variable String lastName; | |
shared actual String string => "Person(``firstName`` ``lastName``)"; | |
} | |
/** | |
We can even give default values to class parameters | |
**/ | |
class DefaultPerson(firstName, shared String lastName="Pelssers") { | |
shared String firstName; | |
shared actual String string => "Person(``firstName`` ``lastName``)"; | |
} | |
/** | |
The main method prints | |
******************* | |
Person(Robby Pelssers) | |
Robby | |
******************* | |
com.pelssers.demo.ImmutablePerson2@4400dfe4 | |
Davy | |
******************* | |
Person(Lindsey Pelssers) | |
Lindsey | |
******************* | |
Person(Valerie Pelssers) | |
Valerie | |
******************* | |
Person(Jef Pelssers) | |
Jef | |
******************* | |
**/ | |
void main() { | |
print("*******************"); | |
ImmutablePerson1 person1 = ImmutablePerson1("Robby", "Pelssers"); | |
print(person1); | |
//person.firstName = "Davy"; WON'T COMPILE | |
print(person1.firstName); | |
print("*******************"); | |
ImmutablePerson2 person2 = ImmutablePerson2("Davy", "Pelssers"); | |
print(person2); | |
print(person2.firstName); | |
print("*******************"); | |
MutablePerson person3 = MutablePerson("Lindsey", "Pelssers"); | |
print(person3); | |
print(person3.firstName); | |
print("*******************"); | |
person3.firstName = "Valerie"; | |
print(person3); | |
print(person3.firstName); | |
print("*******************"); | |
DefaultPerson person4 = DefaultPerson("Jef"); | |
print(person4); | |
print(person4.firstName); | |
print("*******************"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Below an example of how to extend other classes. | |
Mammal has a default implementation for eating. | |
Lion does not override eat() whereas | |
Squirrel provides it's own implementation. | |
**/ | |
class Mammal(shared String name="Mammal") { | |
shared default void eat() { | |
print("``name`` eating food."); | |
} | |
} | |
class Lion() extends Mammal("Lion") { | |
} | |
class Squirrel() extends Mammal("Squirrel") { | |
//here we override the default implementation | |
shared actual void eat() { | |
print("``name`` eating nuts."); | |
} | |
} | |
class Cat() extends Mammal("Cat") { | |
//shortcut notation to refine (override) a default implementation | |
eat() => print("``name`` eating mouse."); | |
} | |
/** | |
the demo prints the following to the console: | |
Some mammal eating food. | |
Lion eating food. | |
Squirrel eating nuts. | |
Cat eating mouse. | |
**/ | |
void inheritance_demo() { | |
Mammal mammal = Mammal("Some mammal"); | |
mammal.eat(); | |
Lion lion = Lion(); | |
lion.eat(); | |
Squirrel squirrel = Squirrel(); | |
squirrel.eat(); | |
Cat cat = Cat(); | |
cat.eat(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Shape(name) { | |
shared String name; | |
shared formal Float circumference(); | |
shared formal Float area(); | |
shared actual String string => "``name`` with circumference=``circumference()`` | |
and area=``area()``".normalized; | |
} | |
class Rectangle(Float width, Float length) extends Shape("Rectangle") { | |
circumference() => 2 * width + 2 * length; | |
area() => width * length; | |
} | |
/** | |
Running this program prints : | |
Rectangle with circumference=70.0 and area=300.0 | |
**/ | |
void abstractclass_demo() { | |
Rectangle rect = Rectangle(15.0, 20.0); | |
print(rect); | |
} |
No comments:
Post a Comment