This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View 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.

View file

@ -0,0 +1,5 @@
---
category:
- Object oriented
- Type System
note: Basic language learning

View 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;

View file

@ -0,0 +1,8 @@
interface Camera {
}
interface Mobile_Phone {
}
class Camera_Phone implements Camera, Mobile_Phone {
}

View 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{})

View file

@ -0,0 +1,16 @@
class Camera
{
// ...
};
class MobilePhone
{
// ...
};
class CameraPhone:
public Camera,
public MobilePhone
{
// ...
};

View file

@ -0,0 +1,7 @@
(defprotocol Camera)
(defprotocol MobilePhone)
(deftype CameraPhone []
Camera
MobilePhone)

View file

@ -0,0 +1,3 @@
(defclass camera () ())
(defclass mobile-phone () ())
(defclass camera-phone (camera mobile-phone) ())

View 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
}

View 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;

View 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
}

View 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
}
}
}

View file

@ -0,0 +1,3 @@
? def p := makeCameraPhone(p)
> [p.takesPictures(), p.makesCalls(), p.internalMemory()]
# value: [true, true, 33619968]

View file

@ -0,0 +1,3 @@
class
CAMERA
end

View file

@ -0,0 +1,3 @@
class
MOBILE_PHONE
end

View file

@ -0,0 +1,6 @@
class
CAMERA_PHONE
inherit
CAMERA
MOBILE_PHONE
end

View 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)
}
}

View 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)
}

View 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()
}

View file

@ -0,0 +1,3 @@
class Camera a
class MobilePhone a
class (Camera a, MobilePhone a) => CameraPhone a

View 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

View file

@ -0,0 +1,3 @@
Camera = Origin mimic
MobilePhone = Origin mimic
CameraPhone = Camera mimic mimic!(MobilePhone)

View 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

View file

@ -0,0 +1,5 @@
public interface Camera{
//functions here with no definition...
//ex:
//public void takePicture();
}

View file

@ -0,0 +1,5 @@
public interface MobilePhone{
//functions here with no definition...
//ex:
//public void makeCall();
}

View file

@ -0,0 +1,3 @@
public class CameraPhone implements Camera, MobilePhone{
//functions here...
}

View file

@ -0,0 +1,4 @@
:- object(camera,
...).
...
:- end_object.

View file

@ -0,0 +1,4 @@
:- object(mobile_phone,
...).
...
:- end_object.

View file

@ -0,0 +1,5 @@
:- object(camera_phone,
specializes(camera, mobile_phone),
...).
...
:- end_object.

View 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})

View file

@ -0,0 +1,3 @@
package Camera;
#functions go here...
1;

View file

@ -0,0 +1,3 @@
package MobilePhone;
#functions go here...
1;

View file

@ -0,0 +1,6 @@
package CameraPhone;
use Camera;
use MobilePhone;
@ISA = qw( Camera MobilePhone );
#functions go here...
1;

View file

@ -0,0 +1,3 @@
package CameraPhone;
use base qw/Camera MobilePhone/;
#functions go here...

View file

@ -0,0 +1,11 @@
use MooseX::Declare;
class Camera {
# methods ...
}
class MobilePhone {
# methods ...
}
class CameraPhone extends(Camera, MobilePhone) {
# methods ...
}

View file

@ -0,0 +1,3 @@
(class +Camera)
(class +MobilePhone)

View file

@ -0,0 +1,2 @@
class Camera:
pass #functions go here...

View file

@ -0,0 +1,2 @@
class MobilePhone:
pass #functions go here...

View file

@ -0,0 +1,2 @@
class CameraPhone(Camera, MobilePhone):
pass #functions go here...

View 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
))

View 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

View file

@ -0,0 +1,7 @@
package require TclOO
oo::class create Camera
oo::class create MobilePhone
oo::class create CameraPhone {
superclass Camera MobilePhone
}