polymorphism

super-constructor if there is no super class?

笑着哭i 提交于 2020-02-10 12:11:55
问题 I found a class like this: public class Computer implements Serializable { private static final long serialVersionUID = 1L; //... public Computer() { super(); } //... } Can someone explain me, how this works? The class isn't inheriting any super class and there could still be "super();" in the constructor? 回答1: By default all classes inherit java.lang.Object . So a hidden code in your class is public class Computer extends java.lang.Object implements Serializable { private static final long

super-constructor if there is no super class?

亡梦爱人 提交于 2020-02-10 12:10:28
问题 I found a class like this: public class Computer implements Serializable { private static final long serialVersionUID = 1L; //... public Computer() { super(); } //... } Can someone explain me, how this works? The class isn't inheriting any super class and there could still be "super();" in the constructor? 回答1: By default all classes inherit java.lang.Object . So a hidden code in your class is public class Computer extends java.lang.Object implements Serializable { private static final long

Polymorphism and dependency injection - too many dependencies

拜拜、爱过 提交于 2020-02-08 02:35:02
问题 So I have read that if we see a switch statement, its a sign that it needs polymorphism. I saw such polymorphism example: include 'vendor/autoload.php'; $calc = new Calculator(); $calc->setOperands(5, 6); $calc->setOperation(new Addition); echo $result = $calc->calculate(); And of course there can be various classes as Subtract, Multiply, Root, etc. Now lets say I want to use this code in a Laravel framework, but this I think should apply to any php framework. Example of addition class ( same

Polymorphism and dependency injection - too many dependencies

柔情痞子 提交于 2020-02-08 02:34:09
问题 So I have read that if we see a switch statement, its a sign that it needs polymorphism. I saw such polymorphism example: include 'vendor/autoload.php'; $calc = new Calculator(); $calc->setOperands(5, 6); $calc->setOperation(new Addition); echo $result = $calc->calculate(); And of course there can be various classes as Subtract, Multiply, Root, etc. Now lets say I want to use this code in a Laravel framework, but this I think should apply to any php framework. Example of addition class ( same

C++ allow derived classes of friend to have access to private nested class

空扰寡人 提交于 2020-02-06 07:30:06
问题 Here's what I'm trying to do: class A { friend class C (and all of C's derived classes) public: void DoAThing() { mpMyC->DelegateResponsibility(myB); } private: class B { }; B mMyB; C* mpMyC; }; class C { // No problem here- C can see B virtual void DelegateResponsibility(const A::B& necessaryInfo); }; class D: public C { // Uh-oh- we don't inherit friendship virtual void DelegateResonsibility(const A::B& necessaryInfo); }; In short, I have a private nested class inside A because it's an

Polymorphism in template functions

筅森魡賤 提交于 2020-02-05 22:53:30
问题 I want to use template function for handling both polymorphic and non-polymorphic classes. Here are 3 basic classes. class NotDerived { }; class Base { public: virtual ~Base() {} void base_method() {} }; class Derived : public Base { }; Since NotDerived has no virtual functions, I can't use dynamic_cast, next comes template function: template<class T> auto foo(T& some_instance) { if (std::is_base_of_v<Base, T>) { //CASE_1: Works for d and b /*some_instance.base_method();*/ //CASE_2: Works for

Polymorphism in template functions

六眼飞鱼酱① 提交于 2020-02-05 22:53:08
问题 I want to use template function for handling both polymorphic and non-polymorphic classes. Here are 3 basic classes. class NotDerived { }; class Base { public: virtual ~Base() {} void base_method() {} }; class Derived : public Base { }; Since NotDerived has no virtual functions, I can't use dynamic_cast, next comes template function: template<class T> auto foo(T& some_instance) { if (std::is_base_of_v<Base, T>) { //CASE_1: Works for d and b /*some_instance.base_method();*/ //CASE_2: Works for

Polymorphism in template functions

懵懂的女人 提交于 2020-02-05 22:51:31
问题 I want to use template function for handling both polymorphic and non-polymorphic classes. Here are 3 basic classes. class NotDerived { }; class Base { public: virtual ~Base() {} void base_method() {} }; class Derived : public Base { }; Since NotDerived has no virtual functions, I can't use dynamic_cast, next comes template function: template<class T> auto foo(T& some_instance) { if (std::is_base_of_v<Base, T>) { //CASE_1: Works for d and b /*some_instance.base_method();*/ //CASE_2: Works for

Polymorphism in Java and creating objects from these classes

烂漫一生 提交于 2020-02-04 02:02:43
问题 I'm a little confused exactly about how polymorphism and extending classes etc... works in Java when creating objects from these classes. I recently had a problem which someone on here helped me resolved (see Not able to access object sub class's variable? (Java / Android)) for background. I was attempting to create an object like so: Quad hero = new Hero(); Where Hero was a subclass of Quad(); I had variables in my Hero class which I wasn't above to access. The solution was to change my

Fortran + OpenMP + polymorphism: what exactly is not supported?

隐身守侯 提交于 2020-02-03 08:21:29
问题 I am aware that the OpenMP 4.5 standard says that in Fortran "polymorphic entities" are not supported. What exactly does this mean? Does this only exclude calls to type-bound procedures that have a PASS attribute, but I can still use an instance of a user-defined type that has type-bound procedures in other ways (e.g. accessing its components)? Does this limitation only apply to the OMP PARALLEL block, or also to procedures called from this block, or to the entire compilation unit? Would be