如何从另一个类获取基类或子类 [英] How do I get the base or sub classes from another class

查看:71
本文介绍了如何从另一个类获取基类或子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// hi:] - 我对这个世界不熟悉

//请帮助

//这里是例子

//

// hi :] - im new to this world
// help please
//here is the exemple
//

class employee
{
public string name;
public int age;
}
//the class group  of emps
class group
{
public string name;
public employee[] emps;
}
employee em1 = new employee();
em1.name = "emp1";
em.age = 20;
employee em2 = new employee();
em2.name = "emp2";
em.age = 25;
group gp1 = new group();
gp.name = "gr1";
gp.emps = {em1,em2};



//

//

/ /我如何获得em1或em2的(组)?

//和组的名称(字符串名称)?

// thanqus nd有phun: ]



我的尝试:



//无法使用基类或子类

//方法


//
//
//how do i get the (group) of em1 or em2 ?
// and the name of the group (string name)?
// thanqus nd have phun :]

What I have tried:

//cant use baseclass or subclass
//methods

推荐答案

基本上,你没有 - 引用不知道引用它的是什么,它会破坏OOP规则(您的员工甚至不需要知道组存在,更不用说它们可能包含员工对象的集合,或者可能存在多个组件)。



如果您想这样做 - 我真的不建议 - 向您的员工类添加一个组参考,并在将该员工添加到该组时进行设置。

但是这些循环引用会使事情复杂化,并限制垃圾收集器执行它的工作。



想想你想要实现的目标 - 可能有一个更好的解决方案,根本不需要这种类型的kludge。
Basically, you don't - a reference doesn't "know" what is refering to it, it would break the OOPs rules (your employee shouldn't even need to know that groups exist, much less that they might contain a collection of employee objects or that there might be more than one of them).

If you want to do that - and I really don't advise it - add a group reference to you employee class and set it when you add the employee to the group.
But those kind of circular references can complicate things, as well as limiting the Garbage Collectors ability to do it's job.

Think about what you are trying to achieve - there may be a better solution that doesn't need this type of kludge at all.


这篇关于如何从另一个类获取基类或子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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