overloading

What is the reasoning behind not allowing supertypes on Java method overrides?

馋奶兔 提交于 2021-01-20 14:48:57
问题 The following code is considered invalid by the compiler: class Foo { void foo(String foo) { ... } } class Bar extends Foo { @Override void foo(Object foo) { ... } } I think that this is described in the JLS 8.4.8.1: "The signature of m1 is a subsignature (§8.4.2) of the signature of m2." and in 8.4.2: "the bounds of corresponding type variables are the same". My question is: why can't the parameter in the subtype (Bar) be a supertype of the parameter in the supertype (Foo). In the example

How to overload print function to expand its functionality?

不想你离开。 提交于 2020-12-01 10:22:05
问题 I am wondering if the build-in function print could be overridden so that the following statement will write to the console and a file at the same time. print("test0","test1","test2",sep='\n') Also, may I know if it is possible to modify the source code of the build-in print function? 回答1: You can create a class with a write method and inside of that method you can print both stdout as well as write to the file. import sys class A(object): def __init__(self, f): self.f = open(f, 'w') def _

Why do I need to redeclare overloaded virtual functions?

纵然是瞬间 提交于 2020-11-29 09:35:40
问题 I have a base class with two overloaded functions f(void) and f(int) . The class Derived implements f(int) by calling f(void) . Derived2 implements f(void) only. The compiler rejects the implementation Derived::f(int) because it wants to call f(int) but I provided no arguments because I want to call f(void) . Why does the compiler reject it? Why does adding the line virtual int f(void) = 0; fix my problem? class Base { public: explicit Base(void) {} virtual ~Base(void) {} virtual int f(void)

Why do I need to redeclare overloaded virtual functions?

自作多情 提交于 2020-11-29 09:34:08
问题 I have a base class with two overloaded functions f(void) and f(int) . The class Derived implements f(int) by calling f(void) . Derived2 implements f(void) only. The compiler rejects the implementation Derived::f(int) because it wants to call f(int) but I provided no arguments because I want to call f(void) . Why does the compiler reject it? Why does adding the line virtual int f(void) = 0; fix my problem? class Base { public: explicit Base(void) {} virtual ~Base(void) {} virtual int f(void)

Can you make a function accept two different data types?

早过忘川 提交于 2020-11-29 09:06:18
问题 I've got a function that should accept two diffrent data types as input: vec3 add(vec3 vec){ this.x += vec.x; this.y += vec.y; this.z += vec.z; return this; } vec3 add(num scalar){ this.x += scalar; this.y += scalar; this.z += scalar; return this; } but this returns an error: The name 'add' is already defined Is there a way to make this work in Dart? I know that types are optional but I would like to know whether there is a way. 回答1: Dart doesn't allow function/method overloading. You can

Can you make a function accept two different data types?

ぃ、小莉子 提交于 2020-11-29 09:06:06
问题 I've got a function that should accept two diffrent data types as input: vec3 add(vec3 vec){ this.x += vec.x; this.y += vec.y; this.z += vec.z; return this; } vec3 add(num scalar){ this.x += scalar; this.y += scalar; this.z += scalar; return this; } but this returns an error: The name 'add' is already defined Is there a way to make this work in Dart? I know that types are optional but I would like to know whether there is a way. 回答1: Dart doesn't allow function/method overloading. You can

Can you make a function accept two different data types?

て烟熏妆下的殇ゞ 提交于 2020-11-29 09:05:59
问题 I've got a function that should accept two diffrent data types as input: vec3 add(vec3 vec){ this.x += vec.x; this.y += vec.y; this.z += vec.z; return this; } vec3 add(num scalar){ this.x += scalar; this.y += scalar; this.z += scalar; return this; } but this returns an error: The name 'add' is already defined Is there a way to make this work in Dart? I know that types are optional but I would like to know whether there is a way. 回答1: Dart doesn't allow function/method overloading. You can