1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
«k:class» «t:Spacecraft» {
«t:String» «v:name»;
«t:DateTime» «v:launchDate»;
«m:// »«x:Constructor, with syntactic sugar for assignment to members.
» «t:Spacecraft»(«k:this».«v:name», «k:this».«v:launchDate») {
«m:// »«x:Initialization code goes here.
» }
«m:// »«x:Named constructor that forwards to the default one.
» «t:Spacecraft».unlaunched(«t:String» «v:name») : «k:this»(name, «c:null»);
«t:int» «b:get» «v:launchYear» => launchDate?.year; «m:// »«x:read-only non-final property
»
«m:// »«x:Method.
» «t:void» «f:describe»() {
print(«s:'Spacecraft: »«v:$name»«s:'»);
«k:if» (launchDate != «c:null») {
«t:int» «v:years» = «t:DateTime».now().difference(launchDate).inDays ~/ «c:365»;
print(«s:'Launched: »«v:$launchYear»«s: (»«v:$years»«s: years ago)'»);
} «k:else» {
print(«s:'Unlaunched'»);
}
}
}
«k:var» «v:voyager» = «t:Spacecraft»(«s:'Voyager I'», «t:DateTime»(«c:1977», «c:9», «c:5»));
voyager.describe();
«k:var» «v:voyager3» = «t:Spacecraft».unlaunched(«s:'Voyager III'»);
voyager3.describe();
|