C# 派生和继承(派生类与基类)
程序员文章站
2022-06-10 16:05:58
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("睡觉"); } } }
推荐阅读