C#拾遗系列(9):继承、接口、扩展方法、分部类、类操作、Ref and Out、可空类型
本文内容:[*]继承
[*]Equal示例
[*]结构和类
[*]属性
[*]Ref and Out
[*]类操作
[*]扩展方法
[*]接口
[*]可空类型
[*]分部类
<strong />
1. 继承
<div style="font-size: 10pt; color: gray; font-family: consolas;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetTest
{
public class Animal
{
public virtual void Fly()
{
Console.Out.WriteLine("Animal Fly");
Console.Out.WriteLine("----------------------------");
}
}
public class Dog : Animal
{
public new virtual void Fly()
{
base.Fly();
Console.Out.WriteLine("Dog can't Fly");
Console.Out.WriteLine("----------------------------");
}
//也可以,但子类不能再覆盖了
//public new void Fly()
//{
// base.Fly();
// Console.Out.WriteLine("Dog can't Fly");
// Console.Out.WriteLine("----------------------------");
//}
}
public class Cat : Animal
{
public override void Fly()
{
base.Fly();
Console.Out.WriteLine("Cat can't fly");
Console.Out.WriteLine("----------------------------");
}
public override string ToString()
{
return "I am a happy cat,wow";
}
}
public class InheritTest
{
public void Test()
{
/*
new 和 overide的区别是,当把子类付给父类变量时,new 将调用父类的方法,而override将调用子类的方法
*/
Animal dog = new Dog();
dog.Fly();
/* 输出
Animal Fly
*/
Animal cat = new Cat();
cat.Fly();
/* 输出
Animal fly
Cat can't Fly
*/
Console.Out.WriteLine(cat.ToString());
}
}
}
2. Equal 示例
页:
[1]