C#语言----继承(学习总结)

来源:互联网 发布:中超后卫数据 编辑:程序博客网 时间:2024/05/24 06:07
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace basicCsharp
{
    //继承分为两种 一种实现继承,一种接口继承
    //实现继承:表示一个类派生于另外一个基类型
    //接口继承:表示一个类只继承了函数的签名,没有继承任何实现代码
    //C#语言支持多继承
    //结构不支持类继承,但是支持接口继承!!!! 切记
    //public class MyDerivedClass:MyBaseClass,Interface1,Interface2{....};
    //public  struct MyDerivedStruct:Interface1,Interface2{....};
    //任何类都有一个默认的继承类,那就是Object.

    class TestInherit
    {}
    
     /*把一个基类函数声明为Virtual,该函数就可以在派生类中重写,写在基类中*/
    class MyBaseClass
    {
        public virtual string VirtualMethod()
        {
            return "This method is virtual and defined in MyBaseClass";
        }
    }
    /*在派生类中重写方法时,需要在方法前+override*/
    class MyDerivedClass : MyBaseClass
    {
        public override string VirtualMethod()
        {
            return "This method is an override  defined in MyDerivedClass";//若是return base.VirthalMethod() 返回基类此方法的返回值
        }
    }

    /*抽象类和抽象函数的声明为abstract,抽象类不能被实例化
      如果类中包括抽象函数,那么此类也必须声明为抽象的*/
    abstract class Building
    {
        public abstract decimal CalculateHeatingCost();//decimal 返回一个十进制小数类型

    }

    /*C#允许把类和方法声明为sealed,对于类来说,这表示不能继承该类;对于方法来说,该方法不能被重写
     用于密封类和密封方法,不允许访问或者重写*/
    sealed class FinalClass
    {      
    }

    /*如果派生类中没有重写或者自己定义构造函数,那么他们是以基类的无参构造函数*/


    //publi,private
    //protected:只能在派生类中调用该方法
    //internal:只能在包含他的程序集中调用该方法
    //protected internal:只能在包含他的程序集中、且在派生类中调用该方法
    //---------------------------------------------------------------------------
    /*接口只能包含方法、属性、索引器和事件的声明
      接口不能有构造函数*/
    public interface IBankAccount
    {
        void PayIn(decimal amount);//映射3个方法
        bool Withdraw(decimal amount);
        decimal Balance
        {
            get;
        }
    }
    public class SaveAccount : IBankAccount
    {
        private decimal balance;//私有的余额属性
        public void PayIn(decimal amount)
        {
            balance = balance + amount;
        }

        public bool Withdraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance = balance - amount;
                return true;
            }
            else
            {
                Console.WriteLine("Withdrawal attempt failed");
                return false;
            }
        }
        public decimal Banlance
        {
            get
            {
                return balance;
            }
        }
        public override string ToString()
        {
            return String.Format("Venus Bank Saver:Banlance={0,6:c}", balance);
        }

    }
    /*接口还可以继承接口,生成派生接口,例如不同银行间的转账业务:*/
    // 首先给个接口继承 用于转账
    public interface ITransferBankAccount : IBankAccount
    {
        bool TransferTo(IBankAccount destination, decimal amount);
    }

    //另外一家银行拥有银行间转账业务
    public class CurrentAccount : ITransferBankAccount//不仅继承ITransferBankAccount的接口方法,还拥有IBankAccount接口方法
    {
        private decimal balance;//私有的余额属性
        public void PayIn(decimal amount)
        {
            balance = balance + amount;
        }

        public bool Withdraw(decimal amount)
        {
            if (balance >= amount)
            {
                balance = balance - amount;
                return true;
            }
            else
            {
                Console.WriteLine("Withdrawal attempt failed");
                return false;
            }
        }
        public decimal Banlance
        {
            get
            {
                return balance;
            }
        }

        public bool TransferTo(IBankAccount destination,decimal amount)
        {
            ;
            if (Withdraw(amount))
            {
                destination.PayIn(amount);
                return true;
            }else return false;
        }
        public override string ToString()
        {
            return String.Format("Jupiter Bank Saver:Banlance={0,6:c}", balance);
        }

    }          

   
}

原创粉丝点击