using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class shengwu { public void shengming() { Console.WriteLine("生物都有生命"); } public virtual void huxi() { Console.WriteLine("生物都呼吸!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class shengwu { public void shengming() { Console.WriteLine("生物都有生命"); } public virtual void huxi() { Console.WriteLine("生物都呼吸!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class dongwu:shengwu { public void shengming() { Console.WriteLine("动物都有生命!"); } public override void huxi() { Console.WriteLine("动物都呼吸!"); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { shengwu sdata = new shengwu(); sdata.shengming();//建的哪一个类就调用哪个类的方法,调用shengwu中的shengming() dongwu ddata = new dongwu(); ddata.shengming();//同上 shengwu zhdata = ddata; zhdata.shengming();//子类dongwu直接转化为父类shengwu后,调用父类shengwu的方法shengming() dongwu hydata = (dongwu)zhdata; hydata.shengming();//转化为父类的dongwu再转回子类dongwu后,调用子类dongwu的方法shengming() sdata.huxi(); ddata.huxi(); zhdata.huxi();//复写函数(复写前virtual,复写后override),子类转换为父类后调用的复写后(子类)的函数而不是父类的函数 hydata.huxi();//转化为父类的dongwu再转回子类dongwu后,调用子类dongwu的方法shengming() Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { shengwu sdata = new shengwu(); sdata.shengming();//建的哪一个类就调用哪个类的方法,调用shengwu中的shengming() dongwu ddata = new dongwu(); ddata.shengming();//同上 shengwu zhdata = ddata; zhdata.shengming();//子类dongwu直接转化为父类shengwu后,调用父类shengwu的方法shengming() dongwu hydata = (dongwu)zhdata; hydata.shengming();//转化为父类的dongwu再转回子类dongwu后,调用子类dongwu的方法shengming() sdata.huxi(); ddata.huxi(); zhdata.huxi();//复写函数(复写前virtual,复写后override),子类转换为父类后调用的复写后(子类)的函数而不是父类的函数 hydata.huxi();//转化为父类的dongwu再转回子类dongwu后,调用子类dongwu的方法shengming() Console.ReadLine(); } } }
时间: 2024-11-06 09:47:50