zoukankan      html  css  js  c++  java
  • 自定义类和集合

    创建三个自定义类和集合。

    animal类。

    using System;
    namespace a
    {
    	public abstract class Animal
    	{
    		protected string name;
    		public string Name
    		{
    			get
    			{
    				return name;
    			}
    			set
    			{
    				name=value;
    			}
    		}
    		public Animal()
    		{
    			name="The animal with no name";
    		}
    		public Animal (string newName)
    		{
    			name=newName;
    		}
    		public void Feed()
    		{
    			Console.WriteLine("{0} has been fed.",name);
    		}
    	}
    }

    chicken类。

    using System;
    namespace a
    {
    	public class Chicken:Animal
    	{
    		public void LayEgg()
    		{
    			Console.WriteLine("{0} has laid an egg.",name);
    		}
    		public Chicken(string newName):base(newName)
    		{
    		}
    	}
    }

    cow类。

    using System;
    namespace a
    {
    	public class Cow:Animal
    	{
    		public void Milk()
    		{
    			Console.WriteLine("{0} has been milked.",name);
    		}
    		public Cow(string newName):base(newName)
    		{
    		}
    	}
    
    }

    programe.cs.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    namespace a
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Console.WriteLine("Create an Array type collection of Animal"+"objects and use it:");
    			Animal[] animalArray=new Animal[2];
    			Cow myCow1=new Cow("Deirdre");
    			animalArray[0]=myCow1;
    			animalArray[1]=new Chicken("ken");
    			foreach(Animal myAnimal in animalArray)
    			{
    				Console.WriteLine("New {0} object added to Array collection,"+"Name={1}",myAnimal.ToString(),myAnimal.Name);
    			}
    			Console.WriteLine("Array collection contains {0} objects.",animalArray.Length);
    			animalArray[0].Feed();
    			((Chicken)animalArray[1]).LayEgg();
    			Console.WriteLine();
    			Console.WriteLine("Create an ArrayList type collection of Animal"+"objects and use it:");
    			ArrayList animalArrayList=new ArrayList();
    			Cow myCow2=new Cow("Hayleg");
    			animalArrayList.Add(myCow2);
    			animalArrayList.Add(new Chicken("Roy"));
    			foreach (Animal myAnimal in animalArrayList)
    			{
    				Console.WriteLine("New {0} object added to ArrayList collection,"+"Name={1}",myAnimal.ToString(),myAnimal.Name);
    			}
    			Console.WriteLine("ArrayList collection contains {0} objects.",animalArrayList.Count);
    			((Animal)animalArrayList[0]).Feed();
    			((Chicken)animalArrayList[1]).LayEgg();
    			Console.WriteLine();
    			Console.WriteLine("Additional manipulation of ArrayList:");
    			animalArrayList.RemoveAt(0);
    			((Animal)animalArrayList[0]).Feed();
    			animalArrayList.AddRange(animalArray);
    			((Chicken)animalArrayList[2]).LayEgg();
    			Console.WriteLine("The animal called {0} is at index {1}.",myCow1.Name,animalArrayList.IndexOf(myCow1));
    			myCow1.Name="Janice";
    			Console.WriteLine("The animal is now called {0}.",((Animal)animalArrayList[1]).Name);
    			Console.ReadKey();
    		}
    	}
    }
  • 相关阅读:
    什么叫继承?
    两类交换元素使序列有序 求最少交换次数的题
    如何理解汉诺塔
    求给出第 K个 N位二进制数,该二进制数不得有相邻的“1”
    7/26 CSU-ACM2018暑期训练3-递归&递推-选讲
    平面分割
    递推算法之平面分割问题总结
    UTC时间
    7/25 CSU-ACM2018暑假集训比赛1
    洛谷 P1824 进击的奶牛【二分答案/类似青蛙过河】
  • 原文地址:https://www.cnblogs.com/bimgoo/p/2474170.html
Copyright © 2011-2022 走看看