将基类属性共享/扩展到多个派生类 [英] Share/extend base class properties to multiple derived classes

查看:78
本文介绍了将基类属性共享/扩展到多个派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有多个带有单个公共基类的派生类。我想在所有派生类中共享基类属性。此外,我想只设置一次基类属性。这是我的问题,当我在一个派生类上设置基类属性时,那些相同的属性在其他派生类中为null。我应该如何构建我的程序来完成这项任务?

解决方案

这称为继承。总是,绝对总是共享所有派生类的基类的所有成员。你需要先学习OOP的基础知识,然后才能构建你的程序,或者只是简单地写下任何合理的东西。我建议现在就开始。



现在,你的基类成员只是因为你覆盖而为空(小心术语,我没说覆盖 )然后在你的派生类中,由一个或另一个原因,比如说,在你的构造函数,或其他类型的初始化,对继承的构造函数的调用等。如果你只是不触摸这些成员,它们将是相同的和你的基类一样。 (当然,我只是指实例成员;静态成员每个Application Domain只生成一个对象实例。)



-SA

Hi,
I have multiple derived classes with a single common base class. I would like to share the base class properties across all derived classes. Furthermore, I would like to set the base class properties only once. Here's my problem, when I set the base class properties on one derived class, those same properties are null in other derived classes. How should I architect my program to accomplish this task?

解决方案

This is called "inheritance". You always, absolutely always "share" all members of base classes with all derived classes. You need to learn at least the very basics of OOP before you can "architect your program" or just simply write anything sensible. I would advise to start right now.

Now, your members of the base class are null only because you overwrite (be careful with terminology, I did not say "override") then in your derived classes, by one or another reason, say, in your constructors, or other kind of initialization, in the call to inherited constructors, etc. If you simply don't touch these members, they will be the same as in your base classes. (Of course, I only mean instance members; static members make only one object instance per Application Domain.)

—SA


这篇关于将基类属性共享/扩展到多个派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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