Inheritance/Multiple: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 66: | Line 66: | ||
}CameraPhone; |
}CameraPhone; |
||
</lang> |
</lang> |
||
=={{header|C++}}== |
|||
<lang cpp>class Camera |
|||
{ |
|||
// ... |
|||
}; |
|||
class MobilePhone |
|||
{ |
|||
// ... |
|||
}; |
|||
class CameraPhone: |
|||
public Camera, |
|||
public MobilePhone |
|||
{ |
|||
// ... |
|||
};</lang> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Line 103: | Line 85: | ||
// ... |
// ... |
||
}</lang> |
}</lang> |
||
=={{header|C++}}== |
|||
<lang cpp>class Camera |
|||
{ |
|||
// ... |
|||
}; |
|||
class MobilePhone |
|||
{ |
|||
// ... |
|||
}; |
|||
class CameraPhone: |
|||
public Camera, |
|||
public MobilePhone |
|||
{ |
|||
// ... |
|||
};</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 396: | Line 396: | ||
console.writeLine(cp.mobileMsg) |
console.writeLine(cp.mobileMsg) |
||
}</lang> |
}</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
A class can only inherit from one other class, but it can implement any number of interfaces. |
|||
<lang fsharp>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)</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 462: | Line 488: | ||
' other stuff |
' other stuff |
||
End Type</lang> |
End Type</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
A class can only inherit from one other class, but it can implement any number of interfaces. |
|||
<lang fsharp>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)</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 621: | Line 621: | ||
myPhone call // --> "Calling home"</lang> |
myPhone call // --> "Calling home"</lang> |
||
In Io each object has an internal list of prototype objects it inherits from. You can add to this list with <code>appendProto</code>. |
In Io each object has an internal list of prototype objects it inherits from. You can add to this list with <code>appendProto</code>. |
||
=={{header|Ioke}}== |
=={{header|Ioke}}== |
||
<lang ioke>Camera = Origin mimic |
<lang ioke>Camera = Origin mimic |
||
Line 680: | Line 681: | ||
{{omit|Julia}} |
{{omit|Julia}} |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 1,096: | Line 1,094: | ||
(*functions go here...*) |
(*functions go here...*) |
||
end</lang> |
end</lang> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 1,204: | Line 1,201: | ||
# methods ... |
# methods ... |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2012.06}} |
|||
<lang perl6>class Camera {} |
|||
class MobilePhone {} |
|||
class CameraPhone is Camera is MobilePhone {} |
|||
say CameraPhone.^mro; # undefined type object |
|||
say CameraPhone.new.^mro; # instantiated object</lang> |
|||
{{out}} |
|||
<pre>CameraPhone() Camera() MobilePhone() Any() Mu() |
|||
CameraPhone() Camera() MobilePhone() Any() Mu()</pre> |
|||
The <tt>.^mro</tt> is not an ordinary method call, |
|||
but a call to the object's metaobject |
|||
that returns the method resolution order for this type. |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,308: | Line 1,288: | ||
class CameraPhone : Camera, MobilePhone {} |
class CameraPhone : Camera, MobilePhone {} |
||
</lang> |
</lang> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Line 1,346: | Line 1,325: | ||
;; implement methods here |
;; implement methods here |
||
))</lang> |
))</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2012.06}} |
|||
<lang perl6>class Camera {} |
|||
class MobilePhone {} |
|||
class CameraPhone is Camera is MobilePhone {} |
|||
say CameraPhone.^mro; # undefined type object |
|||
say CameraPhone.new.^mro; # instantiated object</lang> |
|||
{{out}} |
|||
<pre>CameraPhone() Camera() MobilePhone() Any() Mu() |
|||
CameraPhone() Camera() MobilePhone() Any() Mu()</pre> |
|||
The <tt>.^mro</tt> is not an ordinary method call, |
|||
but a call to the object's metaobject |
|||
that returns the method resolution order for this type. |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
Line 1,431: | Line 1,428: | ||
define: #MobilePhone. |
define: #MobilePhone. |
||
define: #CameraPhone &parents: {Camera. MobilePhone}.</lang> |
define: #CameraPhone &parents: {Camera. MobilePhone}.</lang> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
Like Objective-C, Swift does not allow multiple inheritance. However, you can conform to multiple protocols. |
Like Objective-C, Swift does not allow multiple inheritance. However, you can conform to multiple protocols. |
||
Line 1,454: | Line 1,452: | ||
superclass Camera MobilePhone |
superclass Camera MobilePhone |
||
}</lang> |
}</lang> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |