??????? static public void HowToFeed(this Dog dog) { ??????????? Console.WriteLine("Play a vedio how to feed dog"); ??????? } ?? ? ??? } ??? class Program ??? { ??????? static void Main(string[] args) ??????? {
??????????? //Dog Dog = new Dog(); ??????????? //Dog.Name = "Jack"; ??????????? //Dog.PrintName(); ??????????? //Dog.speak(); ??????????? //Pet Cat = new Cat(); ??????????? //Cat.Name = "Tom"; ??????????? //Cat.PrintName(); ??????????? //Cat.speak(); ??????????? Pet[] pets = new Pet[]{new Dog("Jack"),new Cat("Tom")}; ??????????? for (int i = 0; i < pets.Length;i++ ) { ??????????????? pets[i].speak(); ??????????? ? ??????????? } ??????????? Cat c = new Cat("Tom2"); ??????????? IClimbTree climb = (IClimbTree)c; ??????????? c.catchMice(); ??????????? climb.climbTree(); ??????????? Dog a = new Dog("Jack1"); ??????????? Dog.ShowNum();
??????????? Dog dog = new Dog("Tommy"); ??????????? dog.HowToFeed();
2017-12-30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
??? //public class Pet {
??? //??? public Pet(string name) {
??? //??????? _name = name;
?????? ?
??? //??? }
??? //??? protected string _name;
??? //??? public void PrintName() {
??? //??????? Console.WriteLine("Pet`s name is"+_name); ?
??? //??? }
??? //?? //虛方法
??? //??? virtual public void speak() {
??? //??????? Console.WriteLine(_name+"is speaking");
??? //??? }
??? //}
??? //結(jié)構(gòu)
??? struct fish {
??????? int weight;
??????? int price;
?? ?
??? }
??? //接口
??? interface ICatchMice {
??????? void catchMice();
??? }
??? interface IClimbTree {
??????? void climbTree();
??? }
?? abstract public class Pet
??? {
??????? public Pet(string name)
??????? {
??????????? _name = name;
??????? }
??????? protected string _name;
??????? public void PrintName()
??????? {
??????????? Console.WriteLine("Pet`s name is" + _name);
??????? }
??????? //抽象類,沒有函數(shù)體,無法實(shí)例化
??????? abstract public void speak();
??????? //{
??????????? //Console.WriteLine(_name + "is speaking");
??????? //}
??? }
??? //狗類
??? public class Dog : Pet {
??????? //靜態(tài)
??????? static int num;
??????? static Dog() {
??????????? num = 0;
?????? ?
??????? }
??????? //構(gòu)造函數(shù)
??????? public Dog(string name):base(name) {
??????????? //_name = name;
??????????? ++num;
?????? ?
??????? }
??????? new public void PrintName() {
??????????? Console.WriteLine("我的寵物是"+_name);
??????? }
??????? public override void speak()//重寫
??????? {
??????????? Console.WriteLine(_name + " is speaking"+" wow");
??????? }
??????? static public void ShowNum() {
??????????? Console.WriteLine("有"+num+"條狗");
??????? }
??? }
??? //貓類
??? public class Cat : Pet,ICatchMice,IClimbTree {
??????? //構(gòu)造函數(shù)
??????? public Cat(string name):base(name) {
??????????? //_name = name;
?????? ?
??????? }
??????? new public void PrintName() {
??????????? Console.WriteLine("我的寵物是"+_name);
??????? }
??????? public override void speak()
??????? {
??????????? Console.WriteLine(_name + "is speaking"+" meow");
??????? }
??????? public void catchMice() {
??????????? Console.WriteLine("catchmice");
??????? }
??????? public void climbTree() {
??????????? Console.WriteLine("climbtree");
??????? }
?? ?
??? }
??? //靜態(tài)類,方法擴(kuò)展
??? static class PetGuide{
??????? static public void HowToFeed(this Dog dog) {
??????????? Console.WriteLine("Play a vedio how to feed dog");
??????? }
?? ?
??? }
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? //Dog Dog = new Dog();
??????????? //Dog.Name = "Jack";
??????????? //Dog.PrintName();
??????????? //Dog.speak();
??????????? //Pet Cat = new Cat();
??????????? //Cat.Name = "Tom";
??????????? //Cat.PrintName();
??????????? //Cat.speak();
??????????? Pet[] pets = new Pet[]{new Dog("Jack"),new Cat("Tom")};
??????????? for (int i = 0; i < pets.Length;i++ ) {
??????????????? pets[i].speak();
??????????? ?
??????????? }
??????????? Cat c = new Cat("Tom2");
??????????? IClimbTree climb = (IClimbTree)c;
??????????? c.catchMice();
??????????? climb.climbTree();
??????????? Dog a = new Dog("Jack1");
??????????? Dog.ShowNum();
??????????? Dog dog = new Dog("Tommy");
??????????? dog.HowToFeed();
??????? }
??? }
}