• 技术文章 >后端开发 >C#.Net教程

    C#学习日记22---多重继承

    黄舟黄舟2017-01-21 15:23:41原创1000
    继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类来定义一个类,一个类从另一个类派生出来时,派生类从基类那里继承特性

    继承的思想实现了 属于(IS-A) 关系。例如,哺乳动物 属于(IS-A) 动物,狗属于(IS-A) 哺乳动物,因此狗 属于(IS-A) 动物。

    基类与派生类:

        C#中派生类从他的直接基类继承成员,方法、属性、域、事件、索引指示器但是除开构造函数与析构函数。

     下面写个实例。 

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
      
    namespace Test  
    {  
        class Anlimal  //定义一个基类  
        {  
            protected int foot = 4;  
            protected double weight = 22.4;  
            protected void say(string type, string call)  
            {  
                Console.WriteLine("类别:{0},叫声:{1} ",type,call);  
            }  
        }  
       
        //Dog 继承Anlimal   
        class Dog:Anlimal  
        {  
            static void Main(string[] args)  
            {  
                Dog dog = new Dog();  
                int foot = dog.foot;  
                double weight = dog.weight;  
                Console.WriteLine("dog foot: {0}\ndog weight:{1}",foot,weight);  
                dog.say("狗", "汪汪");  
            }  
        }  
    }

    结果

    653.png

    php入门到就业线上直播课:进入学习

    多重继承:

    C# 不支持多重继承。但是,您可以使用接口来实现多重继承,上面的例子我们为他添加一个smallanlimal接口

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
      
    namespace Test  
    {  
        class Anlimal  //定义一个基类  
        {  
            protected int foot = 4;  
            protected double weight = 22.4;  
            protected void say(string type, string call)  
            {  
                Console.WriteLine("类别:{0},叫声:{1} ",type,call);  
            }  
        }  
      
        public interface smallanlimal  //添加一个接口 接口只声明方法在子类中实现  
        {  
            protected void hight(double hight);  
             
        }  
        //Dog 继承Anlimal   
        class Dog:Anlimal,smallanlimal  
        {  
            public void hight(double hight)  //实现接口  
            {  
                Console.WriteLine("Hight: {0}",hight);  
            }  
            static void Main(string[] args)  
            {  
                Dog dog = new Dog();  
                int foot = dog.foot;  
                double weight = dog.weight;  
                dog.hight(23.23);  
                Console.WriteLine("dog foot: {0}\ndog weight:{1}",foot,weight);  
                dog.say("狗", "汪汪");  
            }  
        }  
    }

    以上就是 C#学习日记22---多重继承的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐: C#,多重继承
    上一篇:Asp.net禁用页面缓存的方法总结 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• c语言中源文件编译后生成什么文件• C#中GDI+编程10个基本技巧二• 应用绝对路径与相对路径• ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误
    1/1

    PHP中文网