C#学习笔记之——重载

using System;

namespace Lesson12_12
{
	public class Pet
	{
		public string name;

//		public void Bark () {
//			Console.WriteLine ("{0} is shouting", name);
//		}

		public virtual void Bark () {
			Console.WriteLine ("{0} is shouting", name);
		}

		public Pet ()
		{
		}
	}

	public class Cat : Pet
	{
		public string servant;

//		public new void Bark () {
//			Console.WriteLine ("{0} meow", name);
//		}

		public override void Bark () {
			Console.WriteLine ("{0} meow", name);
		}
	}

	public class Dog : Pet
	{
		public string monster;

//		public new void Bark () {
//			Console.WriteLine ("{0} bow-wow", name);
//		}

//		public override void Bark () {
//			Console.WriteLine ("{0} meow", name);
//		}
	}

	public class PetCare
	{
		public PetCare () {
		}

//		public void PetShower (Cat cat) {
//			cat.bark ();
//			Console.WriteLine (cat.name + " is taking a shower.");
//		}
//
//		public void PetShower (Dog dog) {
//			dog.bark ();
//			Console.WriteLine (dog.name + " is taking a shower");
//		}

		public void PetShower (Pet pet) {
			Console.WriteLine (pet.name + " is taking a shower");
		}

		public void Call (Pet pet) {
			//里式转换原则二
			Cat cat = pet as Cat;//类型转换
			Console.WriteLine(cat.servant + " come and get your monster");
		}
	}

	class MainClass
	{
		public static void Main (string[] args)
		{
			Cat cat = new Cat ();
			cat.name = "cat";
			cat.servant = "remi";

			Dog dog = new Dog ();
			dog.name = "Pappy";

//			PetCare petCare = new PetCare ();
//
//			petCare.PetShower (cat);
//			petCare.PetShower (dog);
//
//			petCare.Call (cat);
//			bool isCat = dog is Cat;
//			if (isCat) {
//				Console.WriteLine ("dog is cat");
//			} else
//				Console.WriteLine ("dog isn't cat");

			cat.Bark ();
			dog.Bark ();//没有重写就调用父类的
		}
	}
}

posted @ 2017-12-12 17:16  养鼠的猫  阅读(98)  评论(0编辑  收藏  举报