Data update
This commit is contained in:
parent
4d5544505c
commit
4924dd0264
3073 changed files with 55820 additions and 4408 deletions
60
Task/Abstract-type/Free-Pascal-Lazarus/abstract-type-1.pas
Normal file
60
Task/Abstract-type/Free-Pascal-Lazarus/abstract-type-1.pas
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
program abstracts;
|
||||
// this code also compiles in delphi
|
||||
{$ifdef fpc}{$mode objfpc}{$endif}
|
||||
type
|
||||
// Pure Abstract Class. In cs theory also known as an interface,
|
||||
// because it has no implementation
|
||||
TAbstractClass = class abstract
|
||||
protected
|
||||
function noise:string;virtual;abstract;
|
||||
end;
|
||||
// classic inheritance
|
||||
Tdog = class(TAbstractClass)
|
||||
public
|
||||
function noise:string;override;
|
||||
end;
|
||||
|
||||
Tcat = class(TAbstractClass)
|
||||
public
|
||||
function noise:string;override;
|
||||
end;
|
||||
|
||||
// unrelated class that matches the interface of TAbstractClass, the VMT.
|
||||
Tbird = class
|
||||
strict private
|
||||
FField:string;
|
||||
function noise:string;virtual;
|
||||
property field:string read FField write FField;
|
||||
end;
|
||||
|
||||
function Tdog.Noise:string;
|
||||
begin
|
||||
Result := 'Woof';
|
||||
end;
|
||||
|
||||
function Tcat.Noise:string;
|
||||
begin
|
||||
Result := 'Miauw';
|
||||
end;
|
||||
|
||||
function TBird.Noise:string;
|
||||
begin
|
||||
Result := 'Tjirpp';
|
||||
end;
|
||||
|
||||
var
|
||||
cat, dog: TAbstractClass;
|
||||
bird:Tbird;
|
||||
begin
|
||||
cat := Tcat.Create;
|
||||
dog := Tdog.Create;
|
||||
bird := TBird.Create;
|
||||
writeln(cat.noise,dog.noise);
|
||||
// even this works, because the layout is the same
|
||||
// This is similar to C++, where pure abstract classes are interfaces.
|
||||
writeln(TAbstractClass(bird).noise);
|
||||
bird.free;
|
||||
dog.free;
|
||||
cat.free;
|
||||
readln;
|
||||
end.
|
||||
32
Task/Abstract-type/Free-Pascal-Lazarus/abstract-type-2.pas
Normal file
32
Task/Abstract-type/Free-Pascal-Lazarus/abstract-type-2.pas
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
program justasabstract;
|
||||
{$ifdef fpc}{$mode objfpc}{$endif}
|
||||
type
|
||||
IAnimalInterface = interface
|
||||
['{BD40E312-36AD-4F8B-A3EF-727F2474E494}']
|
||||
function noise:string;
|
||||
function move:string;
|
||||
end;
|
||||
|
||||
Tbird = class(TInterfacedObject,IAnimalInterface)
|
||||
strict private
|
||||
function noise:string;virtual;
|
||||
function move:string;virtual;
|
||||
end;
|
||||
|
||||
function TBird.Noise:string;
|
||||
begin
|
||||
Result := 'Tjirpp';
|
||||
end;
|
||||
|
||||
function TBird.move:string;
|
||||
begin
|
||||
Result := 'I fly';
|
||||
end;
|
||||
|
||||
var
|
||||
bird:IAnimalInterface; // as interface, this unhides the strict private methods too.
|
||||
begin
|
||||
bird := TBird.Create;
|
||||
writeln(bird.noise);
|
||||
writeln(bird.move);
|
||||
end.
|
||||
44
Task/Abstract-type/Haxe/abstract-type-1.haxe
Normal file
44
Task/Abstract-type/Haxe/abstract-type-1.haxe
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
interface Vocal {
|
||||
public function speak():String;
|
||||
}
|
||||
|
||||
interface Gravitational {
|
||||
public function getWeight():Int;
|
||||
}
|
||||
|
||||
abstract class Dog implements Vocal implements Gravitational {
|
||||
public function speak():String {
|
||||
return "Woof";
|
||||
}
|
||||
|
||||
public function new() {}
|
||||
}
|
||||
|
||||
class Chihuahua extends Dog {
|
||||
public function getWeight():Int {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
class GreatDane extends Dog {
|
||||
public function getWeight():Int {
|
||||
return 150;
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
static public function main():Void {
|
||||
var dogs:Array<Dog> = [];
|
||||
|
||||
var david = new Chihuahua();
|
||||
var goliath = new GreatDane();
|
||||
|
||||
dogs.push(david);
|
||||
dogs.push(goliath);
|
||||
|
||||
for (dog in dogs) {
|
||||
trace(dog.speak());
|
||||
trace(dog.getWeight());
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Task/Abstract-type/Pluto/abstract-type.pluto
Normal file
34
Task/Abstract-type/Pluto/abstract-type.pluto
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
class Beast
|
||||
function __construct()
|
||||
error("Instantiation not allowed as class is abstract.")
|
||||
end
|
||||
|
||||
function kind() end
|
||||
function name() end
|
||||
function cry() end
|
||||
|
||||
function print()
|
||||
print($'{self:name()}, who\'s a {self:kind()}, cries: "{self:cry()}".')
|
||||
end
|
||||
end
|
||||
|
||||
class Dog extends Beast
|
||||
function __construct(private kind, private name) end
|
||||
|
||||
function kind() return self.kind end
|
||||
function name() return self.name end
|
||||
function cry() return "Woof" end
|
||||
end
|
||||
|
||||
class Cat extends Beast
|
||||
function __construct(private kind, private name) end
|
||||
|
||||
function kind() return self.kind end
|
||||
function name() return self.name end
|
||||
function cry() return "Meow" end
|
||||
end
|
||||
|
||||
local d = new Dog("labrador", "Max")
|
||||
local c = new Cat("siamese", "Sammy")
|
||||
d:print()
|
||||
c:print()
|
||||
|
|
@ -40,5 +40,5 @@ s: make shape [] s/draw ; Nothing happens.
|
|||
print "A box:"
|
||||
b: make box [pen: "O" size: 5] b/draw
|
||||
|
||||
print [crlf "A rectangle:"]
|
||||
print "^/A rectangle:"
|
||||
r: make rectangle [size: 32x5] r/draw
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue