abstract-class

Instance of abstract class with hidden constructor

扶醉桌前 提交于 2019-12-21 05:22:22
问题 I need to create an instance of an abstract class with hidden constructor, the class looks like the following: public abstract class TestClass { /** * @hide */ public TestClass() { } } creating an concrete class does not work, because the constructor is not visible and calling the constructor via reflection API also doesn't work because the class is abstract. I need to create an instance of android.print.PrintDocumentAdapter.LayoutResultCallback 回答1: I ran into precisely the same problem (for

How do I declare an overloaded operator in an abstract class and override it in a derived non-abstract class?

扶醉桌前 提交于 2019-12-21 05:19:07
问题 I'm trying to write an abstract class with some pure virtual binary operators, which should be implemented by the derived class in order to accomplish operator polymorphism. Here's a simplified example: class Base { public: virtual const Base& operator+ (const Base&) const = 0; }; class Derived : public Base { public: const Derived& operator+ (const Derived&) const; }; const Derived& Derived::operator+ (const Derived& rvalue) const { return Derived(); } It doesn't matter right now what the

Change parameter type when implementing an abstract method

怎甘沉沦 提交于 2019-12-21 05:07:18
问题 Is there some way to define an abstract type as a parameter in an abstract method, and when that method is implemented in a derived class, you change the type of the method to accept a derived type? Code: public abstract class ProductBase { } public class SomeProduct : ProductBase { } public abstract class A { protected abstract void addProduct(ProductBase p); } // This works public class B : A { protected override void addProduct(ProductBase p) { // Do some work } } // This is what I'd like

With the advent of extension methods, are abstract classes less attractive?

这一生的挚爱 提交于 2019-12-21 04:18:16
问题 One interesting aspect of extension methods in .NET is the fact that you can apply them to interfaces. For me, it seems nice that I can define functionality near the interface without defining an abstract class that clutters the assembly. I know that abstract classes are not obsolete or anything, but how do you feel about utilizing this side effect in your code? Example: public static class IUserExtensions { public static bool IsCurrentUser(this IUser user) { return (HttpContext.Current.User

Are accessors in Python ever justified?

纵然是瞬间 提交于 2019-12-21 04:17:10
问题 I realize that in most cases, it's preferred in Python to just access attributes directly, since there's no real concept of encapsulation like there is in Java and the like. However, I'm wondering if there aren't any exceptions, particularly with abstract classes that have disparate implementations. Let's say I'm writing a bunch of abstract classes (because I am) and that they represent things having to do with version control systems like repositories and revisions (because they do).

Must a c++ interface obey the rule of five?

China☆狼群 提交于 2019-12-21 03:33:42
问题 What is the correct way to declare instantiation methods when defining an interface class? Abstract base classes are required to have a virtual destructor for obvious reasons. However, the following compilation warning is then given: "'InterfaceClass' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator", which is the 'rule of five'. I understand why the 'rule of five' should be obeyed in general,

Abstract keyword in PHP

懵懂的女人 提交于 2019-12-21 02:01:43
问题 Hey, I'm quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for? What situations would I use the abstract keyword in? How does it change the class/interface? 回答1: (Hope this is simple enough -- I don't think I can do better ^^ ) An abstract class can not be instanciated : you can only create another class that inherits from the abstract class, and instanciate

Python 2.6, 3 abstract base class misunderstanding

别说谁变了你拦得住时间么 提交于 2019-12-20 23:35:06
问题 I'm not seeing what I expect when I use ABCMeta and abstractmethod. This works fine in python3: from abc import ABCMeta, abstractmethod class Super(metaclass=ABCMeta): @abstractmethod def method(self): pass a = Super() TypeError: Can't instantiate abstract class Super ... And in 2.6: class Super(): __metaclass__ = ABCMeta @abstractmethod def method(self): pass a = Super() TypeError: Can't instantiate abstract class Super ... They both also work fine (I get the expected exception) if I derive

Python 2.6, 3 abstract base class misunderstanding

拈花ヽ惹草 提交于 2019-12-20 23:35:05
问题 I'm not seeing what I expect when I use ABCMeta and abstractmethod. This works fine in python3: from abc import ABCMeta, abstractmethod class Super(metaclass=ABCMeta): @abstractmethod def method(self): pass a = Super() TypeError: Can't instantiate abstract class Super ... And in 2.6: class Super(): __metaclass__ = ABCMeta @abstractmethod def method(self): pass a = Super() TypeError: Can't instantiate abstract class Super ... They both also work fine (I get the expected exception) if I derive

avoid implementation of a method which is there in interface - java

我的梦境 提交于 2019-12-20 21:21:12
问题 I have a interface like the below : public interface a { public void m1(); public void m2(); public void m3(); } public class A implements a { public void m3() { // implementation code } } I want to avoid implementation for the rest of the method. one way is to have all the methods without implementing in the class that tries to implement interface . How do I avoid this. Example code would help me understand better :) 回答1: public interface a{ public void m1(); public void m2(); public void m3