Inside C # .NET abstract classes and interfaces What is the difference

1. oriented programming and object-oriented programming interfaces What is the relationship

First of all, the interface for programming and object-oriented programming is not the same level, it is not more advanced than the object-oriented programming is an independent programming ideas, but attached to the object-oriented ideology, are a part of. Or that it is one of the essence of the idea of ​​object-oriented programming system.

2. The nature of the interface

Interfaces, the upper surface is defined by several methods not subject code composed of an aggregate, have a unique name, category, or other interfaces may be implemented (or may be inherited say). It may be in the form of the following way:

interface InterfaceName
{
    void Method1();
    void Method2(int para1);
    void Method3(string para2,string para3);
}

 

So, what is the nature of the interface is it? Or what is the meaning of existence interfaces Yes. I think it can be considered from the following two perspectives:

1) the interface is a collection of rules, which defines a set of rules implementing the interface must have a class or interface. It reflects the nature "If you are ...... you must be able to ......" concept.

 For example, in nature, people can eat, that is, "if you are a man, you must be able to eat." Then the simulation into a computer program, there should be a IPerson (By convention, the interface name from the "I" at the beginning) interface and has a method called Eat (), and then we provide that each "person" class represents, must be implemented IPerson interfaces, which simulates the natural world. "If you are a man, you must be able to eat," this rule.

From here, I think you can see a little object-oriented stuff. One of the core object-oriented thinking, is to simulate the real world, the real world of things into classes, the entire program by each class instance communicate with each other, cooperate with each other to complete the system function, which is consistent with real-world operating conditions, but also for the essence of the object of thought.

2) the interface is a view similar things in the abstract a certain size. Note that I emphasized in view of a certain size, because this concept of "like with like" is relative, it is because of different views and different particle sizes.

 For example, in my eyes, I was a man, and a pig are essentially different, I can accept me and my classmates are kind to this argument, but I absolutely unacceptable and a pig are similar. However, if in the eyes of a zoologist, I and pigs should be the same, because we are all animals, he can be considered "people" and "pigs" have achieved IAnimal this interface, while he was in the study of animal behavior, not I will treat separately and pigs, but this study will be on the larger particle size from "animal", but he and I would think the essential difference between a tree.

Now for a geneticist, the situation is different, because bio can be genetic, so in his eyes, and I did not distinguish pigs, and a mosquito, a bacterium, a tree, a mushroom or even a SARS virus are no different, because he will think that we have achieved ID escendable this interface (Note:. descend vi genetic), that is what we are heritable, he would not we study separately, but will be the same as all biological research in his eyes and the virus is not divided, material and non-material inherited only heritable. But at least, I am a stone and there are differences.

But unfortunately things happen, one day, there has been a great man on the planet, he called Lenin, he was familiar with Marx, Engels, after dialectical materialism masterpiece, erudite, and he got a well-known Definition: the so-called material consciousness that can be reflected in objective reality. At this point, I and a stone, a trace of air, a cell phone signal transmission idioms and electromagnetic fields has been no difference, because the objective in Lenin's eyes, we are all conscious that can be reflected in reality. If Lenin is a programmer, he would say: so-called matter is that all while achieving "IReflectabe" and "IE instance of the class sse" two interfaces generated. (Note: reflect v esse n reflect the objective reality.)

Perhaps you will feel like in the example above me in vain, however, this is the interface to the meaning of existence. Object-oriented thinking and one core, called polymorphisms, what is polymorphism? I mean at some level of granularity view indiscriminate treatment and unified treatment of similar things. And dare to do so, it is because of the presence of the interface. Geneticists like that, he knew that all creatures are realized ID escendable interfaces, if the bird biology, there must be Descend () this method, so he can unify research, which is not separately studied every creature and eventually exhausted .

Here may not give you a visual impression about the nature and role of the interface. So in the example later and resolve several design patterns, you will experience a more intuitive interface connotation.

3. Summary Oriented Programming Interface

So what is it oriented programming interface? My personal definition is: the system analysis and architecture, to distinguish between levels and dependencies, each level is not to provide services directly to its upper layer (ie not directly instantiated in the upper layer), but by defining a set of interfaces, only up layer expose its interface function, the interface is dependent on the upper layer to the lower layer only, without depending on the specific type.

The benefit of this is obvious, first of all good for system flexibility. When the lower layer needs to be changed, as long as the interfaces and interface functions constant, the upper layer without any changes. Even when the underlying code will not change the entire upper replaced, just as we would a 60G hard drive into a WD of Seagate 160G hard drive, computer elsewhere do not have to make any changes, but to pull down the original hard drive, the new drive plug on the line, because the rest of the computer does not depend particularly hard, but depends only an IDE interface, implements this interface as long as the hard disk, can be replaced up. From here, the program interface and the interface is very similar to reality, so I always thought that the interface (interface) this word is really quite similar!

Another benefit of using the interface is different components or levels of developers can start parallel, like making the hard drive without waiting for making the CPU, and so they do not build the display, as long as a consistent interface, reasonable design, can be developed in parallel, thus Improve efficiency.

 

Supplement to this article:

1. Regarding "oriented Programming Interface" in the "Interface" object-oriented language and in particular the "interface" two words

See friends of "programming to an interface" in the "Interface" word should be larger than the range of interface simple programming language. After I thought, that makes sense. I write here is not too reasonable. I think, object-oriented language in the "Interface" refers to a specific code structure, such as C # using interface keyword defines an interface. The "Oriented Programming Interface" in the "Interface" may be said to be a software architecture from the perspective of a finger from a more abstract level for that class and to hide the underlying concrete structural members to achieve polymorphism. In this sense, if you define an abstract class, and the purpose is to achieve polymorphism, then I think this abstract class, also known as the "interface" is reasonable. But polymorphism reasonable and unreasonable implemented in the abstract class? In the second discussed below.

In summary, I think the two concepts "Interface" both distinguished from each other and interconnected. "Oriented Programming Interface" Interface is an idea used to implement polymorphism level, increase flexibility, and maintainability of software architecture components, and the specific language of the "interface" is this idea of the specific component the implementation of the code means.

2. With regard to abstract classes and interfaces

If we simply look at the specific code, these two concepts are easily blurred, or even think the interface is redundant, since the specific function from a single point of view, in addition to multiple inheritance (C #, Java), the abstract class seems completely replace the interface . But does the presence of the interface is to achieve multiple inheritance? of course not. I think, the difference between abstract classes and interfaces that use motives . Abstract class is used for code reuse, the use of the interface motive was polymorphism . So, if you are in a place that is using an interface or abstract class rather hesitant, you can think about your motivation is.

看到有朋友对IPerson这个接口的质疑,我个人的理解是,IPerson这个接口该不该定义,关键看具体应用中是怎么个情况。如果我们的项目中有Women和Man,都继承Person,而且Women和Man绝大多数方法都相同,只有一个方法DoSomethingInWC()不同(例子比较粗俗,各位见谅),那么当然定义一个AbstractPerson抽象类比较合理,因为它可以把其他所有方法都包含进去,子类只定义DoSomethingInWC(),大大减少了重复代码量。

但是,如果我们程序中的Women和Man两个类基本没有共同代码,而且有一个PersonHandle类需要实例化他们,并且不希望知道他们是男是女,而只需把他们当作人看待,并实现多态,那么定义成接口就有必要了。

总而言之,接口与抽象类的区别主要在于使用的动机,而不在于其本身。而一个东西该定义成抽象类还是接口,要根据具体环境的上下文决定。

再者,我认为接口和抽象类的另一个区别在于,抽象类和它的子类之间应该是一般和特殊的关系,而接口仅仅是它的子类应该实现的一组规则。(当然,有时也可能存在一般与特殊的关系,但我们使用接口的目的不在这里)如,交通工具定义成抽象类,汽车、飞机、轮船定义成子类,是可以接受的,因为汽车、飞机、轮船都是一种特殊的交通工具。再譬如Icomparable接口,它只是说,实现这个接口的类必须要可以进行比较,这是一条规则。如果Car这个类实现了Icomparable,只是说,我们的Car中有一个方法可以对两个Car的实例进行比较,可能是比哪辆车更贵,也可能比哪辆车更大,这都无所谓,但我们不能说“汽车是一种特殊的可以比较”,这在文法上都不通。

 

C#.NET里面抽象类和接口有什么区别?

接口和抽象类的概念不一样。接口是对动作的抽象,抽象类是对根源的抽象。

抽象类表示的是,这个对象是什么。接口表示的是,这个对象能做什么。比如,男人,女人,这两个类(如果是类的话……),他们的抽象类是人。说明,他们都是人。

人可以吃东西,狗也可以吃东西,你可以把“吃东西”定义成一个接口,然后让这些类去实现它.

所以,在高级语言上,一个类只能继承一个类(抽象类)(正如人不可能同时是生物和非生物),但是可以实现多个接口(吃饭接口、走路接口)。

 

下面接着再说说两者在应用上的区别:
     接口更多的是在系统架构设计方法发挥作用,主要用于定义模块之间的通信契约。
   而抽象类在代码实现方面发挥作用,可以实现代码的重用

模板方法设计模式是抽象类的一个典型应用

 

最佳答案: 
1抽象类
    (1) 抽象方法只作声明,而不包含实现,可以看成是没有实现体的虚方法
    (2) 抽象类不能被实例化
    (3) 抽象类可以但不是必须有抽象属性和抽象方法,但是一旦有了抽象方法,就一定要把这个类声明为抽象类
    (4) 具体派生类必须覆盖基类的抽象方法
    (5) 抽象派生类可以覆盖基类的抽象方法,也可以不覆盖。如果不覆盖,则其具体派生类必须覆盖它们。如:

using System;
public abstract class A //抽象类A 

    private int num=0;
    public int Num //抽象类包含属性 
    { 
        get 
        { 
            return num; 
        } 
        set 
        { 
            num = value; 
        }         
    }

    public virtual int getNum() //抽象类包含虚方法 
    { 
        return num; 
    }

    public void setNum(int n) // //抽象类包含普通方法 
    { 
        this.num = n; 
    }

    public abstract void E(); //类A中的抽象方法E     
}

public abstract class B : A //由于类B继承了类A中的抽象方法E,所以类B也变成了抽象类 

    
}
public class C : B 

    public override void E() //重写从类A继承的抽象方法。如果类B自己还定义了抽象方法,也必须重写 
    { 
        //throw new Exception("The method or operation is not implemented."); 
    } 
}

public class Test 

    static void Main() 
    { 
        C c = new C(); 
        c.E(); 
    } 
}


二、接 口
    (1) 接口不能被实例化
    (2) 接口只能包含方法声明
    (3) 接口的成员包括方法、属性、索引器、事件
    (4) 接口中不能包含常量、字段(域)、构造函数、析构函数、静态成员。如:
public delegate void EventHandler(object sender, Event e);
public interface ITest 

    //int x = 0;
    int A 
    { 
        get; 
        set; 
    }
    void Test();
    event EventHandler Event;    
    int this[int index] 
    { 
        get;
        set; 
    } 
}
    (5) 接口中的所有成员默认为public,因此接口中不能有private修饰符
    (6) 派生类必须实现接口的所有成员
    (7) 一个类可以直接实现多个接口,接口之间用逗号隔开
    (8) 一个接口可以有多个父接口,实现该接口的类必须实现所有父接口中的所有成员

三、抽象类和接口
    相同点:
    (1) 都可以被继承
    (2) 都不能被实例化
    (3) 都可以包含方法声明
    (4) 派生类必须实现未实现的方法
    区 别:
    (1) 抽象基类可以定义字段、属性、方法实现。接口只能定义属性、索引器、事件、和方法声明,不能包含字段。
    (2) 抽象类是一个不完整的类,需要进一步细化,而接口是一个行为规范。微软的自定义接口总是后带able字段,证明其是表述一类“我能做。。。”
    (3) 接口可以被多重实现,抽象类只能被单一继承
    (4) 抽象类更多的是定义在一系列紧密相关的类间,而接口大多数是关系疏松但都实现某一功能的类中
    (5) 抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性
    (6) 接口基本上不具备继承的任何具体特点,它仅仅承诺了能够调用的方法    
    (7) 接口可以用于支持回调,而继承并不具备这个特点
    (8) 抽象类实现的具体方法默认为虚的,但实现接口的类中的接口方法却默认为非虚的,当然您也可以声明为虚的 
    (9) 如果抽象类实现接口,则可以把接口中方法映射到抽象类中作为抽象方法而不必实现,而在抽象类的子类中实现接口中方法

Guess you like

Origin www.cnblogs.com/net-sky/p/11017872.html