From 9310252adcd26a240f71c9581f40358d939e44dd Mon Sep 17 00:00:00 2001 From: PuqiAR Date: Mon, 9 Feb 2026 13:21:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0interface=E7=9A=84=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=EF=BC=8C=E4=B8=8A=E4=B8=80=E4=B8=AAcommit=E5=85=B6?= =?UTF-8?q?=E5=AE=9E=E6=98=AF=E2=80=9D=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=BB=84=E5=90=88=EF=BC=81=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ExampleCodes/4-Interface.fig | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/ExampleCodes/4-Interface.fig b/ExampleCodes/4-Interface.fig index 4cfc9dc..2fcde81 100644 --- a/ExampleCodes/4-Interface.fig +++ b/ExampleCodes/4-Interface.fig @@ -1,2 +1,94 @@ import std.io; +interface Document +{ + getDepth() -> Int; // return type is necessary + getName() -> String; + + /* toString() -> String + { + // default implementation + } + */ +} + +struct File +{ + public depth: Int; + public name: String; +} + +impl Document for File +{ + getDepth() + { + return depth; + } + getName() + { + return name; + } +} + +struct Folder +{ + public depth: Int; + public name: String; + public childs: List = []; +} + +impl Document for Folder +{ + getDepth() + { + return depth; + } + + getName() + { + return name; + } +} + +const root_folder := new Folder{ + 0, + "root", + [ + new File{ + 1, + "joyo.txt" + }, + new Folder{ + 2, + "joyoyo", + [ + new File{ + 3, + "JOYO2.txt" + }, + new Folder{ + 3, + "joyoyoyo" + + }, + ] + } + ] +}; + +func print_directory(root: Document) +{ +io.print(" " * root.getDepth()); +io.println(root.getDepth(), root.getName()); +if root is Folder +{ + for var i := 0; i < root.childs.length(); i += 1 + { + var child := root.childs[i]; + print_directory(child); + } + +} +} + +print_directory(root_folder); \ No newline at end of file