abstract-class

Why check if cls is the class in __subclasshook__?

不羁的心 提交于 2019-12-22 05:14:38
问题 In the Python standard library documentation, the example implementation of __subclasshook__ is: class MyIterable(metaclass=ABCMeta): [...] @classmethod def __subclasshook__(cls, C): if cls is MyIterable: if any("__iter__" in B.__dict__ for B in C.__mro__): return True return NotImplemented CPython's implementation of collections.abc indeed follows this format for most of the __subclasshook__ member functions it defines. What is the purpose of explicitly checking the cls argument? 回答1: _

Copy constructor for abstract class

穿精又带淫゛_ 提交于 2019-12-22 03:51:20
问题 I have an abstract class named AClass . In the same package I have AnotherClass , in which I have an ArrayList of AClass objects. In the copy constructor of AnotherClass I need to make a duplicate of AClass objects inside the ArrayList . The problem: I cannot create a copy constructor in AClass because it is an abstract class and I cannot know the name of the class which will inherit by AClass . Actually, in this project, no object will inherit from this class, but this project will be used

Relevance of 'public' constructor in abstract class

我只是一个虾纸丫 提交于 2019-12-22 01:39:24
问题 Is there any relevance of a 'public' constructor in an abstract class? I can not think of any possible way to use it, in that case shouldn't it be treated as error by compiler (C#, not sure if other languages allow that). Sample Code: internal abstract class Vehicle { public Vehicle() { } } The C# compiler allows this code to compile, while there is no way i can call this contructor from the outside world. It can be called from derived classes only. So shouldn't it allow 'protected' and

Accessing a method from a templated derived class without using virtual functions in c++?

人盡茶涼 提交于 2019-12-22 01:31:05
问题 How do I get around this? I clearly cannot make the value() method virtual as I won't know what type it is beforehand, and may not know this when accessing the method from b: class Base { public: Base() { } virtual ~Base() { } private: int m_anotherVariable; }; template <typename T> class Derived : public Base { public: Derived(T value) : m_value(value) { } ~Derived() { } T value() { return m_value; } void setValue(T value) { m_value = value; } private: T m_value; }; int main() { Base* b =

MVC3 View With Abstract Property

橙三吉。 提交于 2019-12-22 00:00:25
问题 Consider the following. Models public class DemographicsModel { public List<QuestionModel> Questions { get; set; } } //Does not work at all if this class is abstract. public /*abstract*/ class QuestionModel { //... } public class ChooseOneQuestionModel : QuestionModel { //... } public class ChooseManyQuestionModel : QuestionModel { //... } public class RichTextQuestionModel : QuestionModel { //... } public class TextQuestionModel : QuestionModel { //... } Controller [HttpPost] public

Handling classes inherent from abstract class and type parameter

那年仲夏 提交于 2019-12-21 15:03:06
问题 I have a base abstract class and its abstract type parameter as: public abstract class Database<T> where T : DatabaseItem, new() { protected List<T> _items = new List<T> (); protected virtual void Read (string[] cols) { T item = new T (); ... } public abstract class DatabaseItem { ... } Then I have number of children classes inherent from it: public class ShopDatabase : Database<ShopItem> {} public class ShopItem : DatabaseItem {} public class WeaponDatabase : Database<WeaponItem> {} public

Handling classes inherent from abstract class and type parameter

那年仲夏 提交于 2019-12-21 15:01:11
问题 I have a base abstract class and its abstract type parameter as: public abstract class Database<T> where T : DatabaseItem, new() { protected List<T> _items = new List<T> (); protected virtual void Read (string[] cols) { T item = new T (); ... } public abstract class DatabaseItem { ... } Then I have number of children classes inherent from it: public class ShopDatabase : Database<ShopItem> {} public class ShopItem : DatabaseItem {} public class WeaponDatabase : Database<WeaponItem> {} public

C++ override pure virtual method with pure virtual method

删除回忆录丶 提交于 2019-12-21 06:59:38
问题 Does it ever make sense to override a pure virtual method with another pure virtual method? Are there any functional differences or perhaps code style reasons to prefer one of the following options over the other? class Interface { public: virtual int method() = 0; }; class Abstract : public Interface { public: int method() override = 0; }; class Implementation : public Abstract { public: int method() override { return 42; } }; Versus: class Interface { public: virtual int method() = 0; };

Multiple Method Signatures For A Single Abstract Function/Abstract Overloading

▼魔方 西西 提交于 2019-12-21 05:46:06
问题 I have an abstract class for moving data from one database to another, and sometimes the data required to create the basic entries is different, due to the presence of a legacy table in the destination database which includes instructions for locating the data in the source. Obviously simplified, here is where the problem comes into play: abstract class foo { protected abstract function createBaseEntry($id); } Sometimes, I only need the one ID passed to this function, but in some cases I need

How do you declare an abstract method so that the parameter type (class) is always the Children class?

若如初见. 提交于 2019-12-21 05:31:19
问题 EDIT : see bottom First off I searched for an answer before asking this one, but as you can see with the title I have no idea how this is called and I will edit the question whenever I can. Please forgive me on this. I have the following abstract class : public abstract class ValidableDTO implements Serializable { public abstract boolean isValid(); public abstract boolean equals(ValidableDTO compared); // EDIT : new solution public abstract <T extends ValidableDTO> boolean equals(T compared);