diff --git a/examples/advanced/02_polymorphism.peb b/examples/advanced/02_polymorphism.peb new file mode 100644 index 0000000..656b831 --- /dev/null +++ b/examples/advanced/02_polymorphism.peb @@ -0,0 +1,66 @@ +class Shape { + init(color) { + this.color = color + } + + name() { + return "shape" + } + + area() { + return 0 + } + + describe() { + print("A", this.color, this.name(), "with area", this.area()) + } +} + +class Rectangle < Shape { + init(color, width, height) { + super.init(color) + this.width = width + this.height = height + } + + name() { + return "rectangle" + } + + area() { + return this.width * this.height + } +} + +class Square < Rectangle { + init(color, size) { + super.init(color, size, size) + } + + name() { + return "square" + } +} + +class Circle < Shape { + init(color, radius) { + super.init(color) + this.radius = radius + } + + name() { + return "circle" + } + + area() { + return this.radius * this.radius * 3.1415 + } +} + +let rect = Rectangle("red", 3, 4) +let square = Square("green", 5) +let circle = Circle("blue", 2) + +rect.describe() +square.describe() +circle.describe() \ No newline at end of file diff --git a/main.py b/main.py index a7be3a5..769968c 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ from src.pebble import Pebble def main(): - path: Path = Path("examples/basic/21_super.peb") + path: Path = Path("examples/advanced/02_polymorphism.peb") Pebble.run_file(path)