using System; using System.Globalization; using System.Net.Cache;
namespace PetShop { ??? interface ICatchMice //基類中接口的定義 ??? { ??????? void ICatchMice(); ??? } ??? interface IClimbTree //基類中接口的定義 ??? { ??????? void IClimbTree(); ??? } ??? abstract public class Pet ??? { ??????? public int _age; ??????? public string _name;//設(shè)置為public或者protected,以便于子類訪問 ??????? public Pet(string name)//構(gòu)造一個方法 ??????? { ??????????? _name = name; ??????????? _age= 0; ??????? } ??????? public static Pet operator ++ (Pet pet)//重裝運算符定義 ??????? { ??????????? ++pet._age; ??????????? return pet; ??????? } ??????? public void PrintName() ??????? { ??????????? Console.WriteLine("Pet's name is " + _name); ??????? } ??????? public void ShowAge() ??????? { ??????????? Console.WriteLine(_name+"'s Age="+_age); ??????? } ??????? abstract public void Speak(); ??????? /*上面這句話 是抽象方法的應(yīng)用例子,之前版本 虛方法: ???????? virtual public void Speak() ???????? { ??????????? Console.WriteLine(_name+"is speaking "); ????????? }??????? ???????? ???????? */ ??????
??? } ??? static class PetGuide ??? { ??????? static public void HowToFeedDog(this Dog dog) ??????? { ??????????? Console.WriteLine("Play a vedio about how to feed a dog"); ??????? } ??????? static public void HowToFeedCat(this Cat cat) ??????? { ??????????? Console.WriteLine("Play a vedio about how to feed a cat"); ??????? } ??? } ??? public class Dog:Pet //繼承的應(yīng)用 ??? { ??????? static int Num;//靜態(tài)類的應(yīng)用 ??????? static Dog()//靜態(tài)構(gòu)造方法的定義 ??????? { ??????????? Num = 0; ??????? }
??????? public Dog(string name):base(name)//顯式調(diào)用基類構(gòu)造函數(shù) ??????? { ??????????? ++Num; ??????? } ??????? new public void PrintName()//重定義函數(shù),隱藏功能實例 ??????? { ??????????? Console.WriteLine("寵物的名字叫 " + _name); ??????? } ??????? //sealed public override void Speak()//重寫抽象函數(shù),用override在派生類中被繼承, ??????? //如果加上sealed關(guān)鍵字,那么在Dog的子類Hashiqi里重寫Speak函數(shù)就會出錯,sealed是密封的意思,不能被重寫 ??????? public override void Speak()//重寫抽象函數(shù),用override在派生類中被繼承 ??????? { ??????????? Console.WriteLine(_name + " is speaking"+":wowo"); ??????? } ??????? static public void ShowNum()//靜態(tài)構(gòu)造方法的調(diào)用 ??????? { ??????????? Console.WriteLine("Dog's number:" + Num); ??????? } ??????? /*public static implicit operator Dog(Cat cat)//隱式轉(zhuǎn)換的定義,目的在于把貓貓變成狗狗, ??????? { ??????????? return new Dog(cat._name); ??????? }*/ ??? } ??? ??? class Cat :Pet,ICatchMice, IClimbTree //子類中接口的調(diào)用 ??? { ??????? public Cat(string name) : base(name) ??????? { ??????? } ??????? public override void Speak() ??????? { ??????????? Console.WriteLine(_name + " is speaking" + ":mewo"); ??????? } ??????? public void ICatchMice()//子類中接口的調(diào)用 ??????? { ??????????? Console.WriteLine(_name+"can catch mice"); ??????? } ??????? public void IClimbTree()//子類中接口的調(diào)用 ??????? { ??????????? Console.WriteLine(_name+"can climb tree"); ??????? } ??????? public static implicit operator Cat(Dog dog)//隱式轉(zhuǎn)換的定義,目的在于把狗狗變成貓貓, ??????? { ??????????? return new Cat(dog._name); ??????? }
??? } ??? class Hashiqi:Dog ??? { ??????? public Hashiqi(string name) : base(name) ??????? { ??????? } ??????? new public void PrintName() ??????? { ??????????? Console.WriteLine("二哈的名字叫 " + _name); ??????? } ??????? public override void Speak() ??????? { ??????????? Console.WriteLine(_name + " is speaking" + ":wowu"); ??????? }
2020-05-18
我在收看的教程視頻是,把代碼也給敲出來了,里面有相應(yīng)的注釋,分享一下吧
using System;
using System.Globalization;
using System.Net.Cache;
namespace PetShop
{
??? interface ICatchMice //基類中接口的定義
??? {
??????? void ICatchMice();
??? }
??? interface IClimbTree //基類中接口的定義
??? {
??????? void IClimbTree();
??? }
??? abstract public class Pet
??? {
??????? public int _age;
??????? public string _name;//設(shè)置為public或者protected,以便于子類訪問
??????? public Pet(string name)//構(gòu)造一個方法
??????? {
??????????? _name = name;
??????????? _age= 0;
??????? }
??????? public static Pet operator ++ (Pet pet)//重裝運算符定義
??????? {
??????????? ++pet._age;
??????????? return pet;
??????? }
??????? public void PrintName()
??????? {
??????????? Console.WriteLine("Pet's name is " + _name);
??????? }
??????? public void ShowAge()
??????? {
??????????? Console.WriteLine(_name+"'s Age="+_age);
??????? }
??????? abstract public void Speak();
??????? /*上面這句話 是抽象方法的應(yīng)用例子,之前版本 虛方法:
???????? virtual public void Speak()
???????? {
??????????? Console.WriteLine(_name+"is speaking ");
????????? }???????
????????
???????? */
??????
??? }
??? static class PetGuide
??? {
??????? static public void HowToFeedDog(this Dog dog)
??????? {
??????????? Console.WriteLine("Play a vedio about how to feed a dog");
??????? }
??????? static public void HowToFeedCat(this Cat cat)
??????? {
??????????? Console.WriteLine("Play a vedio about how to feed a cat");
??????? }
??? }
??? public class Dog:Pet //繼承的應(yīng)用
??? {
??????? static int Num;//靜態(tài)類的應(yīng)用
??????? static Dog()//靜態(tài)構(gòu)造方法的定義
??????? {
??????????? Num = 0;
??????? }
??????? public Dog(string name):base(name)//顯式調(diào)用基類構(gòu)造函數(shù)
??????? {
??????????? ++Num;
??????? }
??????? new public void PrintName()//重定義函數(shù),隱藏功能實例
??????? {
??????????? Console.WriteLine("寵物的名字叫 " + _name);
??????? }
??????? //sealed public override void Speak()//重寫抽象函數(shù),用override在派生類中被繼承,
??????? //如果加上sealed關(guān)鍵字,那么在Dog的子類Hashiqi里重寫Speak函數(shù)就會出錯,sealed是密封的意思,不能被重寫
??????? public override void Speak()//重寫抽象函數(shù),用override在派生類中被繼承
??????? {
??????????? Console.WriteLine(_name + " is speaking"+":wowo");
??????? }
??????? static public void ShowNum()//靜態(tài)構(gòu)造方法的調(diào)用
??????? {
??????????? Console.WriteLine("Dog's number:" + Num);
??????? }
??????? /*public static implicit operator Dog(Cat cat)//隱式轉(zhuǎn)換的定義,目的在于把貓貓變成狗狗,
??????? {
??????????? return new Dog(cat._name);
??????? }*/
??? }
???
??? class Cat :Pet,ICatchMice, IClimbTree //子類中接口的調(diào)用
??? {
??????? public Cat(string name) : base(name)
??????? {
??????? }
??????? public override void Speak()
??????? {
??????????? Console.WriteLine(_name + " is speaking" + ":mewo");
??????? }
??????? public void ICatchMice()//子類中接口的調(diào)用
??????? {
??????????? Console.WriteLine(_name+"can catch mice");
??????? }
??????? public void IClimbTree()//子類中接口的調(diào)用
??????? {
??????????? Console.WriteLine(_name+"can climb tree");
??????? }
??????? public static implicit operator Cat(Dog dog)//隱式轉(zhuǎn)換的定義,目的在于把狗狗變成貓貓,
??????? {
??????????? return new Cat(dog._name);
??????? }
??? }
??? class Hashiqi:Dog
??? {
??????? public Hashiqi(string name) : base(name)
??????? {
??????? }
??????? new public void PrintName()
??????? {
??????????? Console.WriteLine("二哈的名字叫 " + _name);
??????? }
??????? public override void Speak()
??????? {
??????????? Console.WriteLine(_name + " is speaking" + ":wowu");
??????? }
??? }
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
????????? Pet[] pets=new Pet[] {new Dog("Jack"),new Cat("Tom"),new Hashiqi("wangcai"),new Hashiqi("Chaijiazhe")};
??????????? for(int i=0;i<pets.Length;i++)
??????????? {
??????????????? pets[i].PrintName();
??????????????? pets[i].Speak();
??????????? }
??????????? Cat c = new Cat("Tom2");
??????????? ICatchMice catchM = (ICatchMice)c;//強制轉(zhuǎn)換c為接口類型
??????????? c.ICatchMice();//通用接口調(diào)用
??????????? catchM.ICatchMice();//通用接口調(diào)用
??????????? IClimbTree climbT = c;//也可不用轉(zhuǎn)換,本身就是一個引用類型
??????????? c.IClimbTree();
??????????? climbT.IClimbTree();
??????????? /* Dog dog = new Dog();
???????????? dog.Name = "Jack";
???????????? dog.PrintName();
???????????? Cat cat = new Cat();
???????????? cat.Name = "Tom";
???????????? cat.PrintName();
???????????? Hashiqi erha = new Hashiqi();
???????????? erha.Name = "wangcai";
???????????? erha.PrintName();*/
??????????? Dog dog0 = new Dog("tony");
??????????? Dog dog1 = new Dog("tommy");
??????????? dog0.HowToFeedDog();
??????????? dog1.HowToFeedDog();
??????????? Console.WriteLine("下面輸出的是自定義轉(zhuǎn)換的實例調(diào)用");
??????????? dog1.Speak();
??????????? Cat cat = dog1;
??????????? cat.Speak();
??????????? Dog.ShowNum();//靜態(tài)方法調(diào)用
??????????? c.HowToFeedCat();
??????????? Console.WriteLine();
??????????
??????????? for (int j = 0; j < pets.Length; j++)
??????????? {
??????????????? ++pets[j];//這里的 ++ 就是重裝運算符,根據(jù)定義,pet的年齡加1,返回pet,
??????????????? pets[j].ShowAge();
??????????? }
???????
??????? }
??? }
}
2019-11-25
重載運算符那章就是把++運算符進行重載,讓++運算符明確為++pet._age;(就是寵物的年齡),這樣下面調(diào)用++方法的時候,動物的_age就會執(zhí)行++運算。