【发布时间】:2015-01-12 19:52:44
【问题描述】:
假设我有以下类型:
type cat
cry:: String
legs:: Int
fur:: String
end
type car
noise::String
wheels::Int
speed::Int
end
Lion = cat("meow", 4, "fuzzy")
vw = car("honk", 4, 45)
我想为它们添加一个方法describe 来打印它们内部的数据。是否最好使用方法来做到这一点:
describe(a::cat) = println("Type: Cat"), println("Cry:", a.cry, " Legs:",a.legs, " fur:", a.fur);
describe(a::car) = println("Type: Car"), println("Noise:", a.noise, " Wheels:", a.wheels, " Speed:", a.speed)
describe(Lion)
describe(vw)
输出:
Type: Cat
Cry:meow Legs:4 fur:fuzzy
Type: Car
Noise:honk Wheels:4 Speed:45
或者我应该使用我之前发布的这个问题中的函数:Julia: What is the best way to set up a OOP model for a library
哪种方法更有效?
documentation 中的大多数 Methods 示例都是简单的函数,如果我想要一个更复杂的带有循环的 Method 或 if 语句可以吗?
【问题讨论】:
标签: oop julia multiple-dispatch