请让我清楚了解抽象类和接口 [英] please make me clear about abstract class and interface

查看:85
本文介绍了请让我清楚了解抽象类和接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我一些真实的简单示例,它可以使我清楚地知道我们使用接口可以执行的操作,而不能使用抽象类实现.

Please give me some real life simple example which can make me clear what exactly we can do using interface and can''t do with abstract class

推荐答案

抽象类
抽象类是无法实例化的一种特殊类.所以问题是为什么我们需要一个无法实例化的类?抽象类仅是子类(继承自).换句话说,它仅允许其他类从中继承而不能实例化.优点是,它为所有子类强制执行某些层次结构.简而言之,它是一种契约,它迫使所有子类都遵循相同的层次结构或标准.
界面
接口不是类.它是由接口一词定义的实体.接口没有实现;它仅具有签名,换句话说,只有方法的定义而没有主体.作为与Abstract类的相似之处之一,它是用于为所有子类定义层次结构的协定,或者它定义了一组特定的方法及其参数.它们之间的主要区别在于,一个类可以实现多个接口,但只能从一个抽象类继承.由于C#不支持多重继承,因此使用接口来实现多重继承.

Abstract Class
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inheritedfrom). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
Interface
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The maindifference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

class Program
    {
        static void Main(string[] args)
        {
            //abstract classes in list<t>
            List<car> cars = new List<car>();
            cars.Add(new Honda());
            cars.Add(new Renult());

            foreach (Car car in cars)
                car.Manufactured();           

            //interfaces in list<t>:
            List<ibike> bikes = new List<ibike>();
            bikes.Add(new Suzuki());
            bikes.Add(new Ducatti());
            foreach (IBike bike in bikes)
                bike.Manufactured();

            //as you can see in my example there is the same implementation of both types
            Console.ReadLine();

        }
    }

    //ABSTRACT:
    public abstract class Car
    {
        public abstract void Manufactured(); //abstract method
    }

    public class Honda : Car
    {
        public override void Manufactured()
        {
            Console.WriteLine("Honda is a Japanese car.");
        }
    }

    public class Renult : Car
    {
        public override void Manufactured()
        {
            Console.WriteLine("Renault is a Franch car.");
        }
    }

    //INTERFACE:
    public interface IBike
    {
        void Manufactured();
    }

    public class Suzuki : IBike
    {
        public void Manufactured()
        {
            Console.WriteLine("Suzuki is prodused on Japan.");
        }
    }

    public class Ducatti : IBike
    {
        public void Manufactured()
        {
            Console.WriteLine("Ducatti is prodused in Italy.");
        }
    }


很多东西.例如,接口可用于多重继承,但不能与类一起使用.这称为多重继承的弱形式":一个类,一个结构或某个接口的基类型列表可以具有多个接口,但是只能有一个基类(在类的情况下).多重继承是使接口在需要此类继承的地方更具吸引力的主要功能.

同样,抽象基类只能用于某些其他类,而不能用于结构.但是结构可以实现接口.这非常重要:这样,结构可以参与多态,这是结构的唯一方法.

这种多态性更强大:首先,它不仅从具体类型中抽象,甚至从这些类型的性质中抽象:如果编译时类型是某些多态集的接口类型,则不必知道这是一个值(结构)还是引用对象(类).此外,同一对象可以参与一个以上的多态集合,每个多态集合具有不同的接口.在需要的情况下,它提供了更大的灵活性.如果可以的话,这使其成为非常通用的设计构建块.

还有什么?接口允许显式实现,即使用对类或结构的引用从用户隐藏"接口方法/属性的实现.隐藏的另一种形式意味着语言的另一种表现力.

请同时参阅我过去的答案:
何时使用抽象以及何时使用接口. ..? [如果抽象类和接口的编号相同,则它们之间的差异方法和var [ ^ ],
如何决定选择Abstract类还是接口 [ ^ ],
接口和多态性 [
Many things. For example, interfaces can be used with multiple inheritance, but not classes. This is called "weak form of multiple inheritance": the base type list of a class, a structure, or some interface can have multiple interfaces, but only one base class (in case of classes). Multiple inheritance is the major feature making interfaces more attractive where such inheritance is desired.

Also, abstract base class can be only for some other class, not a structure. But a structure can implement interfaces. This is very important: this way, a structure can participate in polymorphism, and this is the only way, for a structure.

This kind of polymorphism is more powerful: first, it is abstracted not only from concrete types, but even from the nature of those types: if a compile-time type is the interface type of some polymorphic set, we don''t have to know if this a value (struct) or a reference object (class). Besides, the same object can participate on more than one polymorphic set, each with different interfaces. It provides much more flexibility in cases where it is desired. This makes it a very universal design building block, if you will.

What else? Interfaces allow explicit implementation, which "hides" implementation of interface method/property from the user using the reference to class or structure. Yet another type of hiding means yet another expressive quality of language.

Please see also my past answers:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

It will need some patience and perhaps experimented. First thing to master would be clear understanding run-time types vs compile-time types, which is a root of OOP based on classes, but interfaces as well.

—SA



下载并阅读此
[
Hi
download and read this book[^] I swear ul get what you want and youll find it easly.


这篇关于请让我清楚了解抽象类和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