Oop 如何在Julia中显示字段值

Oop 如何在Julia中显示字段值,oop,field,julia,Oop,Field,Julia,我想知道是否有可能在Julia中显示字段值 例如,此Python程序从consumer类获取对象变量wealth: class Consumer: def __init__(self, w): "Initialize consumer with w dollars of wealth" self.wealth = w def earn(self, y): "The consumer earns

我想知道是否有可能在Julia中显示字段值

例如,此Python程序从consumer类获取对象变量
wealth

   class Consumer:

    def __init__(self, w):
        "Initialize consumer with w dollars of wealth"
        self.wealth = w

    def earn(self, y):
        "The consumer earns y dollars"
        self.wealth += y

    def spend(self, x):
        "The consumer spends x dollars if feasible"
        new_wealth = self.wealth - x
        if new_wealth < 0:
            print("Insufficent funds")
        else:
            self.wealth = new_wealth

c1.wealthc1 = Consumer(10) # Create instance with initial wealth 10
c1.spend(5)
c1.wealth
类消费者:
定义初始化(self,w):
“使用w美元财富初始化消费者”
自我财富=w
def earn(自我,y):
“消费者赚的是y美元”
自我财富+=y
def支出(自我,x):
“如果可行,消费者将花费x美元”
新财富=自我财富-x
如果新财富<0:
打印(“资金不足”)
其他:
自我财富=新财富
c1.wealthc1=消费者(10)#使用初始财富10创建实例
c1.支出(5)
c1.财富

财富变量为
5
。我想知道如何将这段代码翻译成Julia。

Julia不支持类(就OOP而言)

但是,有一些变量可以表示类的变量:

type Consumer
    wealth::Float64
end
现在,由于Julia不支持类,所有方法都必须生活在该类型之外,这使得Julia的关键特性之一,多重分派,也可以使用用户定义的类型。(, ) 因此,您必须添加如下方法:

function earn!(consumer::Consumer, y::Float64)
    println("The consumer earns y dollars")
    consumer.wealth = consumer.wealth + y
end

(类似地,可以实现
spend
功能。)

Julia不支持类(就OOP而言)

但是,有一些变量可以表示类的变量:

type Consumer
    wealth::Float64
end
现在,由于Julia不支持类,所有方法都必须生活在该类型之外,这使得Julia的关键特性之一,多重分派,也可以使用用户定义的类型。(, ) 因此,您必须添加如下方法:

function earn!(consumer::Consumer, y::Float64)
    println("The consumer earns y dollars")
    consumer.wealth = consumer.wealth + y
end

(类似地,可以实现
spend
函数。)

最简单的方法与Python非常相似:

mutable struct Consumer
    wealth
end

function earn(c::Consumer, y)
    c.wealth += y
end

function spend(c::Consumer, y)
    c.wealth -= y
end
现在你可以像这样使用它:

julia> c1 = Consumer(10)
Consumer(10)

julia> spend(c1, 5)
5

julia> c1.wealth
5
mutable struct ConsumerTyped{T<:Real}
    wealth::T
end

function earn(c::ConsumerTyped, y)
    c.wealth += y
end

function spend(c::ConsumerTyped, y)
    c.wealth -= y
end
你可以阅读更多关于它的内容

但也许在《朱莉娅》中你会这样写:

julia> c1 = Consumer(10)
Consumer(10)

julia> spend(c1, 5)
5

julia> c1.wealth
5
mutable struct ConsumerTyped{T<:Real}
    wealth::T
end

function earn(c::ConsumerTyped, y)
    c.wealth += y
end

function spend(c::ConsumerTyped, y)
    c.wealth -= y
end
现在让我们比较两种类型的性能:

julia> using BenchmarkTools

julia> c1 = Consumer(10)
Consumer(10)

julia> c2 = ConsumerTyped(10)
ConsumerTyped{Int64}(10)

julia> @benchmark spend(c1, 1)
BenchmarkTools.Trial:
  memory estimate:  16 bytes
  allocs estimate:  1
  --------------
  minimum time:     56.434 ns (0.00% GC)
  median time:      57.376 ns (0.00% GC)
  mean time:        60.126 ns (0.84% GC)
  maximum time:     847.942 ns (87.69% GC)
  --------------
  samples:          10000
  evals/sample:     992

