本文介绍了如何添加a)添加一个静态方法persondata,它不带任何参数并返回一个人数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 到目前为止,这就是我所拥有的, public static int PersonData() { 人p1 =新人(); 人p2 =新人(); } 我尝试了什么: 这是问题, 添加一个静态方法PersonData,它不接受任何参数并返回Person数组。在Person数组中创建两个人对象p1和p2。一个是关于你自己的,另一个是关于任何人的。 我不知道PersonData是int还是字符串。我糊涂了。你能帮忙吗?So far this is what I have,public static int PersonData(){ Person p1 = new Person(); Person p2 = new Person();}What I have tried:This is the problem, add a static method, PersonData, which does not take any parameters and returns a Person array. Create two person objects, p1 and p2 in your Person array. one is about yourself and other is about any person.I don't know if PersonData is an int or string. I'm confused. Can you help?推荐答案 如果你想返回一个Person数组,那么该方法的返回类型应该是一个数组。 If you want to return an array of Person, then return type of the method should be an array.public static Person[] PersonData(){ Person p1 = new Person(); Person p2 = new Person(); Person[] personArray = new Person[2]; personArray[0] = p1; personArray[1] = p2; return personArray;} 但我建议您使用List而不是Array。阅读关于列表以了解它相对于阵列的优势 But I would suggest you use List instead of Array. Read about List to learn the its advantages over an arraypublic static List<person> PersonData(){ Person p1 = new Person(); Person p2 = new Person(); List<person> personList = new List<person>(); personList.Add(p1); personList.Add(p2); return personList;} 它不是int而不是string。很清楚地写道,这是一个功能。这个问题根本没有任何意义。 这个方法的实现完全没有意义。 你创建了两个实例人然后你松开它们,因为它们是局部变量。调用后删除帧堆栈,包含所有变量。实际创建的对象变得无法访问,最终将被GC删除。 这是你必须要理解的:垃圾收集(计算机科学) - 维基百科,免费的百科全书 [ ^ ]。 做什么不是一个正确的问题。这取决于你想要达到的目标。我建议,在给定上下文中,创建一个方法(静态或非静态)创建同一个类的两个实例的整个想法根本没有任何实际意义。让我们来看看。首先,这是如何以最简单的方式将这些返回到对象: It is not int and not string. It's clearly written that this is a function. The question does not make any sense at all.And the implementation of the method makes no sense at all.You create two instances of Person. Then you loose them, because they are local variables. The frame stack is removed after the call, with all your variables. The actually created objects become inaccessible and will be eventually removed by GC.This is something you have to understand: Garbage collection (computer science) — Wikipedia, the free encyclopedia[^]."What to do" would not be a correct question. It depends on what you want to achieve. I would suggest that, in the give context, the whole idea to create a method (static or not) creating two instances of the same class makes no practical sense at all. Let's see. First of all this is how you can return these to objects in the simplest possible way:public static Person[] PersonData() { return new Person[] { new Person(), new Person(), };}另一种选择是:元组(T1,T2)类(系统) [ ^ ] 但是为什么?为什么?! 让我们考虑使用这种方法的调用代码。它能做什么? Another option would be: Tuple(T1, T2) Class (System)[^]But why? Why?!Let's consider the calling code using this method. What can it do?Person[] people = PersonData();people[0].DoSomething();people[1].DoSomething();// you many need to check up if the array has members at 0 and 1.// by why?!// in the same content, won't the code below be simpler? Look:(new Person()).DoSomething(); //? 这是交易:这一切都没有意义,因为方法 PersonData 不会创建任何值,也不会引入任何知识。这是主要的事情。避免使用这样的方法。 (当然,在这样的函数中创建一个 List 是没有意义的。如果你有几个不同的调用具有相同的列表,列表是好的,但是列表实例应该作为参数透明地传递给方法。解决方案1没有比你的问题更有意义了。) 参见: 恶作剧者(计算机编程) - 维基百科,免费的百科全书 [ ^ ], https://en.wikipedia.org/wiki/Cargo_cult_programming [ ^ ], https://en.wikipedia.org/w iki / Spaghetti_code#Lasagna_code [ ^ ]。 -SA Here is the deal: it all makes no sense, because the method PersonData does not create any value, does not introduce any knowledge. This is the main thing. Avoid such methods.(And of course it makes no sense to create a List in such function. A list is good if you have several different calls with the same list, but then the list instance should be transparently passed to method as a parameter. Solution 1 makes not much more sense than your question.)See also:Poltergeist (computer programming) — Wikipedia, the free encyclopedia[^],https://en.wikipedia.org/wiki/Cargo_cult_programming[^],https://en.wikipedia.org/wiki/Spaghetti_code#Lasagna_code[^].—SA 我应该发布完整的问题。这是。 1.创建一个类库,YourLastNameLibrary。 2.添加以下内容您的班级图书课程。 a)231-233页上的姓名,地址和人物。 b)帐户,支票和储蓄 3.创建一个控制台应用程序YourLastName_Assign6。添加对类库的引用,并在using部分添加using yourLibrary;。 a)添加一个静态方法PersonData,它不带任何参数和返回一个Person数组。在Person数组中创建两个人对象p1和p2。一个是关于你自己的,另一个关于任何人。 b)添加一个静态方法AccountData,它不接受任何参数并返回一个Account数组。为PersonData返回的每个人物对象创建一个支票帐户和储蓄帐户。 c)在Main中,调用AccountData方法,输出帐户类型及其持有人使用AccountData返回的每个帐户的当前余额。I should've posted the full question. Here it is. 1. Create a class library, YourLastNameLibrary. 2. Add the following classes to your class library. a) Name, Address, and Person on Pages 231—233. b) Account, Checking, and Savings 3. Create a console application, YourLastName_Assign6. Add a reference to your class library and add "using yourLibrary;" at the using section. a) add a static method, PersonData, which does not take any parameters and returns a Person array. Create two person objects, p1 and p2 in your Person array. one is about yourself and other is about any person. b) add a static method, AccountData, which does not take any parameters and returns an Account array. Create a Checking account and Savings account for each of your person objects returned by PersonData. c) In Main, call the AccountData method, and output the account type and its holder with the current balance for each account returned by AccountData. 这篇关于如何添加a)添加一个静态方法persondata,它不带任何参数并返回一个人数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 16:17