overriding

Force child class to override parent's methods

三世轮回 提交于 2019-11-27 10:47:23
问题 Suppose I have a base class with unimplemented methods as follows: class Polygon(): def __init__(self): pass def perimeter(self): pass def area(self): pass Now, let's say one of my colleagues uses the Polygon class to create a subclass as follows: import math class Circle(Polygon): def __init__(self, radius): self.radius = radius def perimeter(self): return 2 * math.pi * self.radius (H/Sh)e has forgotten to implement the area() method. How can I force the subclass to implement the parent's

Safely override C++ virtual functions

穿精又带淫゛_ 提交于 2019-11-27 10:32:52
I have a base class with a virtual function and I want to override that function in a derived class. Is there some way to make the compiler check if the function I declared in the derived class actually overrides a function in the base class? I would like to add some macro or something that ensures that I didn't accidentally declare a new function, instead of overriding the old one. Take this example: class parent { public: virtual void handle_event(int something) const { // boring default code } }; class child : public parent { public: virtual void handle_event(int something) { // new

Calling a base class's classmethod in Python

一个人想着一个人 提交于 2019-11-27 10:24:21
问题 Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class Derived(Base): @classmethod def do(cls, a): print 'In derived!' # Base.do(cls, a) -- can't pass `cls` Base.do(a) if __name__ == '__main__': d = Derived() d.do('hello') > $ python play.py > In derived! > <class '__main__.Base'> msg From Derived.do , how do I call Base.do ? I would normally use super or even the base class name directly if this is a normal object method, but apparently I can't find

JavaScript override methods

只谈情不闲聊 提交于 2019-11-27 10:16:06
Let's say you have the below code: function A() { function modify(){ x = 300; y = 400; } var c = new C(); } function B() { function modify(){ x = 3000; y = 4000; } var c = new C(); } C = function () { var x = 10; var y = 20; function modify() { x = 30; y = 40; }; modify(); alert("The sum is: " + (x+y)); } Now the question is, if there is any way in wich I can override the method modify from C with the methods that are in A and B. In Java you would use the super keyword, but how can you achieve something like this in JavaScript? Edit: It's now six years since the original answer was written and

Why does C#/CLR not support method override co/contra-variance?

丶灬走出姿态 提交于 2019-11-27 09:40:50
There are quite a few questions & answers about hacking around the limitation of C# not allowing method return (and argument) types to be changed to compatible types on overrides, but why does this limitation exist, either in the C# compiler or in the CLR? As I an see, there is nothing that could break if co/contra-variance was allowed, so what is the reasoning behind it? A similar question could be asked for widening access parameters - eg overriding a protected internal method with a public method (something which Java supports, IIRC) Robert Kozak Eric Lippert already answered this way

Google Chrome - Override White Blank page between webpage loads [closed]

隐身守侯 提交于 2019-11-27 09:25:15
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I'm looking for different Chrome apps to make my pages darker/inverted to reduce eye strain, I found some apps that work but the only thing left, which these apps doesn't seem to override, is the White Blank page. When a new page is loaded, Chrome first displays a White Blank page, while the page is loading then

Force all classes to implement / override a 'pure virtual' method in multi-level inheritance hierarchy

我是研究僧i 提交于 2019-11-27 09:10:06
In C++ why the pure virtual method mandates its compulsory overriding only to its immediate children (for object creation), but not to the grand children and so on ? struct B { virtual void foo () = 0; }; struct D : B { virtual void foo () { ... }; }; struct DD : D { // ok! ... if 'B::foo' is not overridden; it will use 'D::foo' implicitly }; I don't see any big deal in leaving this feature out. For example, at language design point of view, it could have been possible that, struct DD is allowed to use D::foo only if it has some explicit statement like using D::foo; . Otherwise it has to

How to override the slice functionality of list in its derived class

喜夏-厌秋 提交于 2019-11-27 09:02:32
I make a class like below: class MyList(list): def __init__(self, lst): self.list = lst I want slice functionality to be overridden in MyList You need to provide custom __getitem__() , __setitem__ and __delitem__ hooks. These are passed a slice object when slicing the list; these have start , stop and step attributes (which could be None ): def __getitem__(self, key): if isinstance(key, slice): return [self.list[i] for i in xrange(key.start, key.stop, key.step)] return self.list[key] or, for your case: def __getitem__(self, key): return self.list[key] because a list can take the slice object

@Override annotation error (android prefs)

牧云@^-^@ 提交于 2019-11-27 08:38:42
When I was trying to use this code to enable preferences into my app import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.Preference.OnPreferenceClickListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; public class Preferences extends PreferenceActivity { private RadioButton btn01; @Override

“overriding” private methods with upcasting call in java

南笙酒味 提交于 2019-11-27 08:28:06
问题 public class PrivateOverride { private void f() { System.out.println("PrivateOverride f()"); } public static void main(String[] args) { PrivateOverride po = new DerivedWithOutD(); po.d();// PrivateOverride f() PrivateOverride poD = new DerivedWithD(); poD.d();//Derived f() } public void d() { f(); } } class DerivedWithOutD extends PrivateOverride { public void f() { System.out.println("Derived f()"); } } class DerivedWithD extends PrivateOverride { public void f() { System.out.println(