【C# Programming】继承、接口

一、继承

1、派生

        继承在相似而又不同的概念之间建立了类层次概念。 更一般的类称为基类,更具体的类称为派生类。派生类继承了基类的所有性质。

        定义派生类要在类标识符后面添加一个冒号,接着添加基类名。

public class PdaItem
{
    public string Name { get; set; }
    public DateTime LastUpdated { get; set; }
}

// Define the Contact class as inheriting the PdaItem class
public class Contact : PdaItem
{
    public string Address { get; set; }
    public string Phone { get; set; }
}

        除非明确指定了基类,否则所有类都默认从object 派生。

2、基类和派生类的转型

        由于派生建立了“属于”关系,所以总可以将派生类的值赋给基类型的变量。这称为隐式类型转换。  从基类型转换到派生类型,要求执行显式转型。方法是在原始引用名称前,将要转换成的目标类型放到圆括号中。

public static void Main()
{
    // Derived types can be implicitly converted to base types
    Contact contact = new Contact();
    PdaItem item = contact;//隐式类型转换

    // Base types must be cast explicitly to derived types 显示类型转换/强制类型转换
    contact = (Contact)item; 
    // ...
}

3、自定义转换

        类型之间的转换并不限于单一继承链中的类型,不同类型相互之间也能进行转换,关键是两个类型之间提供转型操作符。 C# 允许显式或隐式转型操作符。 例如:

class GPSCoordinates
{
    public static implicit operator UTMCoordinates(GPSCoordinates coordinates)
    { 
        // ...
        return null;//return the new UTMCoordinates object
    }
}
class UTMCoordinates { /*… */}

        如果需要显式转换,可将implict 换成 explict。

4、private 访问修饰符

        派生类继承了除构造器和析构器之外的所有基类成员,到那时私有成员只能在声明它们的那个类型中才能访问,派生类不能访问基类的私有成员。

public class PdaItem
{
    private string _Name;
    // ...
}

public class Contact : PdaItem
{
    // ...
}
public class Program
{
    public static void ChapterMain()
    {
        Contact contact = new Contact();
        // ERROR:  ‘PdaItem. _Name’ is inaccessible due to its protection level

        //contact._Name = "Inigo Montoya";  //uncomment this line and it will not compile
    }
}   

5、protected 访问修饰符

        基类中受保护的成员只能由基类以及派生链中的其他类访问。

public class Program
{
    public static void Main()
    {
        Contact contact = new Contact();
        contact.Name = "Inigo Montoya";
        // ERROR:  ‘PdaItem.ObjectKey’ is inaccessible due to its protection level
        //contact.ObjectKey = Guid.NewGuid(); //uncomment this line and it will not compile
     }
}
public class PdaItem   {  protected Guid ObjectKey { get; set; } }
public class Contact : PdaItem
{
    void Save()
    {
        // Instantiate a FileStream using <ObjectKey>.dat for the filename.
        FileStream stream = System.IO.File.OpenWrite(ObjectKey + ".dat");
    }
    void Load(PdaItem pdaItem)
    {
        // ERROR:  ‘pdaItem.ObjectKey’ is inaccessible due to its protection level
        //pdaItem.ObjectKey =...;
        Contact contact = pdaItem as Contact;
        if(contact != null)
            contact.ObjectKey = new Guid();//...; 
    }
    public string Name { get; set; }
}

6、单继承

  • 继承链中的类理论是无限的。但是C#是单继承语言,意味着一个类不能从两个类中直接派生。
  • 在极少数需要多继承类结构时, 一般的解决方法是使用聚合(aggregation)即一个类包含另一个类的实例。

7、密封类

        密封类要求使用sealed 修饰符,这样做的结果时不能从它们派生其他类。

public sealed class CommandLineParser
{
    // ...
}
// ERROR:  Sealed classes cannot be derived from
public sealed class DerivedCommandLineParser
    //: CommandLineParser //uncomment this line and it will not compile
{
    // ...
}

8、virtual 修饰符

  • C#支持重写(override)实例方法和属性,但不支持重写字段或者任何静态成员,为了重写,在基类
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Cassooo_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值