抽象类中受保护的抽象或公共抽象方法 [英] Protected abstract or public abstract method in abstract class

查看:156
本文介绍了抽象类中受保护的抽象或公共抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,其中有一些公共方法和一些抽象方法. 我对公众开放,以便他们为派生类实现通用方法.

Hi I have an abstract class in which I have some public methods and some abstract ones. I have the public so that they implement the common methods for the derived classes.

让我感到困惑的是为什么我要定义一个公共抽象方法而不是受保护的抽象.对我来说,在抽象类中定义一个公共抽象方法是没有意义的....因为如果是抽象类,则在派生类中将其覆盖,但是如果将if定义为public,则同样如此,但以某种方式定义它更有意义据我们所知,它将受到保护,因此我们将在派生类中将其覆盖.

What is confusing me is why I will want to define a public abstract method instead of protected abstract. That makes no sense to me to define a public abstract method in abstract class.... because if is an abstract will be overridden, in the derived classes, but the same is if is defined as public but somehow it makes more sense to define it as protected as we know that we will override that in the derived classes.

在抽象类中将方法定义为公共抽象是否错误?哪个更好,为什么?

Is it wrong to define the method as public abstract in an abstract class? Which is better and why?

推荐答案

出于同样的原因,您希望在对象中使用公共方法:) 您只是不知道现阶段的具体实现. 在具有很高抽象水平的类中很常见,例如中间件.

For the same reason you want a public method in an object :) You just don't know the particular implementation at this stage. It is common in classes with very high level of abstraction, middlewares for example.

这是100%合法的.您只需要确保要在每个具体实现中向世界展示它的功能即可.入口点方法(例如:开始,执行,解析..)通常是这种类型.

It is 100% legal. You just need to be sure that it is functionality that you want to expose to the rest of the world in every concrete implementation. Entry point methods (ex: start, execute, parse..) are usually of this kind.

这篇关于抽象类中受保护的抽象或公共抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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