本文介绍了我在类a中创建了一个列表,现在我想在B类中添加一些内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! class A { static void Main( string [] args) {列表与LT; RootJson> eList = new 列表< RootJson>(); RootJson rootjson = new RootJson(); Libs.B.Get(); } } public class B { public 静态 void 获取() { rootjson.Hub = new 列表< Hub>(); Hub hub = new Hub(); hub.OS = 1.1; rootjson.Hub.Add(rootjson.hub); } } 问题是rootjson属于A类而不是B类。我怎样才能使这个可用? 我想在A类中保留rootjson的创建,因为我也想创建C,D和E类,它们也会向rootjson添加东西。 在这种情况下如何在B类中声明rootjson? 感谢先进的/> 我尝试了什么: i尝试声明rootjson但不是; t找到正确的语法。 播放也与public,static void等解决方案 正如NotPoliticallyCorrect所说,访问A类变量的唯一方法是使用A类的实例,不管从哪里上课。 这就像一辆车:他们都有一个手套箱,但如果你把手机放在手套箱里你不希望在我的手套箱里找到它的车!您需要访问正确的汽车实例才能找回您的手机。 我怀疑您需要考虑类和数据流,因为您的设计听起来有点混乱你所描述的那些。 但是......你可以把A类实例(或者只是它的List)作为参数传递给B.Get,这样它就可以直接访问它。 class A{static void Main(string[] args){List<RootJson> eList = new List<RootJson>();RootJson rootjson = new RootJson();Libs.B.Get();}} public class B { public static void Get(){ rootjson.Hub = new List<Hub>(); Hub hub = new Hub(); hub.OS = "1.1"; rootjson.Hub.Add(rootjson.hub);}}the problem is that rootjson is in Class A and not available in Class B. How can i make this available?I want to keep the creation of rootjson in Class A, because i also want create Class C,D and E, which will also be adding stuff to rootjson.How do i declare rootjson in Class B in this case?Thanks in advancedWhat I have tried:i tried to declare rootjson but doesn;t find the right syntax. "played" also with public, static void etc 解决方案 As NotPoliticallyCorrect says, the only way to access a variable of class A is to use an instance of class A, regardless of where the class is being accessed from.It's like a car: they all have a glove box, but if you put your phone in the glove box of your car you don't expect to find it in the glove box of mine! You need to access the correct instance of a Car in order to retrieve your phone.I suspect that you need to think about the classes and data flows, as your design sounds somewhat muddled from the little you have described.But ... you could just pass the A class instance (or just it's List) as a parameter to B.Get so it can access it directly. 这篇关于我在类a中创建了一个列表,现在我想在B类中添加一些内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 15:03