inheritance

Abstract base class should have or not have data members when using decorator pattern C++

狂风中的少年 提交于 2020-01-06 21:28:34
问题 Some posts have previously asked/discussed whether to include private data members in abstract base classes. I want to explore this here on a concrete example in combination with the decorator pattern. I came across this issue when I was implementing a Monte Carlo library to price financial derivatives. A random normal number generator is an essential part of a Monte Carlo pricing library. You have some options on how to define you normal variables starting from a random uniform number

Entity Framework 6: one-to-one relationship with inheritance

喜夏-厌秋 提交于 2020-01-06 20:34:50
问题 I'm using EF6 Code First and here's a simple model which reproduces my issue: abstract class GeoInfo { public int Id { get; set; } public double CoordX { get; set; } public double CoordY { get; set; } } class PersonGeoInfo : GeoInfo { [Required] public Person Person { get; set; } } class CarGeoInfo : GeoInfo { [Required] public Car Car { get; set; } } class Person { public int Id { get; set; } public string Name { get; set; } public virtual PersonGeoInfo PersonGeoInfo { get; set; } } class

Initialise child class with instance of parent class

被刻印的时光 ゝ 提交于 2020-01-06 20:09:08
问题 Suppose I have a class: class Person(object): def __init__(self, name, hobbies): self.name = name self.hobbies = hobbies ... (and so on) Now I want to initialise a child class, Employee, which extends person. I would like to initialise that class with an instance of the Person class. So I would like to do: class Employee(Person): def __init__(self, person, salary): # Initialise the superclass from the given instance somehow # I know I could do: super(Employee, self).__init__(person.name,

Java override explanation

与世无争的帅哥 提交于 2020-01-06 16:35:31
问题 Here is my code class Glyph { void draw() { System.out.println("Glyph.draw()"); } Glyph(int i) { System.out.println("Glyph() before draw()"); draw(); System.out.println("Glyph() after draw()"); } } class RoundGlyph extends Glyph { private int radius = 1; RoundGlyph(int r) { super(r); radius = r; System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius); } void draw() { System.out.println("RoundGlyph.draw(), radius = " + radius); } } public class PolyConstructors { public static void

Java override explanation

北慕城南 提交于 2020-01-06 16:34:47
问题 Here is my code class Glyph { void draw() { System.out.println("Glyph.draw()"); } Glyph(int i) { System.out.println("Glyph() before draw()"); draw(); System.out.println("Glyph() after draw()"); } } class RoundGlyph extends Glyph { private int radius = 1; RoundGlyph(int r) { super(r); radius = r; System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius); } void draw() { System.out.println("RoundGlyph.draw(), radius = " + radius); } } public class PolyConstructors { public static void

Explicitly calling a default method in Java

馋奶兔 提交于 2020-01-06 15:20:51
问题 Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it's possible to explicitly invoke the default implementation of a method when that method has been overridden or is not available because of conflicting default implementations in different interfaces. interface A { default void foo() { System.out.println("A.foo"); } } class B implements A { @Override public void foo() { System.out.println("B.foo"); }

JaxB inheritance marshalling abstract classes

时光毁灭记忆、已成空白 提交于 2020-01-06 15:20:08
问题 I've been searching for the best way to perform the following action using JaxB but I cannot find a way that works. I followed the tutorial here to allow for marshaling and unmarshaling of subclasses. It achieves all that I am looking for, except that in order for the subclasses to be properly marshaled and unmarshaled, they must be wrapped in a class with a specific @XmlRootElement . This doesn't allow you to represent the classes themselves as Xml on their own. I want to have a classes like

Seeing what class an object is

主宰稳场 提交于 2020-01-06 13:12:09
问题 If I have a pointer to a base class A in C++, how would I be able to tell in my code that the pointer is to a derived class B or C ? 回答1: Assuming the base class A is polymorphic (i.e. it has at least one virtual function), you can use dynamic_cast . Given an A* ap; : if (B* bp = dynamic_cast<B*>(ap)) { // the object is a B } else if (C* cp = dynamic_cast<C*>(ap)) { // the object is a C } 回答2: You generally shouldn't need to know: struct A { virtual int generate_foo() = 0; }; struct B : A {

what will happen if fields with same name inherites from two sources(class and interface)

纵然是瞬间 提交于 2020-01-06 08:42:34
问题 valid code: interface Int1{ String str = "123"; } class Pparent{ String str = "123"; } class F extends Pparent implements Int1{ } invalid code add main method to F class: class F extends Pparent implements Int1{ public static void main(String[] args) { System.out.println(str); } } outs Exception in thread "main" java.lang.Error: Unresolved compilation problem: The field str is ambiguous at test.core.F.main(NullReferenceTest.java:45) I don't see striking differs beetwen both variants. I

what will happen if fields with same name inherites from two sources(class and interface)

拜拜、爱过 提交于 2020-01-06 08:42:03
问题 valid code: interface Int1{ String str = "123"; } class Pparent{ String str = "123"; } class F extends Pparent implements Int1{ } invalid code add main method to F class: class F extends Pparent implements Int1{ public static void main(String[] args) { System.out.println(str); } } outs Exception in thread "main" java.lang.Error: Unresolved compilation problem: The field str is ambiguous at test.core.F.main(NullReferenceTest.java:45) I don't see striking differs beetwen both variants. I