面向对象:继承关系
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class shengwu { private string _Name; public string Name { get { return _Name; } set { _Name = value; } } public void life() { Console.WriteLine("I am alive!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class dongwu:shengwu { private string _Dname; public string Dname { get { return _Dname; } set { _Dname = value; } } public void eat() { Console.WriteLine("I am hungry!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class zhiwu:shengwu { private string _Zname; public string Zname { get { return _Zname; } set { _Zname = value; } } public void light() { Console.WriteLine("光合作用!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class zhiwu:shengwu { private string _Zname; public string Zname { get { return _Zname; } set { _Zname = value; } } public void light() { Console.WriteLine("光合作用!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class tiger:dongwu { private string type; public string Type { get { return type; } set { type = value; } } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { shengwu data = new shengwu();//父类 data.Name = "GOD"; Console.WriteLine(data.Name); data.life(); dongwu ddata = new dongwu(); ddata.eat();//子类自身的属性:多态 ddata.life();//子类继承父类的life()方法 ddata.Dname="dog";//子类继承父类的Sname zhiwu zdata = new zhiwu(); zdata.Zname = "";//继承父类同上 tiger tdata = new tiger(); tdata.Name = "";//继承祖父类shengwu tdata.Dname = "";//继承父类dongwu tdata.Type = "";//自身属性 object odata = data;//所有类的基类 shengwu sdata = ddata;//子类可以直接转化为父类;ddata子类转成shengwu父类 dongwu adata = (dongwu)ddata;//父类转子类条件:父类的对象必须是由某个子类的对象转换来的,才能转换回去:把转化成父类的ddata转换为子类ddata Console.WriteLine(adata.Dname);// //zhiwu zzdata = (zhiwu)ddata;不可以,ddata本身为dongwu类,不能强制转换为zhiwu类 Console.ReadLine(); } } }
二百个不间断的重复,只是让我看到了人的命运无法改变这一事实而已。