Call an object method: Difference between revisions
Content added Content deleted
Line 641: | Line 641: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
module Animal |
module Animal |
||
abstract type Animal end |
abstract type Animal end |
||
abstract type Dog <: Animal end |
abstract type Dog <: Animal end |
||
abstract type Cat <: Animal end |
abstract type Cat <: Animal end |
||
struct Collie <: Dog |
struct Collie <: Dog |
||
name::String |
name::String |
||
weight::Float64 |
weight::Float64 |
||
end |
end |
||
struct Persian <: Cat |
struct Persian <: Cat |
||
name::String |
name::String |
||
weight::Float64 |
weight::Float64 |
||
end |
end |
||
# |
|||
# |
|||
# This function is static: it works on any dog, even on a blank instance, in |
# This function is static: it works on any dog, even on a blank instance, in |
||
# the same way, to produce the same output for anything that isa Dog in type. |
# the same way, to produce the same output for anything that isa Dog in type. |
||
# |
|||
# |
|||
function soundalert(a::T) where T <: Dog |
function soundalert(a::T) where T <: Dog |
||
println("Woof!") |
println("Woof!") |
||
end |
end |
||
# |
|||
# |
|||
# This function depends on the class instance's data for its value, so this type |
# This function depends on the class instance's data for its value, so this type |
||
# of function is one way to do an instance method in Jula. |
# of function is one way to do an instance method in Jula. |
||
# |
|||
# |
|||
masskg(x) = x.weight |
masskg(x) = x.weight |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |