自定义转换和重载运算符,这两章节看的不是很明白。有没有具体的案例或者更加详细的说明一下?
我在收看的教程视频是,把代码也给敲出来了,里面有相应的注释,分享一下吧
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;//设置为public或者protected,以便于子类访问
public Pet(string name)//构造一个方法
{
_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();
/*上面这句话 是抽象方法的应用例子,之前版本 虚方法:
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 //继承的应用
{
static int Num;//静态类的应用
static Dog()//静态构造方法的定义
{
Num = 0;
}
public Dog(string name):base(name)//显式调用基类构造函数
{
++Num;
}
new public void PrintName()//重定义函数,隐藏功能实例
{
Console.WriteLine("宠物的名字叫 " + _name);
}
//sealed public override void Speak()//重写抽象函数,用override在派生类中被继承,
//如果加上sealed关键字,那么在Dog的子类Hashiqi里重写Speak函数就会出错,sealed是密封的意思,不能被重写
public override void Speak()//重写抽象函数,用override在派生类中被继承
{
Console.WriteLine(_name + " is speaking"+":wowo");
}
static public void ShowNum()//静态构造方法的调用
{
Console.WriteLine("Dog's number:" + Num);
}
/*public static implicit operator Dog(Cat cat)//隐式转换的定义,目的在于把猫猫变成狗狗,
{
return new Dog(cat._name);
}*/
}
class Cat :Pet,ICatchMice, IClimbTree //子类中接口的调用
{
public Cat(string name) : base(name)
{
}
public override void Speak()
{
Console.WriteLine(_name + " is speaking" + ":mewo");
}
public void ICatchMice()//子类中接口的调用
{
Console.WriteLine(_name+"can catch mice");
}
public void IClimbTree()//子类中接口的调用
{
Console.WriteLine(_name+"can climb tree");
}
public static implicit operator Cat(Dog dog)//隐式转换的定义,目的在于把狗狗变成猫猫,
{
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;//强制转换c为接口类型
c.ICatchMice();//通用接口调用
catchM.ICatchMice();//通用接口调用
IClimbTree climbT = c;//也可不用转换,本身就是一个引用类型
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("下面输出的是自定义转换的实例调用");
dog1.Speak();
Cat cat = dog1;
cat.Speak();
Dog.ShowNum();//静态方法调用
c.HowToFeedCat();
Console.WriteLine();
for (int j = 0; j < pets.Length; j++)
{
++pets[j];//这里的 ++ 就是重装运算符,根据定义,pet的年龄加1,返回pet,
pets[j].ShowAge();
}
}
}
}
重载运算符那章就是把++运算符进行重载,让++运算符明确为++pet._age;(就是宠物的年龄),这样下面调用++方法的时候,动物的_age就会执行++运算。