erg/examples/class.er
2022-09-09 14:18:03 +09:00

18 lines
474 B
Python

@Inheritable
Point2D = Class {x = Int; y = Int}
Point2D.
norm self = self::x**2 + self::y**2
Point3D = Inherit Point2D, Additional := {z = Int}
Point3D.
# Overloading is prohibited by default. Remove this decorator and check for errors.
@Override
new x, y, z =
Point3D::__new__ {x; y; z}
@Override
norm self = self::x**2 + self::y**2 + self::z**2
p = Point2D.new {x = 1; y = 2}
print! p, p.norm()
q = Point3D.new 1, 2, 3
print! q, q.norm()