Call an object method: Difference between revisions

Content added Content deleted
(→‎{{header|Factor}}: change a class to a regular tuple; makes for a better example)
Line 639: Line 639:
If you have a object called <tt>x</tt> and a method called <tt>y</tt> then you can write:
If you have a object called <tt>x</tt> and a method called <tt>y</tt> then you can write:
<lang javascript>x.y()</lang>
<lang javascript>x.y()</lang>

=={{header|Julia}}==
module Animal

abstract type Animal end
abstract type Dog <: Animal end
abstract type Cat <: Animal end
struct Collie <: Dog
name::String
weight::Float64
end

struct Persian <: Cat
name::String
weight::Float64
end

#
# 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.
#
function soundalert(a::T) where T <: Dog
println("Woof!")
end

#
# 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.
#
masskg(x) = x.weight


=={{header|Kotlin}}==
=={{header|Kotlin}}==