zoukankan      html  css  js  c++  java
  • C#继承与重载

    继承

    继承的特点:

     a.派生类是对基类的扩展,派生类可以添加新的成员,但不能移除已经继承的成员的定义。

    b.继承是可以传递的。如果C从B中派生,B又从A中派生,那么C不仅继承了B中声明的成员,同样也继承了A中声明的成员。

    c.构造函数和析构函数不能被继承.

    d.派生类如果定义了与继承而来的成员同名的新成员,那么就可以覆盖已继承的成员,但这兵不是删除了这些成员,只是不能再访问这些成员。

    e.类可以定义虚方法、虚属性及虚索引指示器,它的派生类能够重载这些成员,从而使类可以展示出多态性。

    f.派生类只能从一个类中继承,可以通过接口来实现多重继承。

    代码实现:

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Father f = new Father();
                Childern c = new Childern();
                grandChildren g = new grandChildren();
                //f.Method();
                c.fatherMethod();
                c.Name = 1;
                g.Name = 2;
                Console.ReadKey();
            }
    
        }
        public class Father
        {
    
            public int Name { get; set; }
            public void fatherMethod()
            {
                Console.WriteLine("父类的方法");
            }
        }
        public class Childern : Father
        {
            public new void childerMethod()
            {
                Console.WriteLine("子类的方法");
            }
        }
        public class grandChildren : Childern
        {
            public void grandMethod()
            {
                Console.WriteLine("孙子类的方法");
            }
        }
    }

    抽象类继承:

     class 继承
        {
            static void Main(string[] args)
            {
                多态 d =new One();
                d.Output();
                Console.ReadKey();
            }
    
        }
     public abstract class 多态
        {
            public abstract void Output();
        }
    
        public class One : 多态
        {
            public override void Output()
            {
                Console.WriteLine("这是重写后的");
            }
        }

    多态(重写,重载):

    重写:往往是对于子类与父类而言.子类重写父类的方法

    重写的方法:

    a.重写父类的方法要用到override关键字(具有override关键字修饰的方法是对父类中同名方法的新实现)

    b.要重写父类的方法,前提是父类中该要被重写的方法必须声明为virtual或者是abstract类型

    c.要被重写的方法添加virtual关键字表示可以在子类中重写它的实现。(注意:C#中的方法默认并不是virtual类型的,因此要添加virtual关键字才能够被重写)

    d.virtual关键字用于将方法定义为支持多态,有virtual关键字修饰的方法称为“虚拟方法”

    代码实现:

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Father f = new Father();
                Childer c = new Childer();
                //f.Method();
                c.fatherMethod();
                Console.ReadKey();
            }
    
        }
        public class Father
        {
            public virtual void fatherMethod()
            {
                Console.WriteLine("父类的方法");
            }
        }
        public class Childer : Father
        {
            public override void fatherMethod()
            {
                Console.WriteLine("重写后.子类的方法");
            }
        }
    
       
    }

    重载:在同一个类中,方法名相同,参数列表不同.

    public class Reload
        {
            public void Get()
            {
                Console.WriteLine("无");
            }
            public void Get(string name)
            {
                Console.WriteLine(name);
            }
        }
  • 相关阅读:
    通用Struts2 配置文件Filterwebxml
    spring+hibernate+struts2应用mysql数据库乱码问题
    区域医疗卫生平台建设(五) HL7 RIM
    在昆山的日子终于要结束了
    医疗基本知识之医嘱篇(三)医嘱中药物、药品、处方的处理
    区域卫生信息平台建设(一)政策
    区域卫生信息平台建设(四)概念模型与逻辑模型
    区域卫生信息平台建设(二)基本功能
    区域卫生信息平台建设(三)数据模型基本概念
    平板电脑选择
  • 原文地址:https://www.cnblogs.com/lbjlbj/p/10554880.html
Copyright © 2011-2022 走看看