using System; using System.Collections.Generic; using System.Text; namespace 繼承 { class Program { static void Main(string[] args) { Mammal mammal = ne ...
using System; using System.Collections.Generic; using System.Text; namespace 繼承 { class Program { static void Main(string[] args) { Mammal mammal = new Mammal(); Console.WriteLine("我是一隻哺乳動物"); mammal.Scukle(); mammal.Breate(); mammal.Sleep(); mammal.Message(); } } class Mammal : Vertebrate//派生類:基類 { private string arms; private string legs; private int age; public int Age { set { age = value; } get { return age; } } public Mammal() { arms = "前肢"; legs = "後肢"; Age = 0; Weigth = 10; Temperature = 37; } public void Scukle() { Console.WriteLine("哺乳"); } public void Message() { Console.WriteLine("體重:{0}", Weigth); Console.WriteLine("年齡:{0}", Age); Console.WriteLine("體溫:{0}", Temperature); Console.WriteLine("我有{0}和{1}", arms, legs); } } }
using System; using System.Collections.Generic; using System.Text; namespace 繼承 { class Vertebrate { private string spine; private double weigth; private double temperature; public double Weigth { set { if (value < 0) { weigth = 0; } else { weigth = value; } } get { return weigth; } } public double Temperature { set { if (value < 0) { temperature = 0; } else { temperature = value; } } get { return temperature; } } public Vertebrate() { spine = "脊柱"; weigth = 0; temperature = 0; } public void Breate() { Console.WriteLine("呼吸"); } public void Sleep() { Console.WriteLine("睡覺"); } } }