julia> @benchmark spend(c2, 1)
BenchmarkTools.Trial:
  memory estimate:  16 bytes
  allocs estimate:  1
  --------------
  minimum time:     29.858 ns (0.00% GC)
  median time:      30.791 ns (0.00% GC)
  mean time:        32.835 ns (1.63% GC)
  maximum time:     966.188 ns (90.20% GC)
  --------------
  samples:          10000
  evals/sample:     1000

您可以看到,您可以获得约2倍的加速。

最简单的方法与Python非常相似:

mutable struct Consumer
    wealth
end

function earn(c::Consumer, y)
    c.wealth += y
end

function spend(c::Consumer, y)
    c.wealth -= y
end
现在你可以像这样使用它:

julia> c1 = Consumer(10)
Consumer(10)

julia> spend(c1, 5)
5

julia> c1.wealth
5
mutable struct ConsumerTyped{T<:Real}
    wealth::T
end

function earn(c::ConsumerTyped, y)
    c.wealth += y
end

function spend(c::ConsumerTyped, y)
    c.wealth -= y
end
你可以阅读更多关于它的内容

但也许在《朱莉娅》中你会这样写:

julia> c1 = Consumer(10)
Consumer(10)

julia> spend(c1, 5)
5

julia> c1.wealth
5
mutable struct ConsumerTyped{T<:Real}
    wealth::T
end

function earn(c::ConsumerTyped, y)
    c.wealth += y
end

function spend(c::ConsumerTyped, y)
    c.wealth -= y
end
现在让我们比较两种类型的性能:

julia> using BenchmarkTools

julia> c1 = Consumer(10)
Consumer(10)

julia> c2 = ConsumerTyped(10)
ConsumerTyped{Int64}(10)

julia> @benchmark spend(c1, 1)
BenchmarkTools.Trial:
  memory estimate:  16 bytes
  allocs estimate:  1
  --------------
  minimum time:     56.434 ns (0.00% GC)
  median time:      57.376 ns (0.00% GC)
  mean time:        60.126 ns (0.84% GC)
  maximum time:     847.942 ns (87.69% GC)
  --------------
  samples:          10000
  evals/sample:     992

julia> @benchmark spend(c2, 1)
BenchmarkTools.Trial:
  memory estimate:  16 bytes
  allocs estimate:  1
  --------------
  minimum time:     29.858 ns (0.00% GC)
  median time:      30.791 ns (0.00% GC)
  mean time:        32.835 ns (1.63% GC)
  maximum time:     966.188 ns (90.20% GC)
  --------------
  samples:          10000
  evals/sample:     1000

正如Daniel Muhlbachler指出的那样,这并不完全是OOP,但通过多次调度,您可以获得所需的加速。Python中Julia和OOP之间真正的关键区别在于,正如这里所解释的,Julia中只有抽象类型才能有子类型,因此在某些情况下,您必须使用组合而不是继承。因为您要改变对象,所以应该调用函数
earn
消费。另一方面,使用不可变的
struct
可能更好(更快)。正如Daniel Muhlbachler指出的——这并不完全是OOP,但通过多次分派可以得到想要的结果。Python中Julia和OOP之间真正的关键区别在于,正如这里所解释的,Julia中只有抽象类型才能有子类型,因此在某些情况下,您必须使用组合而不是继承。因为您要改变对象,所以应该调用函数
earn
消费。另一方面,使用不可变的
struct
可能更好(更快)。同意。一个小评论是,
type
关键字当前被
mutable struct
关键字替换(如果您要编写
struct
该类型将是不可变的)。同意。一个小评论是,
type
关键字当前被
mutable struct
关键字替换(如果您要编写
struct
,则类型将是不可变的)。您能将您的措辞更改为与“show”不同的内容吗?在第一次阅读时,我认为你的问题是如何打印数据类型。你能把你的措辞改成不同于“显示”的措辞吗?在第一次阅读时,我认为您的问题是如何打印数据类型。