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

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

问题描述

现在,我正在学习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天全站免登陆