一个抽象类应该至少有一个抽象方法吗? [英] Should an abstract class have at least one abstract method?

查看:24
本文介绍了一个抽象类应该至少有一个抽象方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽象类是否必须至少有一个抽象方法?

Is it necessary for an abstract class to have at least one abstract method?

推荐答案

这篇文章的主题和正文问了两个不同的问题:

The subject of this post and the body ask two different questions:

  1. 它应该至少有一个抽象成员吗?
  2. 是否必须至少有一个抽象成员?

#2 的答案肯定是否定的.

The answer to #2 is definitively no.

#1 的答案是主观的,是风格问题.我个人会说是的.如果您的意图是防止类(没有抽象方法)被实例化,最好的处理方法是使用 privateprotected构造函数,而不是通过标记它abstract.

The answer to #1 is subjective and a matter of style. Personally I would say yes. If your intent is to prevent a class (with no abstract methods) from being instantiated, the best way to handle this is with a privateprotected constructor, not by marking it abstract.

这篇关于一个抽象类应该至少有一个抽象方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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