inheritance

force base class to use its own method and not overrided method

青春壹個敷衍的年華 提交于 2019-12-31 01:00:34
问题 here is my scenario: class SomeBaseClass { public void foo(String str) { ....... } public void foo(String strs[]) { ....... } } class MyClass extends SomeBaseClass { public void foo(String str) { super.foo(str); } public void foo(String strs[]) { throw new RuntimeException("only one input please!"); } } The logic is pretty simple. "SomeBaseClass" is 3rd party tool that i cannot modify. I want to limit its functionality and don't want to allow foo(String strs[]). the problem is that inside

Force a Derived Class to use the Constructor of the Base Class

别来无恙 提交于 2019-12-31 00:57:26
问题 Is there a way to force a derived class to use the constructor of the abstract base class? It must not be a real constructor, I have an open mind about creative solutions. class Abstract { private: int Member; string Text; public: Abstract(int Member, string Text) { this->Member = Member; this->Text = Text; } // e.g. defining virtual functions } For example my abstract class has some private members which every derived class should also have. And they should be defined in the constructor,

What does the new() constraint do on a class definition?

只愿长相守 提交于 2019-12-30 23:27:09
问题 I saw this code example and was wondering what the purpose of the new() constraint was: public class Client<T> : IClient where T : IClientFactory, new() { public Client(int UserID){ } } 回答1: That's called a "'new' constraint". Here's the documentation on it. The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor . To use the new constraint, the type cannot be abstract. (Emphasis mine) Basically, you need it whenever you

Object and String prototypes are not prototypes of defined strings?

微笑、不失礼 提交于 2019-12-30 22:59:16
问题 I'm trying to understand how JavaScript's prototype-based inheritance works. I was expecting the below outputs would evaluate as true . Why is this? var myStr = "Sample"; String.prototype.isPrototypeOf(myStr); // false Object.prototype.isPrototypeOf(myStr); // false 回答1: JavaScript has both primitive strings and string objects. What you've written there is a primitive string. The Object.prototype.isPrototypeOf method always returns false for any primitive, so your results make sense. If you

Numpy Matrix class: Default constructor attributes for inherited class

落爺英雄遲暮 提交于 2019-12-30 19:00:16
问题 I want to implement my own matrix-class that inherits from numpy's matrix class. numpy's matrix constructor requires an attribute, something like ("1 2; 3 4'") . In contrast, my constructor should require no attributes and should set a default attribute to the super-constructor. That's what I did: import numpy as np class MyMatrix(np.matrix): def __init__(self): super(MyMatrix, self).__init__("1 2; 3 4") if __name__ == "__main__": matrix = MyMatrix() There must be a stupid mistake in this

Why does console.log() not show inherited properties from Object.create?

杀马特。学长 韩版系。学妹 提交于 2019-12-30 18:46:18
问题 I am running into a hangup while trying to leverage Object.defineProperty() on a base object. I want to inherit properties from that object, using Object.create() , and then define more properties in the derived object (which may be inherited from there). I should note that I am targetting this at node.js. Here's an example: var Base = {}; Object.defineProperty(Base, 'prop1', { enumerable:true, get:function(){ return 'prop1 value';} }); Object.defineProperty(Base, 'prop2', { enumerable:true,

Why does console.log() not show inherited properties from Object.create?

自古美人都是妖i 提交于 2019-12-30 18:46:07
问题 I am running into a hangup while trying to leverage Object.defineProperty() on a base object. I want to inherit properties from that object, using Object.create() , and then define more properties in the derived object (which may be inherited from there). I should note that I am targetting this at node.js. Here's an example: var Base = {}; Object.defineProperty(Base, 'prop1', { enumerable:true, get:function(){ return 'prop1 value';} }); Object.defineProperty(Base, 'prop2', { enumerable:true,

Overriding default accessor method across different classloaders breaks polymorphism

≯℡__Kan透↙ 提交于 2019-12-30 18:31:21
问题 I come across to a strange behavior while trying to override a method with default accessor (ex: void run() ). According to Java spec, a class can use or override default members of base class if classes belongs to the same package. Everything works correctly while all classes loaded from the same classloader. But if I try to load a subclass from separate classloader then polymorphism don't work. Here is sample: App.java: import java.net.*; import java.lang.reflect.Method; public class App {

Why does my class implement child interfaces, but not their parents?

孤街醉人 提交于 2019-12-30 17:25:32
问题 I found a (at least for me) unexpected behavior when using interface inheritance in Delphi. I have this simple class and interface hierarchy: +---------------+ | << IMyBase >> | +---------------+ ^ | +---------------+ | << IMyIntf >> | +---------------+ ^ | +---------+ | TMyObj | +---------+ I wanted to declare a variable of type IMyBase . Create a TMyObj and assign it to my variable. IHMO this is normal OOP practice. But it turned out that it does not compile. I Have also tried to declare a

Error: Main method not found in class myPackage.inheritance

若如初见. 提交于 2019-12-30 14:38:13
问题 package myPackage; public class inheritance { int salary = 50000; } class worker extends inheritance { int bonus = 10000; public static void main(String[] args) { worker obj1 = new worker(); System.out.println("employee salary is" + obj1.salary); System.out.println("employee bonus is" + obj1.bonus); } } Hi.. I am new to java. I am trying to write an inheritance program and getting this error. Error: Main method not found in class myPackage.inheritance, please define the main method as: public