Tas*_*nou 14 oop single-dispatch julia
我在一本书中读到"你不能用julia创造传统'课程',采用单调度方式,比如obj.myfunc()"......我认为听起来更像是挑战而不是事实.
所以这是我的JavaClass公共/私人领域和方法的类型,仅仅是为了在朱莉娅有一些像丑陋这样丑陋的冲击和恐怖因素,在开发人员为避免它而烦恼之后:
type JavaClass
    # Public fields
    name::String
    # Public methods
    getName::Function
    setName::Function
    getX::Function
    getY::Function
    setX::Function
    setY::Function
    # Primary Constructor - "through Whom all things were made."
    function JavaClass(namearg::String, xarg::Int64, yarg::Int64)
        # Private fields - implemented as "closed" variables
        x = xarg
        y = yarg
        # Private methods used for "overloading"
        setY(yarg::Int64) = (y = yarg; return nothing)
        setY(yarg::Float64) = (y = Int64(yarg * 1000); return nothing)
        # Construct object
        this = new()
        this.name = namearg
        this.getName = () -> this.name
        this.setName = (name::String) -> (this.name = name; return nothing)
        this.getX = () -> x
        this.getY = () -> y
        this.setX = (xarg::Int64) -> (x = xarg; return nothing)
        this.setY = (yarg) -> setY(yarg) #Select appropriate overloaded method
        # Return constructed object
        return this
    end
    # a secondary (inner) constructor
    JavaClass(namearg::String) = JavaClass(namearg, 0,0)
end
使用示例:
julia> a = JavaClass("John", 10, 20);
julia> a.name # public
"John"
julia> a.name = "Jim";
julia> a.getName()
"Jim"
julia> a.setName("Jack")
julia> a.getName()
"Jack"
julia> a.x # private, cannot access
ERROR: type JavaClass has no field x
julia> a.getX()
10
julia> a.setX(11)
julia> a.getX()
11
julia> a.setY(2) # "single-dispatch" call to Int overloaded method
julia> a.getY()
2
julia> a.setY(2.0)
julia> a.getY()  # "single-dispatch" call to Float overloaded method
2000
julia> b = JavaClass("Jill"); # secondary constructor
julia> b.getX()
0
本质上,构造函数变成了一个闭包,即创建"私有"字段和方法/重载的方式.有什么想法吗?(除了"OMG为什么???你为什么要这样做?")
除了其他方法吗?
您可以设想任何可能导致失败的场景吗?
Jef*_*son 29
当然,这不是在朱莉娅中创建对象和方法的惯用方法,但它也没有任何可怕的错误.在任何带闭包的语言中,您都可以像这样定义自己的"对象系统",例如,查看在Scheme中开发的许多对象系统.
在julia v0.5中,由于闭包将捕获的变量自动表示为对象字段,因此有一种特别灵活的方法可以做到这一点.例如:
julia> function Person(name, age)
        getName() = name
        getAge() = age
        getOlder() = (age+=1)
        ()->(getName;getAge;getOlder)
       end
Person (generic function with 1 method)
julia> o = Person("bob", 26)
(::#3) (generic function with 1 method)
julia> o.getName()
"bob"
julia> o.getAge()
26
julia> o.getOlder()
27
julia> o.getAge()
27
你必须返回一个函数才能做到这一点很奇怪,但确实如此.这可以从许多优化中获益,例如为您确定精确字段类型的语言,因此在某些情况下我们甚至可以内联这些"方法调用".另一个很酷的功能是功能的底线控制哪些字段是"公共的"; 那里列出的任何东西都将成为对象的一个领域.在这种情况下,您只获取方法,而不是名称和年龄变量.但是,如果你添加name到列表中,那么你也可以做到o.name.当然这些方法也是多方法; 您可以为其他人添加多个定义getOlder,它会像您期望的那样工作.
| 归档时间: | 
 | 
| 查看次数: | 1602 次 | 
| 最近记录: |