A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
5
Task/Inheritance-Multiple/0DESCRIPTION
Normal file
5
Task/Inheritance-Multiple/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Multiple inheritance allows to specify that one [[classes | class]] is a subclass of several other classes. Some languages allow multiple [[inheritance]] for arbitrary classes, others restrict it to interfaces, some don't allow it at all.
|
||||
|
||||
Write two classes (or interfaces) <tt>Camera</tt> and <tt>MobilePhone</tt>, then write a class <tt>CameraPhone</tt> which is both a <tt>Camera</tt> and a <tt>MobilePhone</tt>.
|
||||
|
||||
There is no need to implement any functions for those classes.
|
||||
5
Task/Inheritance-Multiple/1META.yaml
Normal file
5
Task/Inheritance-Multiple/1META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Object oriented
|
||||
- Type System
|
||||
note: Basic language learning
|
||||
5
Task/Inheritance-Multiple/Ada/inheritance-multiple.ada
Normal file
5
Task/Inheritance-Multiple/Ada/inheritance-multiple.ada
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package Multiple_Interfaces is
|
||||
type Camera is tagged null record;
|
||||
type Mobile_Phone is limited Interface;
|
||||
type Camera_Phone is new Camera and Mobile_Phone with null record;
|
||||
end Multiple_Interfaces;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
interface Camera {
|
||||
}
|
||||
|
||||
interface Mobile_Phone {
|
||||
}
|
||||
|
||||
class Camera_Phone implements Camera, Mobile_Phone {
|
||||
}
|
||||
12
Task/Inheritance-Multiple/BBC-BASIC/inheritance-multiple.bbc
Normal file
12
Task/Inheritance-Multiple/BBC-BASIC/inheritance-multiple.bbc
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
INSTALL @lib$+"CLASSLIB"
|
||||
|
||||
DIM Camera{TakePicture}
|
||||
PROC_class(Camera{})
|
||||
|
||||
DIM MobilePhone{MakeCall}
|
||||
PROC_class(MobilePhone{})
|
||||
|
||||
DIM CameraPhone{methods}
|
||||
PROC_inherit(CameraPhone{}, Camera{})
|
||||
PROC_inherit(CameraPhone{}, MobilePhone{})
|
||||
PROC_class(CameraPhone{})
|
||||
16
Task/Inheritance-Multiple/C++/inheritance-multiple.cpp
Normal file
16
Task/Inheritance-Multiple/C++/inheritance-multiple.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Camera
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
||||
class MobilePhone
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
||||
class CameraPhone:
|
||||
public Camera,
|
||||
public MobilePhone
|
||||
{
|
||||
// ...
|
||||
};
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defprotocol Camera)
|
||||
|
||||
(defprotocol MobilePhone)
|
||||
|
||||
(deftype CameraPhone []
|
||||
Camera
|
||||
MobilePhone)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defclass camera () ())
|
||||
(defclass mobile-phone () ())
|
||||
(defclass camera-phone (camera mobile-phone) ())
|
||||
12
Task/Inheritance-Multiple/D/inheritance-multiple.d
Normal file
12
Task/Inheritance-Multiple/D/inheritance-multiple.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
interface Camera {
|
||||
// member function prototypes and static methods
|
||||
}
|
||||
|
||||
interface MobilePhone {
|
||||
// member function prototypes and static methods
|
||||
}
|
||||
|
||||
class CameraPhone: Camera, MobilePhone {
|
||||
// member function implementations for Camera,
|
||||
// MobilePhone, and CameraPhone
|
||||
}
|
||||
12
Task/Inheritance-Multiple/Delphi/inheritance-multiple.delphi
Normal file
12
Task/Inheritance-Multiple/Delphi/inheritance-multiple.delphi
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
type
|
||||
ICamera = Interface
|
||||
// ICamera methods...
|
||||
end;
|
||||
|
||||
IMobilePhone = Interface
|
||||
// IMobilePhone methods...
|
||||
end;
|
||||
|
||||
TCameraPhone = class(TInterfacedObject, ICamera, IMobilePhone)
|
||||
// ICamera and IMobilePhone methods...
|
||||
end;
|
||||
23
Task/Inheritance-Multiple/E/inheritance-multiple-1.e
Normal file
23
Task/Inheritance-Multiple/E/inheritance-multiple-1.e
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
def minherit(self, supers) {
|
||||
def forwarder match [verb, args] {
|
||||
escape __return {
|
||||
if (verb == "__respondsTo") {
|
||||
def [verb, arity] := args
|
||||
for super ? (super.__respondsTo(verb, arity)) in supers {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} else if (verb == "__getAllegedType") {
|
||||
# XXX not a complete implementation
|
||||
return supers[0].__getAllegedType()
|
||||
} else {
|
||||
def arity := args.size()
|
||||
for super ? (super.__respondsTo(verb, arity)) in supers {
|
||||
return E.call(super, verb, args)
|
||||
}
|
||||
throw(`No parent of $self responds to $verb/$arity`)
|
||||
}
|
||||
}
|
||||
}
|
||||
return forwarder
|
||||
}
|
||||
23
Task/Inheritance-Multiple/E/inheritance-multiple-2.e
Normal file
23
Task/Inheritance-Multiple/E/inheritance-multiple-2.e
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
def makeCamera(self) {
|
||||
return def camera extends minherit(self, []) {
|
||||
to takesPictures() { return true }
|
||||
}
|
||||
}
|
||||
|
||||
def makeMobilePhone(self) {
|
||||
return def mobilePhone extends minherit(self, []) {
|
||||
to makesCalls() { return true }
|
||||
to internalMemory() { return 64*1024 }
|
||||
}
|
||||
}
|
||||
|
||||
def makeCameraPhone(self) {
|
||||
return def cameraPhone extends minherit(self, [
|
||||
makeCamera(self),
|
||||
makeMobilePhone(self),
|
||||
]) {
|
||||
to internalMemory() {
|
||||
return super.internalMemory() + 32 * 1024**2
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Task/Inheritance-Multiple/E/inheritance-multiple-3.e
Normal file
3
Task/Inheritance-Multiple/E/inheritance-multiple-3.e
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
? def p := makeCameraPhone(p)
|
||||
> [p.takesPictures(), p.makesCalls(), p.internalMemory()]
|
||||
# value: [true, true, 33619968]
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class
|
||||
CAMERA
|
||||
end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class
|
||||
MOBILE_PHONE
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
class
|
||||
CAMERA_PHONE
|
||||
inherit
|
||||
CAMERA
|
||||
MOBILE_PHONE
|
||||
end
|
||||
32
Task/Inheritance-Multiple/Fantom/inheritance-multiple.fantom
Normal file
32
Task/Inheritance-Multiple/Fantom/inheritance-multiple.fantom
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// a regular class
|
||||
class Camera
|
||||
{
|
||||
Str cameraMsg ()
|
||||
{
|
||||
"camera"
|
||||
}
|
||||
}
|
||||
|
||||
// a mixin can only contain methods
|
||||
mixin MobilePhone
|
||||
{
|
||||
Str mobileMsg ()
|
||||
{
|
||||
"mobile phone"
|
||||
}
|
||||
}
|
||||
|
||||
// class inherits from Camera, and mixes in the methods from MobilePhone
|
||||
class CameraPhone : Camera, MobilePhone
|
||||
{
|
||||
}
|
||||
|
||||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
cp := CameraPhone ()
|
||||
echo (cp.cameraMsg)
|
||||
echo (cp.mobileMsg)
|
||||
}
|
||||
}
|
||||
31
Task/Inheritance-Multiple/Go/inheritance-multiple-1.go
Normal file
31
Task/Inheritance-Multiple/Go/inheritance-multiple-1.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// Example of composition of anonymous structs
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Two ordinary structs
|
||||
type camera struct {
|
||||
optics, sensor string
|
||||
}
|
||||
|
||||
type mobilePhone struct {
|
||||
sim, firmware string
|
||||
}
|
||||
|
||||
// Fields are anonymous because only the type is listed.
|
||||
// Also called an embedded field.
|
||||
type cameraPhone struct {
|
||||
camera
|
||||
mobilePhone
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Struct literals must still reflect the nested structure
|
||||
htc := cameraPhone{camera{optics: "zoom"}, mobilePhone{firmware: "3.14"}}
|
||||
|
||||
// But fields of anonymous structs can be referenced without qualification.
|
||||
// This provides some effect of the two parent structs being merged, as
|
||||
// with multiple inheritance in some other programming languages.
|
||||
htc.sim = "XYZ"
|
||||
fmt.Println(htc)
|
||||
}
|
||||
47
Task/Inheritance-Multiple/Go/inheritance-multiple-2.go
Normal file
47
Task/Inheritance-Multiple/Go/inheritance-multiple-2.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// Example of composition of interfaces.
|
||||
// Types implement interfaces simply by implementing functions.
|
||||
// The type does not explicitly declare the interfaces it implements.
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Two interfaces.
|
||||
type camera interface {
|
||||
photo()
|
||||
}
|
||||
|
||||
type mobilePhone interface {
|
||||
call()
|
||||
}
|
||||
|
||||
// Compose interfaces. cameraPhone interface now contains whatever
|
||||
// methods are in camera and mobilePhone.
|
||||
type cameraPhone interface {
|
||||
camera
|
||||
mobilePhone
|
||||
}
|
||||
|
||||
// User defined type.
|
||||
type htc int
|
||||
|
||||
// Once the htc type has this method defined on it, it automatically satisfies
|
||||
// the camera interface.
|
||||
func (htc) photo() {
|
||||
fmt.Println("snap")
|
||||
}
|
||||
|
||||
// And then with this additional method defined, it now satisfies both the
|
||||
// mobilePhone and cameraPhone interfaces.
|
||||
func (htc) call() {
|
||||
fmt.Println("omg!")
|
||||
}
|
||||
|
||||
func main() {
|
||||
// type of i is the composed interface. The assignment only compiles
|
||||
// because static type htc satisfies the interface cameraPhone.
|
||||
var i cameraPhone = new(htc)
|
||||
// interface functions can be called without reference to the
|
||||
// underlying type.
|
||||
i.photo()
|
||||
i.call()
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Camera a
|
||||
class MobilePhone a
|
||||
class (Camera a, MobilePhone a) => CameraPhone a
|
||||
14
Task/Inheritance-Multiple/Haskell/inheritance-multiple-2.hs
Normal file
14
Task/Inheritance-Multiple/Haskell/inheritance-multiple-2.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Camera (instanceVars)
|
||||
# methods...
|
||||
# initializer...
|
||||
end
|
||||
|
||||
class Phone (instanceVars)
|
||||
# methods...
|
||||
# initializer...
|
||||
end
|
||||
|
||||
class CameraPhone : Camera, Phone (instanceVars)
|
||||
# methods...
|
||||
# initialiser...
|
||||
end
|
||||
3
Task/Inheritance-Multiple/Ioke/inheritance-multiple.ioke
Normal file
3
Task/Inheritance-Multiple/Ioke/inheritance-multiple.ioke
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Camera = Origin mimic
|
||||
MobilePhone = Origin mimic
|
||||
CameraPhone = Camera mimic mimic!(MobilePhone)
|
||||
32
Task/Inheritance-Multiple/J/inheritance-multiple.j
Normal file
32
Task/Inheritance-Multiple/J/inheritance-multiple.j
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
coclass 'Camera'
|
||||
|
||||
create=: verb define
|
||||
NB. creation-specifics for a camera go here
|
||||
)
|
||||
|
||||
destroy=: codestroy
|
||||
|
||||
NB. additional camera methods go here
|
||||
|
||||
coclass 'MobilePhone'
|
||||
|
||||
create=: verb define
|
||||
NB. creation-specifics for a mobile phone go here
|
||||
)
|
||||
|
||||
destroy=: codestroy
|
||||
|
||||
NB. additional phone methods go here
|
||||
|
||||
coclass 'CameraPhone'
|
||||
coinsert 'Camera MobilePhone'
|
||||
|
||||
create=: verb define
|
||||
create_Camera_ f. y
|
||||
create_MobilePhone_ f. y
|
||||
NB. creation details specific to a camera phone go here
|
||||
)
|
||||
|
||||
destroy=: codestroy
|
||||
|
||||
NB. additional camera-phone methods go here
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
public interface Camera{
|
||||
//functions here with no definition...
|
||||
//ex:
|
||||
//public void takePicture();
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
public interface MobilePhone{
|
||||
//functions here with no definition...
|
||||
//ex:
|
||||
//public void makeCall();
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public class CameraPhone implements Camera, MobilePhone{
|
||||
//functions here...
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
:- object(camera,
|
||||
...).
|
||||
...
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
:- object(mobile_phone,
|
||||
...).
|
||||
...
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
:- object(camera_phone,
|
||||
specializes(camera, mobile_phone),
|
||||
...).
|
||||
...
|
||||
:- end_object.
|
||||
13
Task/Inheritance-Multiple/Lua/inheritance-multiple.lua
Normal file
13
Task/Inheritance-Multiple/Lua/inheritance-multiple.lua
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function setmetatables(t,mts) --takes a table and a list of metatables
|
||||
return setmetatable(t,{__index = function(self, k)
|
||||
--collisions are resolved in this implementation by simply taking the first one that comes along.
|
||||
for i, mt in ipairs(mts) do
|
||||
local member = mt[k]
|
||||
if member then return member end
|
||||
end
|
||||
end})
|
||||
end
|
||||
|
||||
camera = {}
|
||||
mobilephone = {}
|
||||
cameraphone = setemetatables({},{camera,mobilephone})
|
||||
3
Task/Inheritance-Multiple/Perl/inheritance-multiple-1.pl
Normal file
3
Task/Inheritance-Multiple/Perl/inheritance-multiple-1.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package Camera;
|
||||
#functions go here...
|
||||
1;
|
||||
3
Task/Inheritance-Multiple/Perl/inheritance-multiple-2.pl
Normal file
3
Task/Inheritance-Multiple/Perl/inheritance-multiple-2.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package MobilePhone;
|
||||
#functions go here...
|
||||
1;
|
||||
6
Task/Inheritance-Multiple/Perl/inheritance-multiple-3.pl
Normal file
6
Task/Inheritance-Multiple/Perl/inheritance-multiple-3.pl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package CameraPhone;
|
||||
use Camera;
|
||||
use MobilePhone;
|
||||
@ISA = qw( Camera MobilePhone );
|
||||
#functions go here...
|
||||
1;
|
||||
3
Task/Inheritance-Multiple/Perl/inheritance-multiple-4.pl
Normal file
3
Task/Inheritance-Multiple/Perl/inheritance-multiple-4.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
package CameraPhone;
|
||||
use base qw/Camera MobilePhone/;
|
||||
#functions go here...
|
||||
11
Task/Inheritance-Multiple/Perl/inheritance-multiple-5.pl
Normal file
11
Task/Inheritance-Multiple/Perl/inheritance-multiple-5.pl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use MooseX::Declare;
|
||||
|
||||
class Camera {
|
||||
# methods ...
|
||||
}
|
||||
class MobilePhone {
|
||||
# methods ...
|
||||
}
|
||||
class CameraPhone extends(Camera, MobilePhone) {
|
||||
# methods ...
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(class +Camera)
|
||||
|
||||
(class +MobilePhone)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
class Camera:
|
||||
pass #functions go here...
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
class MobilePhone:
|
||||
pass #functions go here...
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
class CameraPhone(Camera, MobilePhone):
|
||||
pass #functions go here...
|
||||
10
Task/Inheritance-Multiple/Racket/inheritance-multiple.rkt
Normal file
10
Task/Inheritance-Multiple/Racket/inheritance-multiple.rkt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#lang racket
|
||||
|
||||
(define camera<%> (interface ()))
|
||||
(define mobile-phone<%> (interface ()))
|
||||
|
||||
(define camera-phone%
|
||||
(class* object% (camera<%> mobile-phone<%>)
|
||||
(super-new)
|
||||
;; implement methods here
|
||||
))
|
||||
10
Task/Inheritance-Multiple/Ruby/inheritance-multiple.rb
Normal file
10
Task/Inheritance-Multiple/Ruby/inheritance-multiple.rb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module Camera
|
||||
# define methods here
|
||||
end
|
||||
class MobilePhone
|
||||
# define methods here
|
||||
end
|
||||
class CameraPhone < MobilePhone
|
||||
include Camera
|
||||
# define methods here
|
||||
end
|
||||
7
Task/Inheritance-Multiple/Tcl/inheritance-multiple.tcl
Normal file
7
Task/Inheritance-Multiple/Tcl/inheritance-multiple.tcl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package require TclOO
|
||||
|
||||
oo::class create Camera
|
||||
oo::class create MobilePhone
|
||||
oo::class create CameraPhone {
|
||||
superclass Camera MobilePhone
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue