overloading

ruby operator overloading question

ぐ巨炮叔叔 提交于 2019-12-20 12:30:28
问题 i've been messing around with ruby and opengl for entertainment purposes, and i decided to write some 3d vector/plane/etc classes to pretty up some of the math. simplified example: class Vec3 attr_accessor :x,:y,:z def *(a) if a.is_a?(Numeric) #multiply by scalar return Vec3.new(@x*a, @y*a, @z*a) elsif a.is_a?(Vec3) #dot product return @x*a.x + @y*a.y + @z*a.z end end end v1 = Vec3.new(1,1,1) v2 = v1*5 #produces [5,5,5] which all fine and dandy, but i also want to be able to write v2 = 5*v1

Overload and hide methods in Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 12:06:02
问题 i have an abstract class BaseClass with a public insert() method: public abstract class BaseClass { public void insert(Object object) { // Do something } } which is extended by many other classes. For some of those classes, however, the insert() method must have additional parameters, so that they instead of overriding it I overload the method of the base class with the parameters required, for example: public class SampleClass extends BaseClass { public void insert(Object object, Long param)

Overload and hide methods in Java

拥有回忆 提交于 2019-12-20 12:05:13
问题 i have an abstract class BaseClass with a public insert() method: public abstract class BaseClass { public void insert(Object object) { // Do something } } which is extended by many other classes. For some of those classes, however, the insert() method must have additional parameters, so that they instead of overriding it I overload the method of the base class with the parameters required, for example: public class SampleClass extends BaseClass { public void insert(Object object, Long param)

The type of the expression must be an array type but it resolved to float

孤街醉人 提交于 2019-12-20 07:27:14
问题 I hit a bump when I'm doing my Java code. I feel like I somehow got the concept messed up, like I not sure for this: void setScore(float[] sco) { sco = score; } public void setScore(float sco, int id) { sco[id] = score; } The error message corresponds to "sco[ID] = score; " The type of the expression must be an array type but it resolved to float I'm confused what I should put in the bracket, the book asks me to put "float[] score" instead of "float[] sco", but it doesn't work, so I edited a

Overloading assignment operator for pointers to two different classes

老子叫甜甜 提交于 2019-12-20 07:26:01
问题 My Question: I'm trying to overload the assignment operator for pointers to two different classes. Here is an example: dc.h: #ifndef DC_H_ #define DC_H_ #include "ic.h" class dc { double d; char c; public: dc(): d(0), c(0) { } dc(double d_, char c_): d(d_), c(c_) { } dc* operator=(const ic* rhs); ~dc() { } }; #endif /* DC_H_ */ class ic.h: #ifndef IC_H_ #define IC_H_ class ic { int i; char c; public: ic(): i(0), c(0) { } ic(int i_, char c_): i(i_), c(c_) { } ~ic() { } }; #endif /* IC_H_ */ dc

Overloading + operator for arrays in groovy

淺唱寂寞╮ 提交于 2019-12-20 05:47:16
问题 I am a groovy newbie. Maybe this is a piece of cake, but I want to overload the + operator for arrays/lists to code like this def a= [1,1,1] def b= [2,2,2] assert [3,3,3] == a + b 回答1: I wouldn't recommend globally overriding well-established behaviors. But, if you insist, this will do as you ask: ArrayList.metaClass.plus << {Collection b -> [delegate, b].transpose().collect{x, y -> x+y} } A more localized alternative would be to use a category: class PlusCategory{ public static Collection

Product Method Overloading

谁说胖子不能爱 提交于 2019-12-20 04:53:45
问题 so I was working on this problem on CodeHS, then I was stuck for so long so decided to ask here. The exercise is to overload the product method to allow for multiplying together other types of values: two doubles an int and a double a double and an int three ints three doubles public class Product extends ConsoleProgram { public void run() { int intValue = 5; double doubleValue = 2.5; int product1 = product(intValue, intValue); System.out.println(product1); // Use method overloading to define

java method overload inheritance and polymorphism

假如想象 提交于 2019-12-20 04:25:09
问题 Here's a test practice question i came across, would appreciate your help in making me understand the concepts Let Hawk be a subclass of Bird. Suppose some class has two overloaded methods void foo(Hawk h) and void foo(Bird b). Which version would get executed in the call foo(x) after the declaration Bird x = new Hawk(); Here's the code i have so far, could someone explain to me why foo(bird b) gets executed? public class MPractice { public static void main(String args[]) { Bird x = new Hawk(

PowerMockito throws NullPointerException when trying to stub private overloaded method

*爱你&永不变心* 提交于 2019-12-20 03:25:17
问题 I'm (still) trying to check if bar(Alpha, Baz) called bar(Xray, Baz) using PowerMockito (as bar(Xray, Baz) is private ) - without actually calling the later, given my MCVE class Foo below. (I went through the same class earlier, with all methods in Foo being public - in case you've got a déjà vu...) public class Foo { private String bar(Xray xray, Baz baz) { return "Xray"; } private String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get()

How does non idiomatic global operator overloading work?

可紊 提交于 2019-12-20 02:11:00
问题 I want to understand the code from this answer type Mult = Mult with static member inline ($) (Mult, v1: 'a list) = fun (v2: 'b list) -> v1 |> List.collect (fun x -> v2 |> List.map (fun y -> (x, y))) : list<'a * 'b> static member inline ($) (Mult, v1:'a ) = fun (v2:'a) -> v1 * v2 :'a let inline (*) v1 v2 = (Mult $ v1) v2 F# can resolve overloaded members. (Because it doesn't support currying of members). So, I supposed, it should work for methods as well But it doesn't: type Mult = Mult with