zoukankan      html  css  js  c++  java
  • 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 ();//没有重写就调用父类的
    		}
    	}
    }
    

  • 相关阅读:
    Tomcat && Servlet
    List,Set,Collections工具类
    多表查询
    常用的API--集合
    msmpeng.exe阻止移动硬盘弹出
    接口400错误解析
    JDBC/Mybatis连接数据库报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
    tomcat启动报错:A child container failed during start
    PAT 1019 数字黑洞
    PAT 1017 A除以B
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852190.html
Copyright © 2011-2022 走看看