我们可以使用普通类作为基类,而不是抽象类 [英] we can use normal class as base class , instead abstract

查看:118
本文介绍了我们可以使用普通类作为基类,而不是抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用普通类作为基类,而不是抽象类?

如果要覆盖,则可以使用虚拟方法,

那么为什么我们要使用abstact类.

we can use normal class as base class , instead abstract?

if we want to override then we can for the virtual methods ,

then why we go for the abstact classes.

推荐答案

对不起,您的问题不是很清楚.您是否在问抽象类的要点?如果是这样,只需搜索Google,我就会发现它是搜索为什么使用抽象类"的#1链接:
http://codeofdoom .com/wordpress/2009/02/12/learn-this-何时使用抽象类和界面

希望这个回答您的问题,
Ed
Sorry your question isn''t very clear. Are you asking what the point of abstract classes is? If so, just search Google, I found this as #1 link for the search "why use abstract class":
http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface

Hope this answer your question,
Ed


因为您无法创建抽象类的实例,但是它迫使您提供各种方法的实现.

它是类和接口之间的交叉-它定义了派生类必须遵守的协定,但是还可以提供基本功能的默认实现(接口不能实现).

例如,在我的电脑中,我有一个FlexPanel和一个抽象的FlexControl.当用户缩放对象时,FlexControl派生的对象会自动调整大小以适合FlexPanel. FlexPanel包含许多基于FlexControl的控件,这些控件通过提供标题,工具栏和主显示元素来提供通用的外观,并允许派生类将其信息填充到显示中.允许基本的FlexControl没有意义,因为它不知道要显示哪种信息.如果我正在编写一个视频管理应用程序,那么一个FlexControl可能会保存视频,另外一个Actor可能会保存第三流派.
因为抽象类允许我提供工具栏的基本实现和显示,所以我可以处理(例如)抽象类中的拖放操作,因此可以调用派生类的重写方法来实际决定如何处理所放​​置的文件,而无需重新发明轮子,并在每个轮子中实现相同的基本拖放代码.
Because you can''t create an instance of an abstract class, but it forces you to provide implementations of various methods.

It''s sort of a cross between and class and an interface - it defines a contract that the derived class must adhere to, but can also provide a default implementation of basic functions (that an interface can''t).

For example, in my I have a FlexPanel, with an abstract FlexControl. The FlexControl derived objects are autosized to fit within the FlexPanel as the user shrinks and enlarges them. The FlexPanel holds a number of controls that are based on FlexControl which provides a common look and feel by providing the title, toolbar, and main display element, allowing the derived class to fill the display with it''s information. There is no point in allowing a basic FlexControl because it does not know what kind of information to display. If I was writing a video management app then one FlexControl might hold Videos, another Actors and the third Genres for example.

Because an abstract class allows me to provide the basic implementation of the tool bar and display I can handle (for example) drag and drop in the abstract class, calling the derived class overrided method to actually decide what to do with the dropped files - without re-inventing the wheel and implementing the same basic drag and drop code in each.


请参阅OriginalGriff的答案和Ed Nutting的有趣评论,后者对问题进行了有效的解释.您有:为什么要在功能上使类抽象化的同时将其抽象化?

我可以说明一下这件事.

首先,请注意,如果它具有抽象方法,则不能创建非抽象类.这很清楚,因为它可以防止开发人员调用没有实现的方法.在早期的面向对象系统中是可能的.通常会引发异常.

您可以通过使用关键字virtual而不是abstract实际实现所有此类方法来解决此问题.如果这种方法的主体在做有用的事情,那么在某些情况下不应忽略它,这是完全可以接受的,而且通常是有用的做法.否则,这种方法称为伪抽象,并且仍然可以使用,但并不经常使用.

所以问题是,为什么不将所有abstract方法都设为virtual,为它们提供一些实现,有时是空的(即使用伪抽象方法),使所有此类都成为非抽象类,并在抽象类中使用它们呢?可能有用吗?答案是:从功能上讲,这是完全可能的,并且只要您在代码中没有犯错误,就完全等同于在可能的情况下使用抽象类的方法.为什么不这样做呢?因为只要有可能,使用抽象类的方法总是更好.为什么?我上面提到的没有错误"是关键-您不能保证不会犯错,例如在需要派生类的基础类中使用某些基类,或者在需要重写方法的地方显式调用伪抽象方法-编译器将无济于事您可以发现自己的错误.换句话说,基于使用这些抽象类的方法是万无一失.这是引入抽象类的主要原因.
—SA
Please see the answer by OriginalGriff and an interesting comment by Ed Nutting, who posed a valid interpretation of the problem you have: why making a class abstract when functionally it can be non-abstract?

I can illuminate this matter a bit.

First of all, let''s note that you cannot create a non-abstract class if it has abstract methods. This is clear, because it guards a developer from calling a method which does not have implementations. It was possible in early object-oriented systems; typically, an exception was thrown.

You can work-around this by actually implementing all such methods with the keyword virtual instead of abstract. If the body of such method is doing something useful, so there are cases where it should not be overridden, this is quite acceptable and often useful practice. Otherwise, such method is called pseudo-abstract and still can be used, but not so often.

So the question, is, why not making all abstract methods virtual, giving them some implementations, sometimes empty (that is, using pseudo-abstract methods), making all such classes non-abstract and using them where an abstract class could be useful? Here is the answer: functionally, this is quite possible, and, provided you make no mistakes in you code, is fully equivalent to the approach with using abstract classes whenever possible. Why not doing so? Because the approach with using abstract classes whenever possible is always better. Why? "No mistakes" I mentioned above is the key — you cannot guarantee you do not make mistakes like using some base class where the derived class is needed or calling a pseudo-abstract method explicitly where the overridden method is needed — the compiler would not help you to detect your mistake. In other words, the approach based on using those abstract classes is fool-proof. This is the main reason why the abstract classes were introduced.

—SA


这篇关于我们可以使用普通类作为基类,而不是抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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