From 15e62f467eb840bf054fddc07c31d7d00214724e Mon Sep 17 00:00:00 2001 From: abdul124 Date: Fri, 16 Aug 2024 17:43:42 +0800 Subject: [PATCH] standalone: add tests for polymorphism --- nac3standalone/demo/src/inheritance.py | 53 +++++++++++++++++++++----- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/nac3standalone/demo/src/inheritance.py b/nac3standalone/demo/src/inheritance.py index d280e3a5..c908c322 100644 --- a/nac3standalone/demo/src/inheritance.py +++ b/nac3standalone/demo/src/inheritance.py @@ -10,23 +10,58 @@ class A: def __init__(self, a: int32): self.a = a - def f1(self): - self.f2() - - def f2(self): + def output_all_fields(self): output_int32(self.a) + + def set_a(self, a: int32): + self.a = a class B(A): b: int32 def __init__(self, b: int32): - self.a = b + 1 + A.__init__(self, b + 1) + self.set_b(b) + + def output_parent_fields(self): + A.output_all_fields(self) + + def output_all_fields(self): + A.output_all_fields(self) + output_int32(self.b) + + def set_b(self, b: int32): self.b = b +class C(B): + c: int32 + + def __init__(self, c: int32): + B.__init__(self, c + 1) + self.c = c + + def output_parent_fields(self): + B.output_all_fields(self) + + def output_all_fields(self): + B.output_all_fields(self) + output_int32(self.c) + + def set_c(self, c: int32): + self.c = c def run() -> int32: - aaa = A(5) - bbb = B(2) - aaa.f1() - bbb.f1() + ccc = C(10) + ccc.output_all_fields() + ccc.set_a(1) + ccc.set_b(2) + ccc.set_c(3) + ccc.output_all_fields() + + bbb = B(10) + bbb.set_a(9) + bbb.set_b(8) + bbb.output_all_fields() + ccc.output_all_fields() + return 0