the Differences between abstract class & interface in C#接口和抽象类的区别

C#抽象类与接口的区别
本文详细介绍了C#中抽象类与接口的基本语法及使用规范,并对比了两者之间的主要区别,包括成员修饰符的使用限制、成员实现等方面的不同。

    abstract class and interface in C# look like much, but sometimes they get the similiar use. However, there are still much differences between them. here we go.
    抽象类和接口初看上去很象,但是还是有很多区别的。
   a) Interface Grammar接口语法
   [public|protected|internal|private] interface interfaceName
    {
        public int add(int x,int y);
//this is error! interface memeber can not be described with any characters to declare its access attribute.
//接口中任何成员和属性不能被任何修饰符修饰来表明其访问属性!
        int add(int x,int y);
//this is right!
        int Count{get;} //interface property接口属性
        string this[int index]{get;set;}//index interface 接口索引
    }

    Difference:    
        1. in interface, all interface members can not be decorated with any other access tag,such as public,private.etc.
        2. in abtract class, such case is just on the opposite.
    
    b) abstract class Grammar 抽象类语法
        [public|protected|internal|private] abstct class className
        {
            public abstract int add(int x,int y);
            //it's ok, but that forbidden in interface!
            //在abstract class中,类成员可以有各种修饰符。如果该类成员没有被implement,那么该类一定要被标示为abstact方法。否则出错!当然也可以实现该方法。如下multiply方法所示
            public virtual int multiply(int x,int y)
            {
                return x*y;
            }
        }

        Difference:
        1.in interface, all interface memebers can not be implemented. and it's just an declaration  for other class which will inherented from it and implemented all of them.
        在接口中,所有接口成员都不能被实现,它只是一个申明而已,其他类继承自该接口,并且要实现该接口中的所有成员。
        2 in abstract class, its member can be implemented ,otherwise must be decorated with abstract that is the same with interface . of course, any class inherented from this  must implemented all its abstract method. of course, u can also override the method that has already implemented method.
        在抽象类中,其成员可以被实现,如果不实现的话,其方法成员一定要被标标示为abstract. 同时,任何继承于该抽象类的类必须要实现其所有标示为abstract的方法,这点与interface相同。或者该类也可以override该抽象类中的method. 这一点interface绝对不可能,因为interface中根本没有实现过,何来override?

        

转载于:https://www.cnblogs.com/Winston/archive/2008/04/23/1166876.html

在 Python 中,`typing.List` `list` 都是用来表示列表的数据类型,但它们有一些微妙的区别。 1. **类型提示(Type Hinting)**: `typing.List` 是从 `typing` 模块引入的一个泛型类型,用于声明变量或函数参数将存储的是列表。例如: ```python from typing import List my_list: List[int] = [1, 2, 3] ``` 这里我们明确指出了 `my_list` 将只包含整数。这提高了代码的可读性文档性,虽然运行时并不强制类型检查。 2. **动态类型 vs 静态类型支持**: 如果你使用了静态类型检查器(如 PyCharm、Mypy 等),`List` 会提供类型安全的优势,帮助开发者发现早期的类型错误。 3. **灵活性**: `list` 是 Python 内置的动态类型列表,它可以存储任意类型的元素,包括未指定类型的元素。而 `typing.List` 则要求所有元素必须具有相同的类型。 4. **类型推断**: 在没有显式类型注解的情况下,Python 根据赋值操作自动推断 `list` 类型。而在 `typing` 中,如果想要获得类似的行为,你需要显式地指定类型。 5. **鸭子类型(Duck Typing)**: Python 自带的 `list` 不受类型约束,如果你不介意类型错误,`typing.List` 的严格类型定义可能不如动态类型灵活。 总的来说,`typing.List` 更加严谨,适合大型项目中提高代码质量类型安全;而 `list` 则更为灵活,适合快速原型设计不需要严格类型检查的小规模代码。在实际编程中,两者可以根据具体需求灵活选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值