methods

return void in a void method?

和自甴很熟 提交于 2021-02-08 10:17:18
问题 I know I can do this: void someMethod(){ return; } but I get a syntax error on void someMethod(){ return void; } Why is the latter not allowed? It makes more sense to me. Edit: I know what a void method is, and that I don't have to return from it at all(and probably shouldn't, in most cases) but I don't understand why I can't return void from a void method. In my opinion, there should be no keyword in the method declaration (like constructors) if the you are able to write return;. 回答1: I

Java program to calculate the combinations function

南楼画角 提交于 2021-02-08 09:12:04
问题 For my programming class I have to make a program that calculates the combinations function C(n,k) = n!/(k!*(n-k)!) by using a main method to deal with input/output, a method to compute the factorial, and a method to compute the combinations function. Here's my code: import java.util.Scanner; public class Combinations { public static void factorials (int set, int objects) { Scanner keyboard = new Scanner(System.in); int n = set; int k = objects; int c = (n-k); int factorial1 = 1; int

Accessing methods via getattr

回眸只為那壹抹淺笑 提交于 2021-02-08 05:45:18
问题 I stumbled across this behaviour, which suggests that you can use getattr to call methods on a class instance, as an alternative to the intuitively named operator.methodcaller : from operator import methodcaller class Foo(): def __init__(self, lst): self.lst = lst def summer(self): return sum(self.lst) my_obj = Foo(range(11)) res1 = methodcaller('summer')(my_obj) # 55 res2 = getattr(my_obj, 'summer')() # 55 assert res1 == res2 I'd like to understand, internally, why this works. Is it because

How to add methods to manipulate an array of class objects?

こ雲淡風輕ζ 提交于 2021-02-08 04:41:09
问题 I didnt know how to word the question, so sorry if it doesnt really make sense, but it should start making sense here. Also, I am sorry if the solution is really, really simple. Google couldnt understand what I was asking (probs cause I was asking it wrong :P) So I wrote a class that I called OrderSelection In my program I need to have an array of OrderSelection objects, and I need to perform actions on this array (re-ordering, sorting etc). What I am doing right now is keeping methods in the

How to add methods to manipulate an array of class objects?

淺唱寂寞╮ 提交于 2021-02-08 04:41:05
问题 I didnt know how to word the question, so sorry if it doesnt really make sense, but it should start making sense here. Also, I am sorry if the solution is really, really simple. Google couldnt understand what I was asking (probs cause I was asking it wrong :P) So I wrote a class that I called OrderSelection In my program I need to have an array of OrderSelection objects, and I need to perform actions on this array (re-ordering, sorting etc). What I am doing right now is keeping methods in the

Calling a method on every instance of a type in c#

岁酱吖の 提交于 2021-02-07 21:12:54
问题 I know that you can call an instance method that executes for each object. I also know that you can have a static method on the type that is callable from the type. But how would one call a method that acts on every instance of a particular type (say, to set a member variable to zero, for example)? 回答1: C# doesn't provide a direct mechanism to track all reachable objects and there's almost never a good reason to want such automatic tracking functionality (rather than, say, an explicit pool

Calling a method on every instance of a type in c#

旧时模样 提交于 2021-02-07 21:10:49
问题 I know that you can call an instance method that executes for each object. I also know that you can have a static method on the type that is callable from the type. But how would one call a method that acts on every instance of a particular type (say, to set a member variable to zero, for example)? 回答1: C# doesn't provide a direct mechanism to track all reachable objects and there's almost never a good reason to want such automatic tracking functionality (rather than, say, an explicit pool

Calling a method on every instance of a type in c#

ⅰ亾dé卋堺 提交于 2021-02-07 21:10:22
问题 I know that you can call an instance method that executes for each object. I also know that you can have a static method on the type that is callable from the type. But how would one call a method that acts on every instance of a particular type (say, to set a member variable to zero, for example)? 回答1: C# doesn't provide a direct mechanism to track all reachable objects and there's almost never a good reason to want such automatic tracking functionality (rather than, say, an explicit pool

Idiomatic way to call method on all objects in a list of objects Python 3

吃可爱长大的小学妹 提交于 2021-02-07 19:37:11
问题 I have a list of objects and they have a method called process. In Python 2 one could do this map(lambda x: x.process, my_object_list) In Python 3 this will not work because map doesn't call the function until the iterable is traversed. One could do this: list(map(lambda x: x.process(), my_object_list)) But then you waste memory with a throwaway list (an issue if the list is big). I could also use a 2-line explicit loop. But this pattern is so common for me that I don't want to, or think I

Using a generic parameter on overloaded methods

随声附和 提交于 2021-02-07 19:12:00
问题 Why does this program output Generic Value and not Hello world! : using System; class Example { public static void Print<T>(T value) { Console.WriteLine("Generic Value"); } public static void Print(string value) { Console.WriteLine(value); } public static void GenericFunc<T>(T value) { Print(value); } static void Main() { GenericFunc("Hello world!"); } } How is the generic method parameter being translated under the hood of C#? 回答1: Overload resolution is only done at compile-time. Since