Results 1 to 3 of 3
  1. Default Abstract base class vs. Base class


    Okay... in my python scripts for finding updated information, I want to create several classes to do it, each which must have a common interface.

    For example.

    Code:
    class Character:
      method GetName
      method GetImage
      method GetStats
    
    class Item:
      method GetName
      method GetImage
      method GetStats
    
    class Mob:
      method GetName
      method GetImage
      method GetStats
    In other words, I have many classes with common interfaces, but the implementation for each method is completely different. For example, the method "GetName" is going to be different for all of them since the strings for the name are in different locations in the WZ files. I want all of my classes to have the same interface to each of them so I can just focus on the implementation part and not the interface part.

    This sounds like inheritance, I think. Create a base class with all of these methods, then have them return a null. Then create a derived class from the base class to implement those methods. However, I've also heard about abstract base classes. How is an abstract parent class different from a derived class's parent?

  2. Default


    My python is a little rusty, as I haven't used it in years, but the basic concept is the same in C++ or C#. An abstract base class is the same as a base class except that it Must be derived from. (As in you can't create an instance of the abstract class.)

    Some sample code in C++ would be:

    class Bar
    {
    Bar(){}
    virtual ~Bar(){}

    virtual void GetName() = 0;
    };

    class Foo : public Bar
    {
    Foo(){}
    virtual ~Foo(){}

    //Must overwite GetName()
    void GetName();
    };

    In practice it is good to use abstract inheritance when you don't want someone to create an instance of the base class, however since I am assumeing that you are writeing the code for your own personal use, it really doesn't matter.

  3. Default


    In languages like C# and Java that don't allow multiple inheritance, there are also interfaces, which are like abstract classes except they're not allowed to specify any implementation. A class can only have one base class, but can implement as many interfaces as it wants.

    You should check out what the Pythonic way of doing an abstract base class is. With Python's duck typing and lack of access modifiers, I don't think it's even possible to make a class truely abstract. The best you can probably do is add documentation saying that it's abstract.

    Rather than returning null, which could have who knows what effect, you should throw an exception in the base class stub method.

  4.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •