面向对象设计:何时创建抽象类 [英] Object oriented design: when to make an abstract class

查看:31
本文介绍了面向对象设计:何时创建抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在学习 OOP,主要是在 c# 中.我对制作无法实例化的类的主要原因是什么感兴趣.何时创建抽象类的正确示例是什么?我发现自己过于热情地以继承方式使用抽象类.当类在系统中是抽象的并且类不应该是抽象的时,是否有一些规则?例如,我创建了在某些方面相似的医生和病人类,所以我从抽象类 Person 派生了它们(因为它们都有名字和姓氏).那错了吗?对不起,如果问题很愚蠢,我对此很陌生.

Right now, I am learning OOP, mainly in c#. I am interested in what are the main reasons to make a class that can't be instantiated. What would be the correct example of when to make an abstract class? I found myself using the abstract class in inheritance way too enthusiastically. Are there some rules when class is abstract in system and when class should not be abstract? For instance, I made doctor and patient classes which are similar in some way so I derived them both from abstract class Person (since both have name and surname). Was that wrong? Sorry if the question is stupid, I am very new at this.

推荐答案

这可能是一个非学术性的定义,但抽象类应该代表一个如此抽象"的实体,以至于无法实例化它.

This is probably a non-academic definition, but an abstract class should represent an entity that is so "abstract" that make no sense to instantiate it.

>

它通常用于创建必须由具体类扩展的模板".所以抽象类可以实现通用的特性,例如实现接口的一些方法,委托给具体类实现特定行为.

It is often used to create "templates" that must be extended by concrete classes. So an abstract class can implement common features, for example implementing some methods of an interface, an delegate to concrete classes implementation of specific behaviors.

这篇关于面向对象设计:何时创建抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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