Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,23 @@
// Interface
public interface PurchaseOrder {
// All other functionality excluded
Double discount();
}
// One implementation of the interface for customers
public class CustomerPurchaseOrder implements PurchaseOrder {
public Double discount() {
return .05; // Flat 5% discount
}
}
// Abstract Class
public abstract class AbstractExampleClass {
protected abstract Integer abstractMethod();
}
// Complete the abstract class by implementing its abstract method
public class Class1 extends AbstractExampleClass {
public override Integer abstractMethod() { return 5; }
}

View file

@ -0,0 +1,43 @@
' FB 1.05.0 Win64
Type Animal Extends Object
Declare Abstract Sub MakeNoise()
End Type
Type Bear Extends Animal
name As String
Declare Constructor(name As String)
Declare Sub MakeNoise()
End Type
Constructor Bear(name As String)
This.name = name
End Constructor
Sub Bear.MakeNoise()
Print name; " is growling"
End Sub
Type Dog Extends Animal
name As String
Declare Constructor(name As String)
Declare Sub MakeNoise()
End Type
Constructor Dog(name As String)
This.name = name
End Constructor
Sub Dog.MakeNoise()
Print name; " is barking"
End Sub
Dim b As Animal Ptr = New Bear("Bruno")
b -> MakeNoise()
Dim d As Animal Ptr = New Dog("Rover")
d -> MakeNoise()
Delete b
Delete d
Print
Print "Press any key to quit program"
Sleep

View file

@ -0,0 +1,21 @@
define abstract_trait => trait {
require get(index::integer)
provide first() => .get(1)
provide second() => .get(2)
provide third() => .get(3)
provide fourth() => .get(4)
}
define my_type => type {
parent array
trait { import abstract_trait }
public onCreate(...) => ..onCreate(:#rest)
}
local(test) = my_type('a','b','c','d','e')
#test->first + "\n"
#test->second + "\n"
#test->third + "\n"
#test->fourth + "\n"

View file

@ -0,0 +1,5 @@
on extendAbstractClass (instance, abstractClass)
-- 'raw' instance of abstract class is made parent ("ancestor") of the
-- passed instance, i.e. the passed instance extends the abstract class
instance.setProp(#ancestor, abstractClass.rawNew())
end

View file

@ -0,0 +1,12 @@
-- instantiation of abstract class by calling its constructor fails
on new (me)
-- optional: show error message as alert
_player.alert("Error:"&&me.script&&" is an abstract class")
return VOID
end
on ring (me, n)
repeat with i = 1 to n
put me.ringtone
end repeat
end

View file

@ -0,0 +1,11 @@
property ringtone
on new (me)
extendAbstractClass(me, script("AbstractClass"))
me.ringtone = "Bell"
return me
end
on foo (me)
put "FOO"
end

View file

@ -0,0 +1,10 @@
obj = script("MyClass").new()
obj.ring(3)
-- "Bell"
-- "Bell"
-- "Bell"
-- this fails
test = script("AbstractClass").new()
put test
-- <Void>

View file

@ -0,0 +1,12 @@
on implementsInterface (instance, interfaceClass)
interfaceFuncs = interfaceClass.handlers()
funcs = instance.handlers()
repeat with f in interfaceFuncs
if funcs.getPos(f)=0 then
-- optional: show error message as alert
_player.alert("Error:"&&instance.script&&"doesn't implement interface"&&interfaceClass)
return FALSE
end if
end repeat
return TRUE
end

View file

@ -0,0 +1,3 @@
on foo
on bar
on foobar

View file

@ -0,0 +1,20 @@
on new (me)
-- if this class doesn't implement all functions of the
-- interface class, instantiation fails
if not implementsInterface(me, script("InterfaceClass")) then
return VOID
end if
return me
end
on foo (me)
put "FOO"
end
on bar (me)
put "BAR"
end
on foobar (me)
put "FOOBAR"
end

View file

@ -0,0 +1,3 @@
obj = script("MyClass").new()
put obj -- would show <Void> if interface is not fully implemented
-- <offspring "MyClass" 2 171868>

View file

@ -0,0 +1,9 @@
type
Comparable = generic x, y
(x < y) is bool
Container[T] = generic c
c.len is ordinal
items(c) is iterator
for value in c:
type(value) is T

View file

@ -0,0 +1,18 @@
# Task: abstract type
#
# Methods without implementation are annotated `abstract`.
#
# Abstract classes and interfaces can contain abstract methods and concrete (i.e. non-abstract) methods.
# Abstract classes can also have attributes.
module abstract_type
interface Inter
fun method1: Int is abstract
fun method2: Int do return 1
end
abstract class Abs
fun method1: Int is abstract
fun method2: Int do return 1
var attr: Int
end

View file

@ -0,0 +1,13 @@
Property new: Spherical(r)
Spherical method: radius @r ;
Spherical method: setRadius := r ;
Spherical method: perimeter @r 2 * Pi * ;
Spherical method: surface @r sq Pi * 4 * ;
Object Class new: Ballon(color)
Ballon is: Spherical
Ballon method: initialize(color, r) color := color self setRadius(r) ;
Object Class new: Planete(name)
Planete is: Spherical
Planete method: initialize(n, r) n := name self setRadius(r) ;

View file

@ -0,0 +1,11 @@
: testProperty
| b p |
Ballon new($red, 0.1) ->b
System.Out "Ballon radius is : " << b radius << cr
System.Out "Ballon perimeter is : " << b perimeter << cr
System.Out "Ballon surface is : " << b surface << cr
Planete new("Earth", 6371000.0) ->p
System.Out "Earth radius is : " << p radius << cr
System.Out "Earth perimeter is : " << p perimeter << cr
System.Out "Earth surface is : " << p surface << cr ;

View file

@ -0,0 +1,45 @@
Red [
Title: "Abstract Type"
Original-Author: oofoe
]
; The "shape" class is an abstract class -- it defines the "pen"
; property and "line" method, but "size" and "draw" are undefined and
; unimplemented.
shape: make object! [
pen: "X"
size: none
line: func [count][loop count [prin self/pen] prin newline]
draw: does [none]
]
; The "box" class inherits from "shape" and provides the missing
; information for drawing boxes.
box: make shape [
size: 10
draw: does [loop self/size [line self/size]]
]
; "rectangle" also inherits from "shape", but handles the
; implementation very differently.
rectangle: make shape [
size: 20x10
draw: does [loop self/size/y [line self/size/x]]
]
; Unlike some languages discussed, REBOL has absolutely no qualms
; about instantiating an "abstract" class -- that's how I created the
; derived classes of "rectangle" and "box", after all.
print "An abstract shape (nothing):"
s: make shape [] s/draw ; Nothing happens.
print [newline "A box:"]
b: make box [pen: "O" size: 5] b/draw
print [newline "A rectangle:"]
r: make rectangle [size: 32x5] r/draw

View file

@ -0,0 +1,17 @@
class A {
# must be filled in by the class which will inherit it
method abstract() { die 'Unimplemented' };
# can be overridden in the class, but that's not mandatory
method concrete() { say '# 42' };
}
class SomeClass << A {
method abstract() {
say "# made concrete in class"
}
}
var obj = SomeClass.new;
obj.abstract(); # made concrete in class
obj.concrete(); # 42