abstract-class

Abstract class to get rid of redundant code in activities

主宰稳场 提交于 2020-01-02 08:55:12
问题 I created an abstract class to prevent redundant code in each of my activities. public abstract class MyGeneralizedActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } @Override public void attachBaseContext(Context newBase) { super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); } //... } I was perfectly fine by doing this: public class

Java abstract method with abstract parameter and inheritance

大兔子大兔子 提交于 2020-01-02 08:13:59
问题 I recently fumbled into a problem with an API and an implementation where the following type of code appeared: The API is an abstract class: public abstract class A { public A sum(A a) { System.out.println("A.sum(A) called"); return null; } } The implementation is a simple class: public class B extends A { public B sum(B b) { System.out.println("B.sum(B) called"); return null; } } When it comes to using it I write: public class Main { public static void main(String args[]) { B b = new B(); A

Can I pass arguments to a base constructor from a derived class's default constructor?

≡放荡痞女 提交于 2020-01-02 02:43:07
问题 Suppose I have an abstract base class Deck: public abstract class Deck { public List<Card> cards; public Deck(string[] values, string[] suits) {...} ... } and a derived class EuchreDeck: public class EuchreDeck : Deck { string[] values = new string[] { "9", "10", "J", "Q", "K", "A" }; string[] suits = new string[] { "clubs", "spades", "hearts", "diamonds" }; public EuchreDeck() : base(values, suits) // Error. {} ... } I want the ability to instantiate EuchreDeck and have the two string arrays

C++ abstract class parameter error workaround

别来无恙 提交于 2020-01-02 00:54:24
问题 The code snippet below produces an error: #include <iostream> using namespace std; class A { public: virtual void print() = 0; }; void test(A x) // ERROR: Abstract class cannot be a parameter type { cout << "Hello" << endl; } Is there a solution/workaround for this error other/better than replacing virtual void print() = 0; with virtual void print() = { } EDIT: I want to be able to pass any class extending/implementing the base class A as parameter by using polymorphism (i.e. A* x = new B() ;

Abstract class cannot be sealed in c#?

给你一囗甜甜゛ 提交于 2020-01-01 09:45:36
问题 I read somewhere "Abstract and Sealed modifiers are equivalent to a class which is static" I also found that "When you declare a static class, internally the compiler marks the class abstract and sealed, and creates a private constructor in the IL code" so, I decided to do this: static class A { public static void test() { Console.WriteLine("test"); } } Now, the class "A" cannot be inherited nor instantiated. So, let us write a class B using abstract to prevent instantiation and using sealed

What's the advantage of this indirect function call?

若如初见. 提交于 2020-01-01 09:07:13
问题 I found the following code in a library: class Bar { public: bool foo(int i) { return foo_(i); } private: virtual bool foo_(int i) = 0; }; Now I'm wondering: Why would you use this indirection? Could there be any reasons why the above would be better than the simple alternative: class Bar { public: virtual bool foo(int i) = 0; }; 回答1: This is the Non-Virtual Interface Idiom (NVI). That page by Herb Sutter has a good bit of detail about it. However, temper what you read there with what the C++

Copy constructor: deep copying an abstract class

拜拜、爱过 提交于 2020-01-01 08:26:20
问题 Suppose I have the following (simplified case): class Color; class IColor { public: virtual Color getValue(const float u, const float v) const = 0; }; class Color : public IColor { public: float r,g,b; Color(float ar, float ag, float ab) : r(ar), g(ag), b(ab) {} Color getValue(const float u, const float v) const { return Color(r, g, b) } } class Material { private: IColor* _color; public: Material(); Material(const Material& m); } Now, is there any way for me to do a deep copy of the abstract

What is the use of constructor in abstract class in php

瘦欲@ 提交于 2020-01-01 04:33:09
问题 I followed this link already before asking - Answer is in JAVA context and this for constructor in PHP . Since I am starter, my implementation of my PHP code in OOP concepts, so I am really willing to know about the usage and benefits or when to use constructor in PHP abstract class. Please provide an example in real world context to grab the concept better. PS - Although I am following PHP Manuals to understand OOP concepts but I am finding it little bit hard to understand, any help with the

@MustOverride annotation?

给你一囗甜甜゛ 提交于 2020-01-01 04:31:05
问题 In .NET, one can specify a "mustoverride" attribute to a method in a particular superclass to ensure that subclasses override that particular method. I was wondering whether anybody has a custom java annotation that could achieve the same effect. Essentially what i want is to push for subclasses to override a method in a superclass that itself has some logic that must be run-through. I dont want to use abstract methods or interfaces, because i want some common functionality to be run in the

implementing a cast operator in a generic abstract class

血红的双手。 提交于 2020-01-01 04:18:10
问题 I'm trying to be lazy and implement the cast operators in the abstract base class rather than in each of the derived concrete classes. I've managed to cast one way, but I'm unable to cast the other. I think it might not be possible, but wanted to pick the collective SO mind before giving up: public interface IValueType<T> { T Value{ get; set; } } public abstract class ValueType<T> : IValueType<T> { public abstract T Value { get; set; } public static explicit operator T(ValueType<T> vt) { if