methods

C# Delegate Instantiation vs. Just Passing the Method Reference

拟墨画扇 提交于 2019-12-17 09:57:02
问题 I have a simple question: what's the advantage of instantiating a C# delegate as opposed to just passing the function reference? What I mean is: Why do: Thread t = new Thread(new ThreadStart(SomeObject.SomeMethod)); When you can do: Thread t = new Thread(SomeObject.SomeMethod); Both will compile and work in my experience...am I missing something? 回答1: As long as the method group SomeObject.SomeMethod has a method with return type void and taking no parameters there is no difference. This is

My object is not updated even if I use the pointer to a type to update it

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 09:55:28
问题 I store some Individual objects in a slice. Before appending it to the slice I print the name of the Individual object. After I have stored it in the slice, I then retrieve it as a pointer and want to change the name to "Peter" , but the change does not work since it still prints "Steve" . Why? type Individual interface { GetName() *string SetName(name string) } type Person struct { name string } // Implement functions of the Individual interface func (p Person) GetName() *string { return &p

Calling a method with a pointer receiver by an object instead of a pointer to it?

心已入冬 提交于 2019-12-17 09:54:19
问题 v is an object of Vertex , and Scale is a method for a pointer to Vertex . Then why is v.Scale(10) not wrong, given that v isn't a pointer to a Vertex object? Thanks. package main import ( "fmt" "math" ) type Vertex struct { X, Y float64 } func (v Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func (v *Vertex) Scale(f float64) { v.X = v.X * f v.Y = v.Y * f } func main() { v := Vertex{3, 4} v.Scale(10) fmt.Println(v.Abs()) } 回答1: Spec: Calls: A method call x.m() is valid if the

how to find all methods called in a method?

霸气de小男生 提交于 2019-12-17 09:50:24
问题 how to take the methods of other classes invoked in a specific method? EXAMPLE method getItem1() public String getItem1() throws UnsupportedEncodingException{ String a = "2"; a.getBytes(); a.getBytes("we"); System.out.println(a); int t = Integer.parseInt(a); return a; } The methods called in getItem1() are: String.getBytes() String.getBytes(String) PrintStream.println(String) Integer.parseInt(String) 回答1: I would do this with javassist. So let's say you have the following class accessible in

How Can I Pass a Member Function to a Function Pointer?

こ雲淡風輕ζ 提交于 2019-12-17 09:49:17
问题 class Child; class Parent { public: void (*funcPointer)(); void (*funcPointer2)(Parent* _this); void (Child::*funcPointer3)(); }; class Child: public Parent { public: void TestFunc(){ } void Do(){ Parent p; p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)' p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)' p.funcPointer3=TestFunc; //this

Performance of using static methods vs instantiating the class containing the methods

帅比萌擦擦* 提交于 2019-12-17 09:39:40
问题 I'm working on a project in C#. The previous programmer didn't know object oriented programming, so most of the code is in huge files (we're talking around 4-5000 lines) spread over tens and sometimes hundreds of methods, but only one class. Refactoring such a project is a huge undertaking, and so I've semi-learned to live with it for now. Whenever a method is used in one of the code files, the class is instantiated and then the method is called on the object instance. I'm wondering whether

Performance of using static methods vs instantiating the class containing the methods

亡梦爱人 提交于 2019-12-17 09:39:18
问题 I'm working on a project in C#. The previous programmer didn't know object oriented programming, so most of the code is in huge files (we're talking around 4-5000 lines) spread over tens and sometimes hundreds of methods, but only one class. Refactoring such a project is a huge undertaking, and so I've semi-learned to live with it for now. Whenever a method is used in one of the code files, the class is instantiated and then the method is called on the object instance. I'm wondering whether

Calling a class method raises a TypeError in Python

陌路散爱 提交于 2019-12-17 08:45:10
问题 I don't understand how classes are used. The following code gives me an error when I try to use the class. class MyStuff: def average(a, b, c): # Get the average of three numbers result = a + b + c result = result / 3 return result # Now use the function `average` from the `MyStuff` class print(MyStuff.average(9, 18, 27)) Error: File "class.py", line 7, in <module> print(MyStuff.average(9, 18, 27)) TypeError: unbound method average() must be called with MyStuff instance as first argument (got

Objective C calling method dynamically with a string

偶尔善良 提交于 2019-12-17 08:34:09
问题 Im just wondering whether there is a way to call a method where i build the name of the method on the fly with a string. e.g. I have a method called loaddata -(void)loadData; to call this i would normally call it like [self loadData]; But i want to be able to call it dynamically with a string e.g. NSString *methodName = [[NSString alloc] initWithString:@"loadData"]; [self methodName]; This is a stupid example but i hope you get my point. I am using it for databinding classes that I am setting

jQuery call function from a string [duplicate]

拟墨画扇 提交于 2019-12-17 08:30:11
问题 This question already has answers here : How to execute a JavaScript function when I have its name as a string (33 answers) Closed 5 years ago . Is it possible to call a function by using the strings ? (i.e) I have a variable var target = 'next'; . Using this string I want to call the jquery method next() . Should i use target + '()' (this is bit foolish) to call next() ? I know it can be done using conditional statements. Since it is a string got from users, but it is difficult to use