泛型+多態,多態

來源:互聯網
上載者:User

泛型+多態,多態

問:泛型 T我們常說是類的預留位置,但是這個"T"是通過什麼機制,來正常運行得到正確的結果呢?

答:泛型是在多態上開花結果的。

舉一個簡單的例子:男女平均身高是不同的,所以判斷男,女身高的標準就不同,但是兩者的比較過程是完全一樣的。如果我們唯寫一個比較身高的方法,如果按照男士的平均身高進行判斷,那麼對女士來說就是不準確的,如果按照女士的平均身高來判斷,那麼對男士來說是不準確的,那麼如何做到判斷男士身高根據男士平均身高,判斷女士根據女士平均身高呢?對象為男士的話,調用男士對象的身高方法,對象為女士的話,調用女士的身高方法。

 

 

 

現在建立一個父類:

namespaceGeneric.demo{    public class Person    {        public double height;        public string name;        public Person()        {            this.height = 1.6;            this.name = "小菜";        }         public Person(string name,doubleheight)        {            this.height = height;            this.name = name;        }        virtual public void IsTall()        {            if (height > 1.68)            {                Console.Write("The Personnamed {0} is tall", name);                Console.WriteLine();            }            else            {                Console.Write("The Personnamed {0} is short", name);                Console.WriteLine();            }        }     }}


現在建立一個子類

namespaceGeneric.demo{    public class Woman : Person    {        public Woman()        {            this.height = 1.68;            this.name = "小紅";        }        public Woman(string name, doubleheight)        {            this.height = height;            this.name = name;        }          public override void IsTall()        {            if (height > 1.68)            {                Console.Write("The Womannamed {0} is tall", name);                Console.WriteLine();            }            else            {                Console.Write("The Womannamed {0} is short", name);                Console.WriteLine();            }        }    }}


現在建立一個子類:

namespaceGeneric.demo{    public class Man : Person    {        public Man()        {            this.height = 1.78;            this.name = "張亮";        }        public Man(string name, double height)        {            this.height = height;            this.name = name;        }        public override void IsTall()        {            if (height > 1.78)            {                Console.Write("The Mannamed {0} is tall", name);                Console.WriteLine();            }            else            {                Console.Write("The Womannamed {0} is short", name);                Console.WriteLine();            }        }    }}


現在建立一個泛型類:

namespaceGeneric.demo{    public class HeightCompare<T> where T: Person    {        public void IsTall(T person)        {            person.IsTall();        }    }}


 

現在對泛型類進行調用

static void Main(string[] args)        {             //人對象            Person person=newPerson("張",1.79);             HeightCompare<Person> hc =new HeightCompare<Person>();             hc.IsTall(person);             //男人對象            Man man = newMan("李",1.80);             HeightCompare<Man> hcm = newHeightCompare<Man>();             hcm.IsTall(man);             //女人對象            Woman woman = newWoman("楊", 1.69);            HeightCompare<Woman> hcw =new HeightCompare<Woman>();            hcw.IsTall(woman);        }


 

運行結果:

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.