• 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 ();//没有重写就调用父类的
    		}
    	}
    }
    

  • 相关阅读:
    UltraEdit的配置
    字符编码笔记:ASCII,Unicode和UTF-8
    Hello World Hexo
    好久不见,味素
    记一次springboot+dubbo+zookeeper吐血的问题
    [深度学习]模型部署之优化
    [深度学习]pytorch部署之onnx
    count(*)、count(1)、count(column)
    like %和-的区别与使用
    SQL语言分类
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852190.html
Copyright © 2020-2023  润新知