简体   繁体   English

从外部类获取对实例变量的访问权限

[英]Gaining access to instance variables from outside classes

I am fairly new to programming and I have a question about class access. 我对编程很新,我对类访问有疑问。 Say that I create a class called TheClass with a bunch of public variables and public methods. 假设我使用一堆公共变量和公共方法创建了一个名为TheClass的类。 I then create several other top-level classes that need access to the methods/variables of an instance of TheClass. 然后我创建了几个需要访问TheClass实例的方法/变量的其他顶级类。 So, when I create an instance of TheClass and I want to access the variables of that instance by other top-level classes, then it is my understanding that I can: 所以,当我创建一个TheClass实例并且我想通过其他顶级类访问该实例的变量时,我理解我可以:

Pass the new instance of TheClass into the constructor of each of the other classes when they are created so they can access those variables inside of the instance of TheClass like this: 在创建它们时,将新的TheClass实例传递给每个其他类的构造函数,以便它们可以像这样访问TheClass实例中的那些变量:

TheClass theClass = new TheClass(); 

OtherClass otherClass = new OtherClass(theClass);

Alternatively, I could nest all of the other classes inside of TheClass thereby giving them access to the variables of theClass instance... I think? 或者,我可以将所有其他类嵌套在TheClass中,从而让他们可以访问theClass实例的变量......我想?

Finally, I could make variables/methods of TheClass static thereby giving other classes outside of TheClass access to them, however this is no good because I need access to the variables of the instance of TheClass that is running. 最后,我可以使TheClass的变量/方法变为静态,从而让其他类在TheClass之外访问它们,但这并不好,因为我需要访问正在运行的TheClass实例的变量。

The reason I'm asking is because I recently created my first big program and I was passing an instance of a class object into a large number of other classes and It became quite a chore just following it around. 我问的原因是因为我最近创建了我的第一个大程序,并且我正在将一个类对象的实例传递给大量其他类,并且它只是跟着它变得非常繁琐。 I felt like I was doing something wrong. 我觉得自己做错了什么。 Have I overlooked something huge with regards to accessing instance variables of a class from other top-level classes? 我是否忽略了从其他顶级类访问类的实例变量方面的巨大内容?

I am a long ways away from understanding even the basics of Java programming but this is a question that keeps surfacing for me so I thought I would ask it here. 我距离理解Java编程的基础知识还有很长的路要走,但这是一个不断浮出水面的问题所以我想我会在这里问一下。

This is a common problem, and one of the reasons that things like the singleton pattern , dependency injection and application context (which is just a variant of singleton) are so popular. 这是一个常见问题,其中一个原因就是singleton patterndependency injectionapplication context (它只是单例的变体)之类的东西如此受欢迎。

They all allow you to minimize the passing around of objects all the time. 它们都允许您始终最小化对象的传递。

The first step is architecture. 第一步是架构。 If you have your inheritance and composition right it reduces this - although it still exists. 如果你的继承和组合正确,它会减少这一点 - 尽管它仍然存在。

The next option always used to be Singletons. 下一个选项始终是单身人士。 (Application Context is just using one Singleton to then point to other resources from that Singleton). (应用程序上下文只使用一个Singleton然后指向该Singleton中的其他资源)。 More recently Dependency Injection is by far the preferred route as it also makes things like unit testing a lot easier. 最近,依赖注入是最受欢迎的路径,因为它也使单元测试变得更容易。

http://en.wikipedia.org/wiki/Dependency_injection http://en.wikipedia.org/wiki/Dependency_injection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM