如何将类的几个函数只设置为类集合 [英] How to make few functions of class avialable to only set of classes

查看:70
本文介绍了如何将类的几个函数只设置为类集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有一个主要的课程,其中包含函数funX()和funY()。我还有另外两个类SubA和SubB。

 I have a class Main with functions funX() and funY().and i have two other classes SubA and SubB.

我想让funX仅适用于SubA,而funY()仅适用于SubB

i want to make funX is only available to SubA and funY() is only avialble to SubB

如何实现这一目标。

推荐答案

在SubA中定义funX并在SubB中定义funY。

Define funX in SubA and funY in SubB.

您无法创建基类方法然后指示哪个派生方法类可以访问基类的特定成员。 那是推车在马前;当你定义一个基类时,它假设派生类
还不存在(并且假设一个infinte数量的类可以从你的基础继承)。 那么,你如何在基类中声明funX只能被称为SubA的(假设的)子类调用?

You can't create base class methods and then dictate which derived classes have access to specific members of the base class.  That's putting the cart before the horse; when you define a base class, it's under the assumption that the derived classes don't exist yet (and also under the assumption that an infinte number of classes can inherit from your base).  Thus, how would you declare in a base class that funX can only be called by a (hypothetical) subclass called SubA?

一种可能的解决方案是在基类中创建一个名为的成员功能并在SubA和SubB中覆盖它并以不同方式实现它们。

A possible solution is to create a single member in the base class called Function and override it in SubA and SubB and implement them differently.

Evan


这篇关于如何将类的几个函数只设置为类集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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