Abstract type: Difference between revisions
Content added Content deleted
m (→{{header|M2000 Interpreter}}: bare lang tag) |
No edit summary |
||
Line 3,135: | Line 3,135: | ||
Sub Method_Must_Be_Implemented() |
Sub Method_Must_Be_Implemented() |
||
End Interface</lang> |
End Interface</lang> |
||
=={{header|Vlang}}== |
|||
{{trans|go}} |
|||
Vlangs ''interface type'' is an abstract type. It defines a set of methods that a ''concrete type'' must have to satisfy it. |
|||
A variable of an interface type can hold a value of any type that implements the methods that are specified in the interface. You don't need to explicitly "declare" that the type "implements" the interface or anything like that -- the compatibility is purely structural based on the methods. |
|||
In the following example, the Dog and Cat types both satisfy the Beast interface because they each have the specified methods. The ''bprint'' function can print details for any Beast. |
|||
<lang vlang>interface Beast { |
|||
kind() string |
|||
name() string |
|||
cry() string |
|||
} |
|||
struct Dog { |
|||
kind string |
|||
name string |
|||
} |
|||
fn (d Dog) kind() string { return d.kind } |
|||
fn (d Dog) name() string { return d.name } |
|||
fn (d Dog) cry() string { return "Woof" } |
|||
struct Cat { |
|||
kind string |
|||
name string |
|||
} |
|||
fn (c Cat) kind() string { return c.kind } |
|||
fn (c Cat) name() string { return c.name } |
|||
fn (c Cat) cry() string { return "Meow" } |
|||
fn bprint(b Beast) { |
|||
println("${b.name()}, who's a ${b.kind()}, cries: ${b.cry()}.") |
|||
} |
|||
fn main() { |
|||
d := Dog{"labrador", "Max"} |
|||
c := Cat{"siamese", "Sammy"} |
|||
bprint(d) |
|||
bprint(c) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Max, who's a labrador, cries: "Woof". |
|||
Sammy, who's a siamese, cries: "Meow". |
|||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |