继承与抽象之间的确切区别 [英] Exact difference between Inheritance and Abstraction

查看:53
本文介绍了继承与抽象之间的确切区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我继承和抽象之间的确切区别是什么.即在哪种情况下我只必须使用继承,在哪种情况下我只必须使用抽象类.

Can somebody tell me what is the exact difference between Inheritance and Abstraction. i.e. In which scenario I have to use only inheritance and in which scenario i have to use only Abstract classes.

推荐答案



当然,抽象对继承很感兴趣.但是有一些重要的要点.
首先,将类标记为抽象时,无法创建实例.这个非常重要.抽象类过去只能被继承.

但是,当您从非抽象类继承时,可以从派生类和基类两者创建实例.

因此,可以说抽象类用于生成新类型,要被继承而不是创建实例!

当然,抽象对多态性很感兴趣.抽象迫使我们转向多态.

例如,您有一个基本的Employee类,如下所示:
Hi,

Of course Abstraction is interested with Inheritance. But there are some important points.
First of all when you mark a class as abstract you cannot create an instance. This is very important. Abstract classes are used to be inherited only.

But when you inherited from a non abstract class you can create instance from both of derived and base classes.

So we can say abstract classes are used to generate new types,to be inherited not to create instance !

And of course abstraction is interested with Polymorphism. Abstraction forces us to Polymorphism.

For example you have a base Employee class like below:
public class Employee
    {
        public string Name { get; set; }
        public double Salary { get; set; }

        public void IncreaseSalary(double rate)
        {
            this.Salary += (this.Salary * rate / 100);
        }
    }



现在,当我们创建如下所示的SalesRepresentative类时,我们应该从Employee继承它,因为SalesRepresentative是Employee.



Now when we create SalesRepresentative class like below we should inherit it from Employee because SalesRepresentative is an Employee.

public class SalesRepresentative : Employee
    {
        public double AnnualSalesAmount { get; set; }
    }




现在,由于SalesRepresentative对象是从Employee继承的,因此它具有增量方法.但是通常情况下,销售代表和雇员的工资会根据他们的AnnualSalesAmount等以不同的方式提高.

在这种情况下,您应该能够从SalesRepresentative更改IncreaseSalary的方法代码,但是您不能.实际上,您现在遇到了多态性

现在让我们抽象化.如果您想从继承的类中更改默认代码IncreaseSalary,则有两种选择.首先将方法标记为虚拟.第二个将其标记为抽象.

区别在于是否将其标记为虚拟.您不必在SalesRepresentative中实现它,但是如果将其标记为抽象,则必须实现它,并且您不应该忘记抽象成员只能位于抽象类中.检查下面的示例




Now SalesRepresentative object has IncreaseSalary method because it is inherited from Employee. But in generally Sales Representatives'' and Employee''s Salaries are increased by different ways for example according to their AnnualSalesAmount.

In this case you should be able to change method code of IncreaseSalary from SalesRepresentative but you can''t. Actually now you are across with Polymorphism

Now Let''s come to abstraction. If you want to change the default code of IncreaseSalary from inherited class there are 2 choices. First marking the method as Virtual. And the second one is marking it as abstract.

The difference is If you mark it as virtual. You don''t have to implement it in SalesRepresentative but If you mark it as abstract you have to implement it and you should''nt forget an abstract member can only be in abstract classes. Examine the example below

public abstract class Employee
   {
       public string Name { get; set; }
       public double Salary { get; set; }

       public abstract void IncreaseSalary(double rate);

   }





public class SalesRepresentative : Employee
    {
        public double AnnualSalesAmount { get; set; }

        public override void IncreaseSalary(double rate)
        {
            if (this.AnnualSalesAmount > 100000)
            {
                this.Salary += (this.Salary * (rate + 5) / 100);

            }
            else
            {
                this.Salary += (this.Salary * rate / 100);
            }
        }
    }


Abstraction Inheritance 是在Object Oriented Programming中具有广泛含义的重要功能.简短的答案可能不会详尽地涵盖所有功能,并且这些功能会在各种书籍和文章中详细介绍.但是,为了快速了解它们,可以将它们描述如下:

从面向对象编程"的角度来看,Abstraction 正在提取要处理的对象的核心功能,而无需具体说明实现细节.例如,如果我们乘汽车,那么它可以像具有Wheels, Seats, Gears的对象一样抽象,该对象可以被视为描述其组成的Vehicle 类的成员.要为车辆增加行为,可以使用GiveHorn, Drive, ChangeGear, ApplyBrake等方法.

然后,可以使用Inheritance从此基类派生特定的类.说a Car, a Bus.因此,a Car是具有4个车轮,4个齿轮和说4个座位的车辆. Drive, Change Gear, Apply brake可以在此派生类中重新实现,也可以按基类提供的方式使用,也可以根据需要组合使用.同样,使用继承性从Vehicle类派生出一辆公共汽车,它有6个轮子,20个座位,5个齿轮.可以按照上述方法实施这些方法.

在这方面,以下文章可能会有所帮助.
抽象(计算机科学) [继承(面向对象的编程) [抽象类与接口 [抽象类Vs接口 [接口的目的 [ ^ ]
Abstraction and Inheritance are the important features having wide implications in Object Oriented Programming. The full features may not be covered exhaustively in a short answer, and these are dealt elaborately in various books and articles. However, to get a quick idea they can described as below:

Abstraction from Object Oriented Programming perspective is extracting the core features of an object to deal without being specific about the implementation details. For example if we take a vehicle then it can be abstracted like a an object having Wheels, Seats, Gears among other things, which can be treated as the members of a Vehicle class describing its composition. To add behaviour to the Vehicle there can be methods like GiveHorn, Drive, ChangeGear, ApplyBrake etc.

Then specific classes can be derived from this base class using Inheritance. Say a Car, a Bus. So a Car is a vehicle with 4 wheels, 4 gears and say 4 seats. The Drive, Change Gear, Apply brake can be implemented within this derived class either afresh or they can used as provided in the base class or with a combination as per the requirement. Similarly a Bus is derived from the Vehicle class using inheritance and it has 6 wheels, 20 seats, 5 gears. The methods can be implemented as explained above.

In this connection the following articles may be helpful.
Abstraction (computer science)[^]
Inheritance (object-oriented programming)[^]

The abstraction and inheritance can be implemented either with Abstract classes or Interfaces and the following references may be helpful in this regard.

Abstract Class versus Interface[^]
Abstract Class Vs Interface[^]

The answers given to this question may also be helpful.
Purpose of Interfaces[^]


阅读本文:

面向对象编程概念(OOP)以及更多内容 [ ^ ]

希望对您有所帮助:)
Read this article:

Introduction to Object Oriented Programming Concepts (OOP) and More[^]

hope it helps :)


这篇关于继承与抽象之间的确切区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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