• コンストラクタで初期化 class Dog { public int Age; public Dog(int age) { Age = age; } public void Bark() { System.out.print("ワン"); } } ... Dog dog = new Dog(3); dog.Bark()
extends Dog { public int Temperature; public HotDog(int age, int temp) : Dog(age) { Temperature = temp; } public void Breath() { System.out.print("ハッハッ"); } } ... HotDog dog = new HotDog(3, 25); dog.Bark()
class Dog { ... 略 ... public void BarkTwice() { Bark(); Bark(); } } class AmericanDog extends Dog { public void Bark() { System.out.print("Bow"); } } ... AmericanDog dog = new AmericanDog(3); dog.BarkTwice() // BowBow
• 別途interfaceを定義 • Javaより型制約がゆるい ◦ 移植する上では問題ない type IDog interface { Bark() } var dog IDog dog = NewHotDog(2, 20) dog.Bark() dog = NewAmericanDog(3) dog.Bark()