abstract

C# abstract class static field inheritance

送分小仙女□ 提交于 2019-11-30 08:24:50
I feel like I skipped a C# class or two, but here's my dilemma: I have an abstract class from which I derive multiple child classes. I know for sure that for each of the child classes I will have a constructor that needs a certain static object as a model and this object will be different for each of the child classes. My first approach was to make a public static object in the abstract parent class and then, before I start creating any instances of the child classes, I would modify it for each of them, but it turns out that this way I actually make only ONE static object, for the abstract

Java cloning abstract objects

柔情痞子 提交于 2019-11-30 07:26:07
问题 I'm wondering if there is any way to do the following. I have an abstract class, Shape , and all its different subclasses and I want to override the clone method. All I want to do in the method is create a new Shape from the toString() of the current one. Obviously I can't do the following because Shape is abstract. Is there another way to do this because overriding clone in every subclass just for a simple name change seems useless. public abstract class Shape { public Shape(String str) { //

Can I create Java-like interfaces in Perl?

大兔子大兔子 提交于 2019-11-30 05:03:28
I understand that Perl's OO model is rather primitive; it is, in most respects, essentially a namespace hack. Nevertheless, I wonder if it is possible to create something like an "interface?" My goal is to have a base class from which others are extended whose principal purpose is to make mandatory the implementation of certain methods (by name is fine, no signature necessary) by those subclasses. I don't really care if it's a "purely virtual" class (like an "interface" in Java) or a concrete class with actual implementational stubs for those methods in the superclass, but what I want is to

C#: Creating an instance of an abstract class without defining new class

自闭症网瘾萝莉.ら 提交于 2019-11-30 04:39:34
I know it can be done in Java, as I have used this technique quite extensively in the past. An example in Java would be shown below. (Additional question. What is this technique called? It's hard to find an example of this without a name.) public abstract class Example { public abstract void doStuff(); } public class StartHere{ public static void main(string[] args){ Example x = new Example(){ public void doStuff(){ System.out.println("Did stuff"); } }; x.doStuff(); } } Now, my main question would be, can this also be done in C#, and if so, how? With lamba expressions and class initializers

Abstract constants in PHP - Force a child class to define a constant

笑着哭i 提交于 2019-11-29 22:50:24
I noticed that you can't have abstract constants in PHP. Is there a way I can force a child class to define a constant (which I need to use in one of the abstract class internal methods) ? Baba A constant is a constant ; there is no abstract or private constants in PHP as far as I know, but you can have a work around: Sample Abstract Class abstract class Hello { const CONSTANT_1 = 'abstract'; // Make Abstract const CONSTANT_2 = 'abstract'; // Make Abstract const CONSTANT_3 = 'Hello World'; // Normal Constant function __construct() { Enforcer::__add(__CLASS__, get_called_class()); } } This

Java: static field in abstract class

家住魔仙堡 提交于 2019-11-29 21:15:45
I just start out with an example, that explains it best: public abstract class A{ static String str; } public class B extends A{ public B(){ str = "123"; } } public class C extends A{ public C(){ str = "abc"; } } public class Main{ public static void main(String[] args){ A b = new B(); A c = new C(); System.out.println("b.str = " + b.str); System.out.println("c.str = " + c.str); } } This will print out: b.str = abc c.str = abc But I would like a solution where each subclass that instantiate the super class, has their own class variable, at the same time I want to be able to reference that

Best way to declare an interface in C++11

为君一笑 提交于 2019-11-29 19:36:25
As we all know, some languages have the notion of interfaces. This is Java: public interface Testable { void test(); } How can I achieve this in C++ (or C++11) in most compact way and with little code noise? I'd appreciate a solution that wouldn't need a separate definition (let the header be sufficient). This is a very simple approach that even I find buggy ;-) class Testable { public: virtual void test() = 0; protected: Testable(); Testable(const Testable& that); Testable& operator= (const Testable& that); virtual ~Testable(); } This is only the beginning.. and already longer that I'd want.

Flatten a list of lists

*爱你&永不变心* 提交于 2019-11-29 18:58:09
问题 I have to write a function that flattens a list of lists. For example flatten [] = [] or flatten [1,2,3,4] = [1,2,3,4] or flatten [[1,2],[3],4,5]] = [1,2,3,4,5] I'm having trouble with the being able to match the type depending on what is given to the flatten function. Here's what I have: data A a = B a | C [a] deriving (Show, Eq, Ord) flatten::(Show a, Eq a, Ord a)=>A a -> A a flatten (C []) = (C []) flatten (C (x:xs) ) = (C flatten x) ++ (C flatten xs) flatten (B a) = (C [a]) From what I

Run-time Polymorphism in Java without “abstract”?

拥有回忆 提交于 2019-11-29 17:24:25
I was going over the official Oracle tutorial where it introduces the idea of polymorphism with the example of a class hierarchy of 3 classes; Bicycle being the superclass, and MountainBike and RoadBike being 2 subclasses. It shows how the 2 subclasses override a method "printDescription" declared in Bicycle, by declaring different versions of it. And finally, toward the end, the tutorial mentions the Java Virtual Machine (JVM) calls the appropriate method for the object that is referred to in each variable. But, nowhere does the tutorial on polymorphism mention the concept of "abstract"

protected data in abstract class

柔情痞子 提交于 2019-11-29 17:16:33
问题 My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters used only. Now, I understand we want to shield data from direct manipulation by casual users of the class, and that public data members in general are a questionable practice. I have looked at "Java protected fields vs public getters" ( Java protected fields vs public getters ), but I still am dubious that: protected int i;