methods

Context dependent method in Python - what am I doing wrong?

廉价感情. 提交于 2019-12-06 15:53:59
问题 I would like to create a class whose f method depends on the "mode" the object of the class has been created. The code below doesn't work but hope it gets you an idea of what I am trying to do. My idea is to have a dictionary in which I define the settings for each mode (in this case the function or method to assign to self.f, so that rather than using many if elif statements in the init function I just assign the correct values using the dictionary. class A(object): _methods_dict={ 'a':A.f1,

C# - Access another class methods

落爺英雄遲暮 提交于 2019-12-06 15:32:40
I have something like this: public class Map { class DataHandler { private int posX, posY; // Other attributes public int GetX() { return posX; } public int GetY() { return posY; } // Other functions for handling data (get/set) } class FileHandler { // Functions for reading/writing setting files } DataHandler Data; FileHandler Files; public Map() { Data = new DataHandler(); Files = new FileHandler(); } } Now, when I write a setting file I need to read some variables (non-static) of the DataHandler class. For instance, let's suppose I want to get the value of posX and posY , using the GetX and

viewDidLoad called before an IBAction?

佐手、 提交于 2019-12-06 15:13:30
问题 in my Xcode project I have a button, that does two things. First: call an IBAction Second: go to another view (segue) In the action linked to the button I set a variable to YES (global var), but in the viewDidLoad method in the linked view, its still NO. Its YES in viewDidAppear though. I wonder why the action is not called first. Can I change that? I'd prefer to use the viewDidLoad method, since it seems to load quicker. 回答1: You can use the prepareForSegue method to pass whatever data to

How to call a function or method inside its own class in php?

被刻印的时光 ゝ 提交于 2019-12-06 15:11:27
问题 I declare my class in PHP and several functions inside. I need to call one of this functions inside another function but I got the error that this function is undefined. This is what I have: <?php class A{ function b($msg){ return $msg; } function c(){ $m = b('Mesage'); echo $m; } } 回答1: You can use class functions using $this <?php class A{ function b($msg){ return $msg; } function c(){ $m = $this->b('Mesage'); echo $m; } } 回答2: This is basic OOP. You use the $this keyword to refer to any

How to call a method from another class in Java

让人想犯罪 __ 提交于 2019-12-06 14:42:05
So, I have this class: public class Product { private String name, id, info ; private int quantity; public Product(String newName, String newID, String newInfo, Integer newQuantity){ setName(newName); setID(newID); setPrice(newInfo); setQuantity(newQuantity);} public void setName(String name) { this.name = name; } public void setID(String id) { this.id = id; } public void setPrice(String info) { this.info = info; } public void setQuantity(Integer quantity) { this.quantity = quantity; } public String getID( ) { return id; } public String getName( ) { return name; } public String getInfo( ) {

Javascript pub/sub implementation works for functions, but not objects' methods

北城余情 提交于 2019-12-06 14:33:51
问题 I'm trying to implement a simple Pub/Sub object in javascript, and here's my code: var PubSub=new function(){ this.subscriptions=[]; this.subscribe=function(topic,callback){ if(!this.subscriptions[topic]){ this.subscriptions[topic]=[]; } this.subscriptions[topic].push(callback); } this.unsubscribe=function(topic,callback){ if(this.subscriptions[topic]){ for(var i=this.subscriptions[topic].length-1;i>=0;i--){ if(this.subscriptions[topic][i]==callback){ this.subscriptions[topic].splice(i,1); }

How to call a method inside a jquery function from global scope?

有些话、适合烂在心里 提交于 2019-12-06 14:18:10
I am looking for a way to call a method inside a jquery function. Example : in the above code, how can I call the method() method from global scope? (function( $ ) { $.fn.test = function() { var method = function() { alert('test succeeded!'); }; } })( jQuery ); I tried with the following code: $(document).ready(function() { $(document).test.method(); // undefined }); But this does not help. Fiddle: http://jsfiddle.net/kB7mc/ Your method is of local scope available inside the function test only, you cannot access it outside the scope. Instead you can do it this way. Also while calling it,

Java Question, how to get the value of a method from an unknown object

冷暖自知 提交于 2019-12-06 14:05:36
问题 I have lots of object defined in the system, perhaps 1000 objects, and some of them have this method: public Date getDate(); Is there anyway I can do something like this: Object o = getFromSomeWhere.....; Method m = o.getMethod("getDate"); Date date = (Date) m.getValue(); 回答1: If you can make them all implement an interface, that would certainly be the best option. However, reflection will also work, and your code was nearly there: Object o = getFromSomeWhere.....; Method m = o.getClass()

How to figure out dynamically all methods with custom attribute

一世执手 提交于 2019-12-06 11:50:35
I have a simple challenge. I dynamically need to figure out all methods with a specific attribute in C#. I'm going to load the assemblies dynamically from another application and need to find out the exact methods. The assemblies look like the followings: Base.dll: Class Base { [testmethod] public void method1() ... } Derived.dll: Class Derived:Base { [testmethod] public void method2() } Now in 3rd application I dynamically like to load the above mentioned dlls and find out testmethods. If I load Base.dll, I need to get testmethod1. If I load Drived.dll, should I get testmethod1 and

Objective C - “Duplicate declaration of method” compilation error

為{幸葍}努か 提交于 2019-12-06 11:44:27
I have this piece of code: - (id) getSearchSuggestions:(NSString*)q; - (NSOperationQueue*) getSearchSuggestions:(NSString*)q callback:(id<UserDelegate>)callback; - (id) getSearchSuggestions; - (NSOperationQueue*) getSearchSuggestions:(id<UserDelegate>)callback; And I Xcode keeps showing me an error on the last line: Duplicate declaration of method "getSearchSuggestions" Why? It seems to me that the signatures are all different. This signature: - (id) getSearchSuggestions:(NSString*)q; Is identical to this signature: - (NSOperationQueue*) getSearchSuggestions:(id<UserDelegate>)callback; All