Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
6
Task/Inheritance-Multiple/00-META.yaml
Normal file
6
Task/Inheritance-Multiple/00-META.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
category:
|
||||
- Object oriented
|
||||
- Type System
|
||||
from: http://rosettacode.org/wiki/Inheritance/Multiple
|
||||
note: Basic language learning
|
||||
11
Task/Inheritance-Multiple/00-TASK.txt
Normal file
11
Task/Inheritance-Multiple/00-TASK.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
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.
|
||||
|
||||
|
||||
;Task:
|
||||
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.
|
||||
<br><br>
|
||||
|
||||
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 {
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
// ...
|
||||
};
|
||||
11
Task/Inheritance-Multiple/C-sharp/inheritance-multiple.cs
Normal file
11
Task/Inheritance-Multiple/C-sharp/inheritance-multiple.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
interface ICamera {
|
||||
// ...
|
||||
}
|
||||
|
||||
class MobilePhone {
|
||||
// ...
|
||||
}
|
||||
|
||||
class CameraPhone: ICamera, MobilePhone {
|
||||
// ...
|
||||
}
|
||||
16
Task/Inheritance-Multiple/C/inheritance-multiple.c
Normal file
16
Task/Inheritance-Multiple/C/inheritance-multiple.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
typedef struct{
|
||||
double focalLength;
|
||||
double resolution;
|
||||
double memory;
|
||||
}Camera;
|
||||
|
||||
typedef struct{
|
||||
double balance;
|
||||
double batteryLevel;
|
||||
char** contacts;
|
||||
}Phone;
|
||||
|
||||
typedef struct{
|
||||
Camera cameraSample;
|
||||
Phone phoneSample;
|
||||
}CameraPhone;
|
||||
17
Task/Inheritance-Multiple/COBOL/inheritance-multiple.cobol
Normal file
17
Task/Inheritance-Multiple/COBOL/inheritance-multiple.cobol
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
CLASS-ID. Camera.
|
||||
*> ...
|
||||
END CLASS Camera.
|
||||
|
||||
CLASS-ID. Mobile-Phone.
|
||||
*> ...
|
||||
END CLASS Mobile-Phone.
|
||||
|
||||
CLASS-ID. Camera-Phone INHERITS Camera, Mobile-Phone.
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
CLASS Camera
|
||||
CLASS Mobile-Phone.
|
||||
|
||||
*> ...
|
||||
END CLASS Camera-Phone.
|
||||
|
|
@ -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-1.d
Normal file
12
Task/Inheritance-Multiple/D/inheritance-multiple-1.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/D/inheritance-multiple-2.d
Normal file
12
Task/Inheritance-Multiple/D/inheritance-multiple-2.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
interface Camera {
|
||||
// A virtual function.
|
||||
Image takePhoto();
|
||||
|
||||
// A non-virtual function.
|
||||
final Image[] takeSeveralPhotos(int count) {
|
||||
auto result = new Image[count];
|
||||
foreach (ref img; result) {
|
||||
img = takePhoto();
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Task/Inheritance-Multiple/D/inheritance-multiple-3.d
Normal file
38
Task/Inheritance-Multiple/D/inheritance-multiple-3.d
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
class A {
|
||||
string foo() {
|
||||
return "I am an A.";
|
||||
}
|
||||
}
|
||||
class B {
|
||||
string foo() {
|
||||
return "I am a B.";
|
||||
}
|
||||
}
|
||||
|
||||
class C : A {
|
||||
string className = "C";
|
||||
override string foo() {
|
||||
return "I am a "~className~", and thus an A.";
|
||||
}
|
||||
@property
|
||||
BWrapper asB() {
|
||||
return new BWrapper();
|
||||
}
|
||||
alias asB this;
|
||||
class BWrapper : B {
|
||||
override string foo() {
|
||||
return "I am a "~className~", disguised as a B.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unittest {
|
||||
import std.stdio : writeln;
|
||||
|
||||
auto c = new C();
|
||||
A a = c;
|
||||
B b = c;
|
||||
|
||||
writeln(a.foo());
|
||||
writeln(b.foo());
|
||||
}
|
||||
19
Task/Inheritance-Multiple/D/inheritance-multiple-4.d
Normal file
19
Task/Inheritance-Multiple/D/inheritance-multiple-4.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
template registerable() {
|
||||
void register() { /* implementation */ }
|
||||
}
|
||||
|
||||
string makeFunction(string s) {
|
||||
return `string `~s~`(){ return "`~s~`";}`;
|
||||
}
|
||||
|
||||
class Foo {
|
||||
mixin registerable!();
|
||||
mixin(makeFunction("myFunction"));
|
||||
}
|
||||
|
||||
unittest {
|
||||
import std.stdio : writeln;
|
||||
Foo foo = new Foo;
|
||||
foo.register();
|
||||
writeln(foo.myFunction());
|
||||
}
|
||||
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
|
||||
24
Task/Inheritance-Multiple/Elena/inheritance-multiple-1.elena
Normal file
24
Task/Inheritance-Multiple/Elena/inheritance-multiple-1.elena
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
singleton CameraFeature
|
||||
{
|
||||
cameraMsg
|
||||
= "camera";
|
||||
}
|
||||
|
||||
class MobilePhone
|
||||
{
|
||||
mobileMsg
|
||||
= "phone";
|
||||
}
|
||||
|
||||
class CameraPhone : MobilePhone
|
||||
{
|
||||
dispatch() => CameraFeature;
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var cp := new CameraPhone();
|
||||
|
||||
console.writeLine(cp.cameraMsg);
|
||||
console.writeLine(cp.mobileMsg)
|
||||
}
|
||||
26
Task/Inheritance-Multiple/Elena/inheritance-multiple-2.elena
Normal file
26
Task/Inheritance-Multiple/Elena/inheritance-multiple-2.elena
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import system'dynamic;
|
||||
|
||||
class CameraFeature
|
||||
{
|
||||
cameraMsg
|
||||
= "camera";
|
||||
}
|
||||
|
||||
class MobilePhone
|
||||
{
|
||||
mobileMsg
|
||||
= "phone";
|
||||
}
|
||||
|
||||
singleton CameraPhone
|
||||
{
|
||||
new() = new MobilePhone().mixInto(new CameraFeature());
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var cp := CameraPhone.new();
|
||||
|
||||
console.writeLine(cp.cameraMsg);
|
||||
console.writeLine(cp.mobileMsg)
|
||||
}
|
||||
22
Task/Inheritance-Multiple/F-Sharp/inheritance-multiple.fs
Normal file
22
Task/Inheritance-Multiple/F-Sharp/inheritance-multiple.fs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
type Picture = System.Drawing.Bitmap // (a type synonym)
|
||||
|
||||
// an interface type
|
||||
type Camera =
|
||||
abstract takePicture : unit -> Picture
|
||||
|
||||
// an interface that inherits multiple interfaces
|
||||
type Camera2 =
|
||||
inherits System.ComponentModel.INotifyPropertyChanged
|
||||
inherits Camera
|
||||
|
||||
// a class with an abstract method with a default implementation
|
||||
// (usually called a virtual method)
|
||||
type MobilePhone() =
|
||||
abstract makeCall : int[] -> unit
|
||||
default x.makeCall(number) = () // empty impl
|
||||
|
||||
// a class that inherits from another class and implements an interface
|
||||
type CameraPhone() =
|
||||
inherit MobilePhone()
|
||||
interface Camera with
|
||||
member x.takePicture() = new Picture(10, 10)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
TUPLE: camera ;
|
||||
TUPLE: mobile-phone ;
|
||||
UNION: camera-phone camera mobile-phone ;
|
||||
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)
|
||||
}
|
||||
}
|
||||
30
Task/Inheritance-Multiple/Forth/inheritance-multiple.fth
Normal file
30
Task/Inheritance-Multiple/Forth/inheritance-multiple.fth
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
\ define class camera with method say:
|
||||
:class camera
|
||||
:m say: ." camera " ;m
|
||||
;class
|
||||
|
||||
\ define class phone with method say:
|
||||
:class phone
|
||||
:m say: ." phone " ;m
|
||||
;class
|
||||
|
||||
\ define cameraPhone phone with method say:
|
||||
\ class cameraPhone inherits from both class
|
||||
\ camera and class phone
|
||||
:class cameraPhone super{ camera phone }
|
||||
:m say: self say: \ method conflicts in superclasses
|
||||
\ are resolved by left-to-right order
|
||||
\ so self say: will call the say: method
|
||||
\ from class camera
|
||||
super> phone say: \ super> phone is used to direct
|
||||
\ this say: method to use the
|
||||
\ method from class phone
|
||||
;m
|
||||
;class
|
||||
|
||||
cameraPhone cp \ instantiate a cameraPhone object named cp
|
||||
|
||||
cp say: \ send the say: message to cp
|
||||
|
||||
\ output:
|
||||
camera phone
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' FB does not currently support multiple inheritance. Composition has to be used instead if one wants
|
||||
' to (effectively) inherit from more than one class. In some cases, this might arguably be a better
|
||||
' solution anyway.
|
||||
|
||||
Type Camera Extends Object ' if virtual methods etc needed
|
||||
' ...
|
||||
End Type
|
||||
|
||||
Type Phone Extends Object
|
||||
' ...
|
||||
End Type
|
||||
|
||||
Type CameraPhone Extends Phone ' single inheritance
|
||||
cam As Camera ' using composition here
|
||||
' other stuff
|
||||
End Type
|
||||
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
|
||||
12
Task/Inheritance-Multiple/Io/inheritance-multiple.io
Normal file
12
Task/Inheritance-Multiple/Io/inheritance-multiple.io
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Camera := Object clone
|
||||
Camera click := method("Taking snapshot" println)
|
||||
|
||||
MobilePhone := Object clone
|
||||
MobilePhone call := method("Calling home" println)
|
||||
|
||||
CameraPhone := Camera clone
|
||||
CameraPhone appendProto(MobilePhone)
|
||||
|
||||
myPhone := CameraPhone clone
|
||||
myPhone click // --> "Taking snapshot"
|
||||
myPhone call // --> "Calling home"
|
||||
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...
|
||||
}
|
||||
35
Task/Inheritance-Multiple/Julia/inheritance-multiple.julia
Normal file
35
Task/Inheritance-Multiple/Julia/inheritance-multiple.julia
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
abstract type Phone end
|
||||
|
||||
struct DeskPhone <: Phone
|
||||
book::Dict{String,String}
|
||||
end
|
||||
|
||||
abstract type Camera end
|
||||
|
||||
struct kodak
|
||||
roll::Vector{Array{Int32,2}}
|
||||
end
|
||||
|
||||
struct CellPhone <: Phone
|
||||
book::Dict{String,String}
|
||||
roll::Vector{AbstractVector}
|
||||
end
|
||||
|
||||
function dialnumber(phone::CellPhone)
|
||||
println("beep beep")
|
||||
end
|
||||
|
||||
function dialnumber(phone::Phone)
|
||||
println("tat tat tat tat")
|
||||
end
|
||||
|
||||
function snap(camera, img)
|
||||
println("click")
|
||||
push!(camera.roll, img)
|
||||
end
|
||||
|
||||
dphone = DeskPhone(Dict(["information" => "411"]))
|
||||
cphone = CellPhone(Dict(["emergency" => "911"]), [[]])
|
||||
|
||||
dialnumber(dphone)
|
||||
dialnumber(cphone)
|
||||
28
Task/Inheritance-Multiple/Kotlin/inheritance-multiple.kotlin
Normal file
28
Task/Inheritance-Multiple/Kotlin/inheritance-multiple.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
interface Camera {
|
||||
val numberOfLenses : Int
|
||||
}
|
||||
|
||||
interface MobilePhone {
|
||||
fun charge(n : Int) {
|
||||
if (n >= 0)
|
||||
battery_level = (battery_level + n).coerceAtMost(100)
|
||||
}
|
||||
|
||||
var battery_level : Int
|
||||
}
|
||||
|
||||
data class CameraPhone(override val numberOfLenses : Int = 1, override var battery_level: Int) : Camera, MobilePhone
|
||||
data class TwinLensCamera(override val numberOfLenses : Int = 2) : Camera
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val c = CameraPhone(1, 50)
|
||||
println(c)
|
||||
c.charge(35)
|
||||
println(c)
|
||||
c.charge(78)
|
||||
println(c)
|
||||
println(listOf(c.javaClass.superclass) + c.javaClass.interfaces)
|
||||
val c2 = TwinLensCamera()
|
||||
println(c2)
|
||||
println(listOf(c2.javaClass.superclass) + c2.javaClass.interfaces)
|
||||
}
|
||||
37
Task/Inheritance-Multiple/Lasso/inheritance-multiple.lasso
Normal file
37
Task/Inheritance-Multiple/Lasso/inheritance-multiple.lasso
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
define trait_camera => trait {
|
||||
require zoomfactor
|
||||
|
||||
provide has_zoom() => {
|
||||
return .zoomfactor > 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
define trait_mobilephone => trait {
|
||||
require brand
|
||||
|
||||
provide is_smart() => {
|
||||
return .brand == 'Apple'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
define cameraphone => type {
|
||||
|
||||
trait {
|
||||
import trait_camera, trait_mobilephone
|
||||
}
|
||||
|
||||
data public zoomfactor::integer = 0,
|
||||
public brand::string
|
||||
|
||||
}
|
||||
|
||||
local(mydevice = cameraphone)
|
||||
|
||||
#mydevice -> brand = 'Apple'
|
||||
#mydevice -> zoomfactor = 0
|
||||
|
||||
#mydevice -> has_zoom
|
||||
'<br />'
|
||||
#mydevice -> is_smart
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Camera ::= Mixin clone.
|
||||
MobilePhone ::= Mixin clone.
|
||||
|
||||
CameraPhone ::= Object clone.
|
||||
Camera inject: CameraPhone.
|
||||
MobilePhone inject: CameraPhone.
|
||||
11
Task/Inheritance-Multiple/Lingo/inheritance-multiple-1.lingo
Normal file
11
Task/Inheritance-Multiple/Lingo/inheritance-multiple-1.lingo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
-- parent script "Camera"
|
||||
property resolution
|
||||
|
||||
on new (me)
|
||||
me.resolution = "1024x768"
|
||||
return me
|
||||
end
|
||||
|
||||
on snap (me)
|
||||
put "SNAP!"
|
||||
end
|
||||
13
Task/Inheritance-Multiple/Lingo/inheritance-multiple-2.lingo
Normal file
13
Task/Inheritance-Multiple/Lingo/inheritance-multiple-2.lingo
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-- parent script "MobilePhone"
|
||||
property ringtone
|
||||
|
||||
on new (me)
|
||||
me.ringtone = "Bell"
|
||||
return me
|
||||
end
|
||||
|
||||
on ring (me, n)
|
||||
repeat with i = 1 to n
|
||||
put "RING!!!"
|
||||
end repeat
|
||||
end
|
||||
15
Task/Inheritance-Multiple/Lingo/inheritance-multiple-3.lingo
Normal file
15
Task/Inheritance-Multiple/Lingo/inheritance-multiple-3.lingo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
-- parent script "CameraPhone"
|
||||
property ancestor
|
||||
|
||||
on new (me)
|
||||
c = script("Camera").new()
|
||||
mp = script("MobilePhone").new()
|
||||
|
||||
-- make the Camera instance a parent of the MobilePhone instance
|
||||
mp.setProp(#ancestor, c)
|
||||
|
||||
-- make the MobilePhone instance a parent of this CameraPhone instance
|
||||
me.ancestor = mp
|
||||
|
||||
return me
|
||||
end
|
||||
15
Task/Inheritance-Multiple/Lingo/inheritance-multiple-4.lingo
Normal file
15
Task/Inheritance-Multiple/Lingo/inheritance-multiple-4.lingo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
cp = script("CameraPhone").new()
|
||||
|
||||
cp.snap()
|
||||
-- "SNAP!"
|
||||
|
||||
cp.ring(3)
|
||||
-- "RING!!!"
|
||||
-- "RING!!!"
|
||||
-- "RING!!!"
|
||||
|
||||
put cp.resolution
|
||||
-- "1024x768"
|
||||
|
||||
put cp.ringtone
|
||||
-- "Bell"
|
||||
|
|
@ -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})
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
Module CheckIt {
|
||||
Class Camera {
|
||||
Private:
|
||||
cameratype$
|
||||
Class:
|
||||
module Camera (.cameratype$){
|
||||
}
|
||||
}
|
||||
\\ INHERITANCE AT CODE LEVEL
|
||||
Class MobilePhone {
|
||||
Private:
|
||||
model$
|
||||
Class:
|
||||
module MobilePhone (.model$) {
|
||||
}
|
||||
}
|
||||
Class CameraPhone as Camera as MobilePhone {
|
||||
Module CameraPhone ( .model$, .cameratype$) {
|
||||
}
|
||||
}
|
||||
CP1 =CameraPhone("X-15", "OBSCURE")
|
||||
Print CP1 is type CameraPhone = true
|
||||
Print CP1 is type Camera = true
|
||||
Print CP1 is type MobilePhone = true
|
||||
|
||||
\\ INHERITANCE AT OBJECT LEVEL
|
||||
CP2 = MobilePhone("X-9") with Camera("WIDE")
|
||||
\\ CP3 has no type
|
||||
Group CP3 {
|
||||
Module PrintAll {
|
||||
If this is type Camera and this is type MobilePhone then
|
||||
Print .model$, .cameratype$
|
||||
Else
|
||||
Print "Nothing to print"
|
||||
End if
|
||||
}
|
||||
}
|
||||
CP3.PrintAll ' Nothing to print
|
||||
\\ using pointers and prepate inheritance at object level
|
||||
CP->(CP1 with CP3)
|
||||
CP=>PrintAll
|
||||
CP->(CP2 with CP3)
|
||||
CP=>PrintAll
|
||||
}
|
||||
CheckIt
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
interface ICamera {
|
||||
// ...
|
||||
}
|
||||
|
||||
class MobilePhone {
|
||||
// ...
|
||||
}
|
||||
|
||||
class CameraPhone: MobilePhone, ICamera {
|
||||
// ...
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols binary
|
||||
|
||||
class RInheritMultiple public
|
||||
method main(args = String[]) public static
|
||||
iPhone = RInheritMultiple_CameraPhone()
|
||||
if iPhone <= RInheritMultiple_Camera then -
|
||||
say -
|
||||
'Object' hashToString(iPhone) '['iPhone.getClass().getSimpleName()']' -
|
||||
'is a' RInheritMultiple_Camera.class.getSimpleName()
|
||||
if iPhone <= RInheritMultiple_MobilePhone then -
|
||||
say -
|
||||
'Object' hashToString(iPhone) '['iPhone.getClass().getSimpleName()']' -
|
||||
'is a' RInheritMultiple_MobilePhone.class.getSimpleName()
|
||||
say iPhone.snap()
|
||||
say iPhone.call()
|
||||
return
|
||||
method hashToString(that = Object) public static
|
||||
return '@'(Rexx that.hashCode()).d2x().right(8, 0)
|
||||
|
||||
class RInheritMultiple_Camera private interface
|
||||
-- properties follow...
|
||||
shutter = 'click...'
|
||||
-- method prototypes follow
|
||||
method snap() public returns Rexx
|
||||
|
||||
class RInheritMultiple_MobilePhone private interface
|
||||
-- properties follow...
|
||||
ringTone = 'ring...'
|
||||
-- method prototypes follow
|
||||
method call() public returns Rexx
|
||||
|
||||
class RInheritMultiple_CameraPhone private -
|
||||
implements -
|
||||
RInheritMultiple_Camera, -
|
||||
RInheritMultiple_MobilePhone -
|
||||
uses -
|
||||
RInheritMultiple_Camera, -
|
||||
RInheritMultiple_MobilePhone
|
||||
method RInheritMultiple_CameraPhone() public
|
||||
return
|
||||
-- method implementations follow
|
||||
method snap() public
|
||||
return shutter
|
||||
method call() public
|
||||
return ringTone
|
||||
13
Task/Inheritance-Multiple/Nim/inheritance-multiple.nim
Normal file
13
Task/Inheritance-Multiple/Nim/inheritance-multiple.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
type
|
||||
Camera = ref object of RootObj
|
||||
MobilePhone = ref object of RootObj
|
||||
CameraPhone = object
|
||||
camera: Camera
|
||||
phone: MobilePhone
|
||||
proc `is`(cp: CameraPhone, t: typedesc): bool =
|
||||
for field in cp.fields():
|
||||
if field of t:
|
||||
return true
|
||||
var cp: CameraPhone
|
||||
echo(cp is Camera)
|
||||
echo(cp is MobilePhone)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
class camera =
|
||||
object (self)
|
||||
(*functions go here...*)
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
class mobile_phone =
|
||||
object (self)
|
||||
(*functions go here...*)
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
class camera_phone =
|
||||
object (self)
|
||||
inherit camera
|
||||
inherit mobile_phone
|
||||
(*functions go here...*)
|
||||
end
|
||||
53
Task/Inheritance-Multiple/Objective-C/inheritance-multiple.m
Normal file
53
Task/Inheritance-Multiple/Objective-C/inheritance-multiple.m
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
@interface Camera : NSObject {
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation Camera
|
||||
@end
|
||||
|
||||
@interface MobilePhone : NSObject {
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MobilePhone
|
||||
@end
|
||||
|
||||
@interface CameraPhone : NSObject {
|
||||
Camera *camera;
|
||||
MobilePhone *phone;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation CameraPhone
|
||||
|
||||
-(instancetype)init {
|
||||
if ((self = [super init])) {
|
||||
camera = [[Camera alloc] init];
|
||||
phone = [[MobilePhone alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
-(void)forwardInvocation:(NSInvocation *)anInvocation {
|
||||
SEL aSelector = [anInvocation selector];
|
||||
if ([camera respondsToSelector:aSelector])
|
||||
[anInvocation invokeWithTarget:camera];
|
||||
else if ([phone respondsToSelector:aSelector])
|
||||
[anInvocation invokeWithTarget:phone];
|
||||
else
|
||||
[self doesNotRecognizeSelector:aSelector];
|
||||
}
|
||||
|
||||
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
|
||||
return [camera methodSignatureForSelector:aSelector]
|
||||
?: [phone methodSignatureForSelector:aSelector]
|
||||
?: [super methodSignatureForSelector:aSelector];
|
||||
}
|
||||
|
||||
-(BOOL)respondsToSelector:(SEL)aSelector {
|
||||
return [camera respondsToSelector:aSelector]
|
||||
|| [phone respondsToSelector:aSelector]
|
||||
|| [super respondsToSelector:aSelector];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Property new: Camera
|
||||
Property new: MobilePhone
|
||||
|
||||
Object Class new: CameraPhone
|
||||
CameraPhone is: Camera
|
||||
CameraPhone is: MobilePhone
|
||||
14
Task/Inheritance-Multiple/OoRexx/inheritance-multiple.rexx
Normal file
14
Task/Inheritance-Multiple/OoRexx/inheritance-multiple.rexx
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
-- inherited classes must be created as mixinclasses.
|
||||
::class phone mixinclass object
|
||||
|
||||
::class camera mixinclass object
|
||||
|
||||
-- not a direct subclass of either, but inherits both
|
||||
::class cameraphone inherit phone camera
|
||||
|
||||
-- could also be
|
||||
::class cameraphone1 subclass phone inherit camera
|
||||
|
||||
-- or
|
||||
|
||||
::class cameraphone2 subclass camera inherit phone
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
class Camera
|
||||
string cbuf
|
||||
method TakePhoto()
|
||||
end method
|
||||
method ViewPhoto()
|
||||
end method
|
||||
end class
|
||||
|
||||
class MobilePhone
|
||||
string pbuf
|
||||
method MakeCall()
|
||||
end method
|
||||
method TakeCall()
|
||||
end method
|
||||
end class
|
||||
|
||||
class CameraPhone
|
||||
has Camera,MobilePhone
|
||||
end class
|
||||
|
||||
CameraPhone cp
|
||||
|
||||
cp.ViewPhoto
|
||||
cp.MakeCall
|
||||
5
Task/Inheritance-Multiple/Oz/inheritance-multiple.oz
Normal file
5
Task/Inheritance-Multiple/Oz/inheritance-multiple.oz
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Camera end
|
||||
|
||||
class MobilePhone end
|
||||
|
||||
class CameraPhone from Camera MobilePhone end
|
||||
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 ...
|
||||
}
|
||||
12
Task/Inheritance-Multiple/Phix/inheritance-multiple-1.phix
Normal file
12
Task/Inheritance-Multiple/Phix/inheritance-multiple-1.phix
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class Camra
|
||||
string name = "nikkon"
|
||||
end class
|
||||
class Mobile
|
||||
-- string name = "nokia" -- oops!
|
||||
string mane = "nokia" -- ok!
|
||||
end class
|
||||
class CamraPhone extends Camra,Mobile
|
||||
procedure show() ?{name,mane} end procedure
|
||||
end class
|
||||
CamraPhone cp = new()
|
||||
cp.show()
|
||||
23
Task/Inheritance-Multiple/Phix/inheritance-multiple-2.phix
Normal file
23
Task/Inheritance-Multiple/Phix/inheritance-multiple-2.phix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class Camera
|
||||
public string name = "nikkon"
|
||||
end class
|
||||
class MobilePhone
|
||||
public string name = "nokia" -- (clash no more)
|
||||
end class
|
||||
class CameraPhone
|
||||
-- Camera c = new()
|
||||
-- MobilePhone m = new()
|
||||
public Camera c
|
||||
public MobilePhone m
|
||||
procedure show() ?{c.name,m.name} end procedure
|
||||
end class
|
||||
Camera c = new({"canon"})
|
||||
MobilePhone m = new()
|
||||
CameraPhone cp1 = new({c,m}),
|
||||
cp2 = new({new("Camera"),new("MobilePhone")}),
|
||||
cp3 = new() -- (internal/shared/NULL c,m)
|
||||
cp3.c = new() -- (obviously c must be public)
|
||||
cp3.m = new({"LG20"}) -- "" m "" ""
|
||||
cp1.show()
|
||||
cp2.show()
|
||||
cp3.show() -- crashes without internal/above new()
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(class +Camera)
|
||||
(class +MobilePhone)
|
||||
(class +CameraPhone +MobilePhone +Camera)
|
||||
16
Task/Inheritance-Multiple/Pop11/inheritance-multiple.pop11
Normal file
16
Task/Inheritance-Multiple/Pop11/inheritance-multiple.pop11
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
;;; load object support
|
||||
lib objectclass;
|
||||
|
||||
define :class Camera;
|
||||
;;; slots go here
|
||||
enddefine;
|
||||
|
||||
define :class MobilePhone;
|
||||
;;; slots go here
|
||||
enddefine;
|
||||
|
||||
define :class CameraPhone is Camera, MobilePhone;
|
||||
;;; extra slots go here
|
||||
enddefine;
|
||||
|
||||
;;; methods go here
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Camera {}
|
||||
class MobilePhone {}
|
||||
class CameraPhone : Camera, MobilePhone {}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
Class Camera
|
||||
EndClass
|
||||
|
||||
Class Mobil
|
||||
EndClass
|
||||
|
||||
Class CameraMobile Extends Camera Extends Mobil
|
||||
EndClass
|
||||
|
|
@ -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
|
||||
))
|
||||
6
Task/Inheritance-Multiple/Raku/inheritance-multiple.raku
Normal file
6
Task/Inheritance-Multiple/Raku/inheritance-multiple.raku
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class Camera {}
|
||||
class MobilePhone {}
|
||||
class CameraPhone is Camera is MobilePhone {}
|
||||
|
||||
say CameraPhone.^mro; # undefined type object
|
||||
say CameraPhone.new.^mro; # instantiated object
|
||||
34
Task/Inheritance-Multiple/Ring/inheritance-multiple.ring
Normal file
34
Task/Inheritance-Multiple/Ring/inheritance-multiple.ring
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# Project : Inheritance/Multiple
|
||||
|
||||
mergemethods(:CameraPhone,:MobilePhone)
|
||||
|
||||
o1 = new CameraPhone
|
||||
? o1
|
||||
? o1.testCamera()
|
||||
? o1.testMobilePhone()
|
||||
|
||||
func AddParentClassAttributes oObject,cClass
|
||||
# Add Attributes
|
||||
cCode = "oTempObject = new " + cClass
|
||||
eval(cCode)
|
||||
for cAttribute in Attributes(oTempObject)
|
||||
AddAttribute(oObject,cAttribute)
|
||||
cCode = "oObject." + cAttribute + " = oTempObject." + cAttribute
|
||||
eval(cCode)
|
||||
next
|
||||
|
||||
|
||||
class Camera
|
||||
Name = "Camera"
|
||||
func testCamera
|
||||
? "Message from testCamera"
|
||||
|
||||
class MobilePhone
|
||||
Type = "Android"
|
||||
func testMobilePhone
|
||||
? "Message from MobilePhone"
|
||||
|
||||
class CameraPhone from Camera
|
||||
|
||||
# Add MobilePhone Attributes
|
||||
AddParentClassAttributes(self,:MobilePhone)
|
||||
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
|
||||
3
Task/Inheritance-Multiple/Rust/inheritance-multiple.rust
Normal file
3
Task/Inheritance-Multiple/Rust/inheritance-multiple.rust
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
trait Camera {}
|
||||
trait MobilePhone {}
|
||||
trait CameraPhone: Camera + MobilePhone {}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
trait Camera
|
||||
trait MobilePhone
|
||||
class CameraPhone extends Camera with MobilePhone
|
||||
|
|
@ -0,0 +1 @@
|
|||
camera = ()
|
||||
|
|
@ -0,0 +1 @@
|
|||
mobilePhone = ()
|
||||
|
|
@ -0,0 +1 @@
|
|||
cameraPhone = (| cameraParent* = camera. mobilePhoneParent* = mobilePhone |)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
class Camera {};
|
||||
class MobilePhone {};
|
||||
class CameraPhone << Camera, MobilePhone {};
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
define: #Camera.
|
||||
define: #MobilePhone.
|
||||
define: #CameraPhone &parents: {Camera. MobilePhone}.
|
||||
11
Task/Inheritance-Multiple/Swift/inheritance-multiple.swift
Normal file
11
Task/Inheritance-Multiple/Swift/inheritance-multiple.swift
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
protocol Camera {
|
||||
|
||||
}
|
||||
|
||||
protocol Phone {
|
||||
|
||||
}
|
||||
|
||||
class CameraPhone: Camera, Phone {
|
||||
|
||||
}
|
||||
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
|
||||
}
|
||||
21
Task/Inheritance-Multiple/Wren/inheritance-multiple.wren
Normal file
21
Task/Inheritance-Multiple/Wren/inheritance-multiple.wren
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Camera {
|
||||
construct new() {}
|
||||
snap() { System.print("taking a photo") }
|
||||
}
|
||||
|
||||
class Phone {
|
||||
construct new() {}
|
||||
call() { System.print("calling home") }
|
||||
}
|
||||
|
||||
class CameraPhone is Camera {
|
||||
construct new(phone) { _phone = phone } // uses composition for the Phone part
|
||||
// inherits Camera's snap() method
|
||||
// Phone's call() method can be wrapped
|
||||
call() { _phone.call() }
|
||||
}
|
||||
|
||||
var p = Phone.new()
|
||||
var cp = CameraPhone.new(p)
|
||||
cp.snap()
|
||||
cp.call()
|
||||
3
Task/Inheritance-Multiple/Zkl/inheritance-multiple.zkl
Normal file
3
Task/Inheritance-Multiple/Zkl/inheritance-multiple.zkl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Camera{} class MobilePhone{}
|
||||
class CameraPhone(Camera,MobilePhone){}
|
||||
CameraPhone.linearizeParents
|
||||
Loading…
Add table
Add a link
Reference in a new issue