methods

How to pass a Class as parameter for a method? [duplicate]

☆樱花仙子☆ 提交于 2019-12-18 06:14:07
问题 This question already has answers here : How to use class name as parameter in C# (4 answers) Closed 3 years ago . I have two classs: Class Gold; Class Functions; There is a method ClassGet in class Functions , which has 2 parameters. I want to send the class Gold as parameter for one of my methods in class Functions . How is it possible? For example: public void ClassGet(class MyClassName, string blabla) { MyClassName NewInstance = new MyClassName(); } Attention: I want to send MyClassName

How better refactor chain of methods that can return null in java?

梦想的初衷 提交于 2019-12-18 05:54:41
问题 I have code like: obj1 = SomeObject.method1(); if (obj1 != null) { obj2 = obj1.method2(); if (obj2 != null) { obj3 = obj2.method3(); if (obj3 != null) { ............ return objN.methodM(); } } } .... I have near 10 steps. It seems very fragile and error prone. Is there a better way to check on null chained methods? Thanks. 回答1: It's common problem for null references in java. I prefer chaining with && : if (obj1 != null && obj1.method1() != null && obj1.method1().method2() != null) 回答2: More

Generate additional custom method with jaxb-xjc

跟風遠走 提交于 2019-12-18 05:44:12
问题 There's some way to generate a custom method within an class generated with JAXB. I search around tutorials, including oracle's tutorial, but I didn't find clear instructions how can I custom methods to a generated class described on XML Schema. 回答1: You can write an XJC plugin: http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html 回答2: I have found the following to be the best way to add custom behavior: https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#compiling

Generate additional custom method with jaxb-xjc

青春壹個敷衍的年華 提交于 2019-12-18 05:44:01
问题 There's some way to generate a custom method within an class generated with JAXB. I search around tutorials, including oracle's tutorial, but I didn't find clear instructions how can I custom methods to a generated class described on XML Schema. 回答1: You can write an XJC plugin: http://weblogs.java.net/blog/kohsuke/archive/2005/06/writing_a_plugi.html 回答2: I have found the following to be the best way to add custom behavior: https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#compiling

Is there a use of free floating block inside a method in Java?

China☆狼群 提交于 2019-12-18 05:40:52
问题 I didn't know methods could have floating blocks like this: class X { public static void main( String [] args ) { { //<--- start int i; } //<-- ends } } I was aware of floating blocks outside methods, but never tried them inside. This could probably be used to define local scope or something. Is there a use for floating blocks inside methods in Java? 回答1: Is there a use? Yes - to limit the scope of local variables. Is it a good idea? Probably debatable (and likely will be). The "pro" camp

Array containing Methods

痴心易碎 提交于 2019-12-18 05:40:12
问题 I was wondering if you can create an Array or a List<> that contains methods. I don't want to use a switch or lots of if statements. Thanks 回答1: There you go List<Action> list = new List<Action>(); list.Add( () => ClassA.MethodX(paramM) ); list.Add( () => ClassB.MethodY(paramN, ...) ); foreach (Action a in list) { a.Invoke(); } 回答2: Yes, it is possible to have such an array or list. Depending on the number of input or output parameters, you'd use something like List<Func<T1, T2, TReturn>> An

Creating a setter method that takes extra arguments in Ruby

半城伤御伤魂 提交于 2019-12-18 05:34:50
问题 I'm trying to write a method that acts as a setter and takes some extra arguments besides the assigned value. Silly example: class WordGenerator def []=(letter, position, allowed) puts "#{letter}#{allowed ? ' now' : ' no longer'} allowed at #{position}" end def allow=(letter, position, allowed) # ... end end Writing it as an indexer works and I can call it like this: gen = WordGenerator.new gen['a', 1] = true # or explicitly: gen.[]=('a', 1, true) But when I try any of the following, the

iOS - passing arguments in action:@selector()

…衆ロ難τιáo~ 提交于 2019-12-18 05:23:10
问题 I'm adding a button to a UITableViewCell programmatically. The method to be run when the button is pressed is - (void) quantityDown:(id)sender rowNumber:(int)rowNum , where rowNum is the row that the button appears in. When adding the target to the button, Xcode autocompletes the following: [buttonDown addTarget:self action:@selector(quantityDown:rowNumber:) forControlEvents:UIControlEventTouchUpInside]; But no matter what I try, I cannot pass the row number into the method. I assumed the

iOS - passing arguments in action:@selector()

六月ゝ 毕业季﹏ 提交于 2019-12-18 05:22:21
问题 I'm adding a button to a UITableViewCell programmatically. The method to be run when the button is pressed is - (void) quantityDown:(id)sender rowNumber:(int)rowNum , where rowNum is the row that the button appears in. When adding the target to the button, Xcode autocompletes the following: [buttonDown addTarget:self action:@selector(quantityDown:rowNumber:) forControlEvents:UIControlEventTouchUpInside]; But no matter what I try, I cannot pass the row number into the method. I assumed the

Java: Using “this” as an argument/parameter name of an instance method? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-18 04:39:09
问题 This question already has answers here : Why can we use 'this' as an instance method parameter? (2 answers) Closed 3 years ago . Recently from this question I have learned that the following appears to be legal java: class Bar { void foo(Bar this) {} } Now, I have tried to find where in the java standard it says that you are allowed to do this, and looked here but I couldn't find the section. Can someone quote where it allows this form of method declaration and what the restrictions of