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

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

问题描述

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

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.

令我困惑的是为什么我要定义一个公共抽象方法而不是受保护的抽象方法.这对我来说在抽象类中定义一个公共抽象方法是没有意义的......因为如果是一个抽象将被覆盖,在派生类中,但同样是如果被定义为公共但不知何故定义更有意义正如我们所知,我们将在派生类中覆盖它.

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% 合法的.您只需要确保它是您希望在每个具体实现中向世界其他地方公开的功能.入口点方法(例如:start、execute、parse..)通常属于这种类型.

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天全站免登陆