In [ ]:
#
# 나는 julia 를 사랑하는가
#
In [ ]:
# 새로운 객체를 만들어내는 함수를 생성자라고 부른다.
# Constructors
In [1]:
type Foo
bar
baz
end
In [2]:
foo = Foo( 1, 2)
Out[2]:
In [3]:
foo.bar
Out[3]:
In [4]:
foo.baz
Out[4]:
In [5]:
# Outer Constructor
Foo(x) = Foo(x, x)
Out[5]:
In [6]:
foo = Foo(5)
Out[6]:
In [7]:
Foo() = Foo(0)
Out[7]:
In [8]:
foo = Foo()
Out[8]:
In [9]:
# Outer constructor
# Inner constructor
# 외부 생성자
# 내부 생성자
type OrderedPair
x::Real
y::Real
OrderedPair(x, y) = x > y ? error("순서가 틀려먹었다") : new(x,y)
end
In [10]:
OrderedPair(1,2)
Out[10]:
In [11]:
OrderedPair(2,1)
In [12]:
# 불완전 초기화
# 자기참조 객체
# 재귀적 데이타 구조
type SelfReferential
obj::SelfReferenctial
end
In [14]:
type SelfReferential
obj::SelfReferential
SelfReferential() = (x = new(); x.obj = x)
end
In [15]:
x = SelfReferential()
Out[15]:
In [16]:
is(x, x)
Out[16]:
In [17]:
is(x, x.obj)
Out[17]:
In [18]:
is(x, x.obj.obj)
Out[18]:
In [19]:
type Incomplete
xx
Incomplete() = new()
end
In [20]:
z = Incomplete()
Out[20]:
In [21]:
z.xx
In [22]:
type HasPlain
n::Int
HasPlain() = new()
end
In [23]:
h = HasPlain()
Out[23]:
In [24]:
h.n
Out[24]:
In [25]:
# Parametric Constructor
type Point{T <: Real}
x::T
y::T
end
In [26]:
Point(1,2)
Out[26]:
In [27]:
Point(1.0, 2.5)
Out[27]:
In [28]:
Point(1, 2.5)
In [29]:
Point{Int64}(1.0, 2.5)
In [30]:
Point{Float64}(1.0,2.5)
Out[30]:
In [31]:
Point{Float64}(1,2)
Out[31]:
In [32]:
x = 1
Out[32]:
In [33]:
typeof(x)
Out[33]:
In [34]:
y = convert(Float64, x)
Out[34]:
In [35]:
typeof(y)
Out[35]:
In [36]:
x2, y2 = promote( x, y)
Out[36]:
In [37]:
( typeof(x2), typeof(y2))
Out[37]:
In [38]:
typeof(ans)
Out[38]:
In [39]:
s = widen(Int32)
Out[39]:
In [40]:
typeof(s)
Out[40]: