六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 61|回复: 0

C#拾遗系列(9):继承、接口、扩展方法、分部类、类操作、Ref and Out、可空类型

[复制链接]

升级  77.33%

136

主题

136

主题

136

主题

举人

Rank: 3Rank: 3

积分
432
 楼主| 发表于 2013-1-27 04:40:45 | 显示全部楼层 |阅读模式
本文内容:
       
  • 继承   
  • 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 示例
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表