C# 面向对象 接口(interface)和虚方法(virtual)

 C# 是面向对象的编程语言,对象就是面向对象程序设计的核心。所谓对象就是真实世界中的实体,对象与实体是一一对应的,也就是说现实世界中每一个实体都是一个对象,它是一种具体的概念。本文主要介绍C# 面向对象 接口(interface)和虚方法(virtual)。

1、C# 接口(interface)
在C#中实现抽象的另一种方法是使用接口(interface)。接口(interface)是抽象方法和常量值的定义的集合。从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现。有时必须从几个类中派生出一个子类,继承它们所有的属性和方法。但是,C#不支持多重继承。有了接口,就可以得到多重继承的效果。下面看一下接口使用示例。
 

   // interface
interface Animal {
  void Eat(); // 接口方法(没有实现)
  void Run(); // 接口方法(没有实现)
}

要访问接口方法,必须实现接口 (有点像继承的),也是使用:。例如

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cjavapy
{
    // Interface
    interface Animal
    {
        void Eat();   // 接口方法(没有实现)
        void Sleep(); // 接口方法(没有实现)
    }
    // Cat 实现 Animal 接口
    class Cat : Animal
    {
        public void Eat()
        {
            // 这里提供了eat()的实现
            Console.WriteLine("吃鱼");
        }
        public void Sleep()
        {
            // 这里提供了sleep()的实现
            Console.WriteLine("Zzz");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cat myCat = new Cat();  // 创建Cat对象
            myCat.Eat();
            myCat.Sleep();
        }
    }
}

2、实现多个接口

要实现多个接口,需要用逗号分隔它们:

例如,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cjavapy
{
    interface FirstInterface
    {
        void MyMethod(); // 接口方法
    }
    interface SecondInterface
    {
        void MyOtherMethod(); // 接口方法
    }
    class DemoClass : FirstInterface, SecondInterface
    {
        public void MyMethod()
        {
            Console.WriteLine("myMethod");
        }
        public void MyOtherMethod()
        {
            Console.WriteLine("myOtherMethod");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DemoClass myObj = new DemoClass();
            myObj.MyMethod();
            myObj.MyOtherMethod();
        }
    }
}

3、虚方法

virtual 关键字用于修饰方法或属性的声明,方法或属性被称作虚拟成员。虚拟成员的实现可由派生类中的重写实现。相对于接口,虚方法可以不被重写,定义方法时能够有自己的方法体。虚方法默认是有实现的。例用示例如下,

using System;
namespace VirtualMethodTest
{
    class BaseAnimal
    {
        public virtual void Func() // 注意virtual,表明这是一个虚方法
        {
            Console.WriteLine("I am baseAnimal");
        }
    }
    class Animal: BaseAnimal 
    {
        public override void Func() // 注意override ,表明重新实现了虚函数
        {
            Console.WriteLine("I am Animal");
        }
    }
    class Cat : Animal
    {
    }
    class Dog : BaseAnimal 
    {
        public new void Func() // 注意new ,表明覆盖父类里的同名方法,而不是重新实现
        {
            Console.WriteLine("I am Dog");
        }
    }
    class program
    {
        static void Main()
        {
            BaseAnimal a;         
            BaseAnimal b;       
            BaseAnimal c;  
            BaseAnimal d; 
            a = new BaseAnimal(); 
            b = new Animal();
            c = new Cat(); 
            d = new Dog(); 
            a.Func();    
            b.Func();    
            c.Func(); 
            d.Func();    
            Dog d1 = new Dog();
            d1.Func(); 
            Console.ReadLine();
        }
    }
}

4、虚方法与接口的区别
虚方法与抽象方法都能被重写,抽象方法只能存在于抽象类中,虚方法既可以存在于抽象类中,也可以存在非抽象类中。继承虚方法的类可以重写虚方法也可以选择不重写虚方法,但是抽象方法必须要重写,不然编译会报错。抽象方法是虚方法的一种特殊情况,他的特点在于定了一个规则,他的子类必须遵循这一规则。抽象类运用场景一般在于一个方法是流程里面必要的方法,但是这个方法根据具体情况有多重不一样的实现方法,此时就可以用抽象方法。

5、抽象类与接口的区别
抽象方法与接口都是定义了一种规则,继承他们的类都必须遵循这一规则。抽象类也是类,类所有的特性他都有,接口则不然。抽象类里面可以有非抽象成员,但是接口里面只能有抽象成员,所有成员默认是abstract,类实现继承的时候最多只能继承一个类(抽象类、非抽象类)但是可以继承多个接口。

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熬夜写代码的平头哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值