overriding

How to override method with derived return type in C#?

六月ゝ 毕业季﹏ 提交于 2019-12-02 11:46:52
I want to override a virtual method with a derived class type. What's the current best way to do this? So far I've found two approaches: Use an abstract base class for each derived type; bridge with protected methods. Use a protected implementation with a public accessor. Base case (no solution implemented, Clone always returns base type A1 ): public class A1 { public int X1 { get; set; } public A1(int x1) { this.X1 = x1; } public virtual A1 Clone() { return new A1(X1); } } public class A2 : A1 { public int X2 { get; set; } public A2(int x1, int x2) : base(x1) { this.X2 = x2; } public override

static method behaving like other method those can override

╄→гoц情女王★ 提交于 2019-12-02 11:46:33
On object of child class, static methos of super class are available but when we define same method in child class, now object of child class start pointing to child class method.this complete sounds like overriding but it is not,since static method can't override. How this is happen and what this functionality of java is called? class A extends B { public static void main(String[] args) { new A().method();//call class B's method if method is not in A otherwise A's } /* public static void method(){ System.out.println("class A method"); */ } class B { public static void method() { System.out

Is it wrong trying to suppress overriding on a case by case basis? [closed]

99封情书 提交于 2019-12-02 11:41:16
I understand why polymorphism achieved through method overriding is very useful. I am asking what problems, if any, might arise with trying to suppress it in certain situations, at the time the polymorphic object is received as an argument (not at the time its class is defined!). class Car describes the behavior of a car. class FlyingCar describes the behavior of a car that can transform and fly. I received from somewhere the object of class Car or its subclass. I have no control over what they pass me. I know that due to the technical limitations of my graphics engine I cannot display the a

Override AutoSize for derived Label Control in C#

守給你的承諾、 提交于 2019-12-02 11:36:18
I am trying to extend the System.Windows.Forms.Label class to support vertically drawn text. I do this by creating a new property called MyLabelOrientation that the user can set to Horizontal or Vertical. When the user changes this setting, the values for width and height are swapped to resize the control to its new orientation. Finally, I override the OnPaint function to draw my Label. I would like to extend the AutoSize property for this control as well so that my Label will auto-size to the text it contains. For the horizontal orientation, the base functionality implements this for me. For

Attributes and polymorphism

那年仲夏 提交于 2019-12-02 11:35:31
问题 I have 2 classes: public class Increase { public int a=3; public void add(){ a+=5; System.out.println("f"); } } class SubIncrease extends Increase{ public int a=8; public void add(){ a+=5; System.out.println("b" + a); } } But when I run Increase f=new SubIncrease(); System.out.println(f.a); f.add(); System.out.println(f.a); I got this output: 3 b13 3 Could anyone help me to understand why this happens? The value of the a attribute was changed in method add, as shown by the second outpuy row..

C++ overridden function not called

北战南征 提交于 2019-12-02 11:02:36
I am running into an issue where an overloaded function is not called, and the base function is called instead. I suspect this is related to how things are split between the project files. In files obj1.h/obj1.cpp I have something like this class obj1{ public: void print(); }; void obj1::print(){ cout << "obj1::print()"; } In files obj2.h/obj2.cpp I have something like this: #include "obj1.h" class obj2 : public obj1{ public: void print(); }; void obj2::print(){ cout << "obj2::print()"; } In separate files, I do something like this: #include "obj1.h" class obj3{ public: vector<obj1*> objlist;

Which operator needs to be overridden in order to use std::set in the C++ code?

只谈情不闲聊 提交于 2019-12-02 10:39:49
This is an interview question. Referring to the sample code, which one of the operators needs to be overridden in order to use std::set<Value> #include<iostream> class Value { std::string s_val; int i_val; public: Value(std::string s, int i): s_val(s) , i_val(i){} }; // EOF /* a operator != b operator > c operator <= d operator >= e operator < */ Actually, I do not understand why an operator needs to be overridden here. "set" does not allow duplicated elements, maybe operator != needs to be overridden ? You don't have to override any operator, the std::set class template allows you to provide

error in overriding generic collections in Java

此生再无相见时 提交于 2019-12-02 09:31:30
When I try to override a method that takes a List<String> , I get the following compile error . Multiple markers at this line: - The method getname(List<Integer>) of type child must override or implement a supertype method - Name clash: The method getname(List<Integer>) of type child has the same erasure as getname(List<String>) of type parent but does not override it I was under the impression, that due to erasure , a method that takes a List<String> and a subclass method that takes a List<Integer> would be considered as overridden , because the signature of both methods are same after

How to override/clone modules, which are downloaded to the vendor by composer in zf2?

荒凉一梦 提交于 2019-12-02 08:53:32
SO!!! I added zfcUser module (but I want to do this with any other module too) to my project, based on Zend Framework 2.*. How I can override controllers/views/configs of this module, so changes of them wouldn't be deleted with updating dependencies with composer (I tried to do this and didn't found files are chaned, but what if.... Oo). I know that there are the way existing to do this, but I don't know it. View Helper? Or what? Help me, please! Thank you. ADDITIONAL INFO Current folders and files of ZfcUserOverride: let's answer all your question one by one. But before let's keep in mind

Java cast to superclass and call overload method

荒凉一梦 提交于 2019-12-02 08:16:23
问题 abstract class A { int met(A a) { return 0; } int met(B b) { return 1; } int met(C c) { return 2; } } class B extends A { int met(A a) { return 3; } int met(B b) { return 4; } int met(C c) { return 5; } } class C extends B { int f() { return ((A)this).met((A)this); } } public class teste { public static void main(String args[]) { C x = new C(); System.out.println(x.f()); } } The program will return 3 and I was expecting 0. Why does the first cast in the method f do nothing and the second one