可以说我有以下类型:
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 setup a OOP model for a library
哪种方法更有效?
文档中的大多数Methods
示例都是简单的函数,如果我想要更复杂的循环或 if 语句是否可能?Method