methods

static <T extends Number & Comparable<? super Number>>

依然范特西╮ 提交于 2019-12-05 08:25:12
I have following class with one static method: public class Helper { public static <T extends Number & Comparable<? super Number>> Boolean inRange(T value, T minRange, T maxRange) { // equivalent (value >= minRange && value <= maxRange) if (value.compareTo(minRange) >= 0 && value.compareTo(maxRange) <= 0) return true; else return false; } } I try to call this method: Integer value = 2; Integer min = 3; Integer max = 8; Helper.inRange(value, min, max) ; Netbeans compiler show me this error message: method inRange in class Helper cannot be applied to given types; required: T,T,T found: java.lang

Mocking Method Calls In Python

点点圈 提交于 2019-12-05 08:17:04
I have been searching stack exchange and around the web for how to do this, but I cannot understand how to mock behaviors for methods. I am trying to mock openpyxl behaviors and behaviors for my custom class. Here is my attempt: import unittest from unittest.mock import MagicMock import openpyxl from MyPythonFile import MyClass class TestMyClass(unittest.TestCase): def test_myclass(self): myclass = MyClass() wb = openpyxl.workbook() ws = openpyxl.worksheet() wbPath = 'wbPath' openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb) When I try the final line I get the error

Forcing two parameters of a generic method to have the same concrete type

吃可爱长大的小学妹 提交于 2019-12-05 08:08:45
How can I have a method with two parameters, with both parameters having the same concrete type? For example, boolean equals(Object a, Object b) allows for a of any type and b of any type. I want to force such that a and b have the same concrete type. I tried <T> boolean equals(T a, T b) and inputting a Date and a String to that method, expecting a compile-time error, but I get no errors, since T will resolve to ? extends Serializable & Comparable , since both Date and String implements Serializable and Comparable . You can't, basically. There's no way of doing that. Even if you could do it

c# generic method overload not consistent with abstract Visitor pattern

我怕爱的太早我们不能终老 提交于 2019-12-05 07:33:52
experimenting with Visitor pattern and generic method I found a kind of discrepancy in C#.NET. AFAIK C# compiler prefers an explicit overload to a generic method, therefore the following code: public abstract class A { public abstract void Accept(Visitor v); } public class B : A { public override void Accept(Visitor v) { v.Visit(this); } } public class C : A { public override void Accept(Visitor v) { v.Visit(this); } } public class D : A { public override void Accept(Visitor v) { v.Visit(this); } } public class Visitor { public void Visit(B b) { Console.WriteLine("visiting B"); } public void

Static methods vs instance methods in C#

旧巷老猫 提交于 2019-12-05 07:32:53
For an application I am writing, I want to have extreme extensibility and extension methods seem to give me what I want, plus the ability to call them without an instance, which I need too. I remember reading that static methods are faster than instance methods but don't get the advantages of GC. Is this correct? It's highly unlikely I will change my design unless I find a superior alternative by design not speed. But still for extra information I wanna know the differences in speed, GC, etc. EDIT: Thanks. More info: Let's say we have a Person class: class Person which can have an instance

How to pass an Object type to Type parameter in C#

£可爱£侵袭症+ 提交于 2019-12-05 07:30:47
I have a function that has Type Parameter. public static object SetValuesToObject(Type tClass, DataRow dr) { // ............. // return object } I have no idea how to pass a class as parameter for this function. Here i want to pass parmeter Class "Product". I tried this SetValuesToObject(Product,datarow); But it doesn't work. How could i do this? The typeof keyword when you know the class at compile time. SetValuesToObject(typeof(Product),datarow); You can also use object.GetType() on instances that you don't know their type at compile time. You have a few options: instance.GetType() : this

When to use which - multiple methods, multiple parameters, or an options parameter

天大地大妈咪最大 提交于 2019-12-05 07:27:04
This question is coming from a javascript point of view, but it certainly could apply to other languages. I have been running into this more and more lately, and was wondering if there was a best practice, or at least good design standard, for when how to build your methods. The obvious options that I see are as follows, along with a trivial example for each Multiple methods: this.makeGetRequest = function(controller){...} this.makeSynchronousGetRequest = function(controller){...} this.makePostRequest = function(controller, data){...} One method, with more parameters: //data would be an

C++ Is it correct to call class member variables “attributes”?

孤街浪徒 提交于 2019-12-05 07:12:53
Can someone please disambiguate class attributes and methods for C++? I was under the impression that attribute means any member variable, and method means any member function. Thanks Define "correct". Referring to data members and member functions as "attributes/properties" and "methods", respectively, is common practice - it's the general OO wording. ("attributes" are used in C++ for something else , though, so this may very well be a source of confusion.) The C++ standard, however, does not use these terms (apart from attributes of course, as explained above). If you don't want to risk

Calling method on child class from parent class method (Objective-c 2.0)

流过昼夜 提交于 2019-12-05 06:54:24
I have experience with object-oriented programming however this situation is unfamiliar for some reason. Consider the following Objective-c 2.0 code: @interface A : NSObject @end @implementation A - (void) f { [self g]; } @end @interface B : A @end @implementation B - (void) g { NSLog(@"called g..."); } @end Is this the correct way to call a method on a child class from a method in the parent class? What happens if another child class doesn't implement method g ? Perhaps there's a better way to solve this like an abstract method in the parent class A ? The key is to have a method in the parent

How can additional data be associated with existing objects using extension methods?

依然范特西╮ 提交于 2019-12-05 06:41:57
Since .NET Framework 3.5, developers have been able to add extension methods callable from instances of any object type. Extension properties have not been implemented in C#, however. Unlike extension methods, extension properties would involve storing some extra state information for individual objects. However, even for extension methods, it would be highly useful in some programming scenarios to be able to access after-added/extension information for the objects on which those extension methods are called. Here is the original question: How can one add extension properties, or otherwise set