实例化子类python

时间:2016-05-20 12:21:44

标签: python subclass private-methods

只是一个带有子类的简单类定义来显示继承

import datetime

class LibaryItem:        #The base class definition
    def __init__(self, t, a, i):  # initialiser method
        self.__Title = t
        self.__Author_Artist = a
        self.__ItemID = i
        self.__OnLoan = False
        self.DueDate = datetime.date.today()

    def GetTitle(self):
        return(self.__Title)
# All other Get methods go here

    def Borrowing(self):
        self.__OnLoan = True
        self.__DueDate = self.__DueDate + datetime.timedelta(weeks = 3)
    def Returning(self):
        self.OnLoan = False
    def PrintDetails(self):
        print(self.__Title, '; ', self.__Author_Artist,'; ',end='') # end='' Appends a space instead of a newline
        print(self.__ItemID, '; ', self.__OnLoan,'; ', self.__DueDate)

class Book(LibaryItem):# A subclass definition
    def __init__(self, t, a, i):  # Initialiser method
        LibaryItem.__init__(self, t, a, i) 
        # This statement calls the constructor for the base class

        self.__IsRequested = False
        self.__RequestBy = 0
    def GetIsRequested(self):
        return(self.__IsRequested)
class CD(LibaryItem):
    def __init__(self, t, a, i): # Initialiser method
        LibaryItem.__init__(self, t, a, i)
        self.__Genre = ""
    def GetGenre(self):
        return(self.__Genre)
    def SetGenre(self, g):
        self.__Genre = g

实例化子类

ThisBook = Book('Title', 'Author', 'ItemID')

ThisCD = CD('Title', 'Author', 'ItemID')

这是我的问题,我不明白为什么ThisBook对象的属性不会从False的默认值更改为True

# Using A method
print(ThisBook.GetIsRequested())

ThisBook.IsRequested = True
print(ThisBook.GetIsRequested())

感谢您解释为什么这不起作用的原因

2 个答案:

答案 0 :(得分:2)

你可能打算做

ThisBook.__IsRequested = True

由于name mangling而无法做到。你可以写另一个二传手。

但是在你深入研究很多吸气剂和制定者之前,你应该意识到pythonic方式是不使用它们。或者,如果需要其他逻辑,则使用@property decorator

class LibaryItem:
    def __init__(self, title, author, itemid):  # initialiser method
        self.title = title
        self.author = author
        self.itemid = itemid
        self._onloan = False
        self.duedate = datetime.date.today()

    @property
    def onloan(self):
        return self._onloan

    @onloan.setter
    def onloan(self, value):
        if value:
            self.duedate += datetime.timedelta(weeks = 3)
        self._onloan = value

    def __str__(self):
        return "%s; %s; %s; %s; %s" % (self.title, self.author, self.itemid, self.onloan, self.duedate)

class Book(LibaryItem):
    def __init__(self, title, author, itemid):
        LibaryItem.__init__(self, title, author, itemid) 
        self.requested = False
        self.requestby = 0

然后

ThisBook = Book('Title', 'Author', 'ItemID')
print(ThisBook.requested)
ThisBook.requested = True
ThisBook.onloan = True
print(ThisBook.duedate)

答案 1 :(得分:1)

您无法访问具有2个下划线前缀的字段(请参阅What is the meaning of a single- and a double-underscore before an object name?)。 你需要写一个合适的setter:

def SetIsRequested(self, val):
    self.__IsRequested = val

您遇到的是动态语言的典型愚蠢。课堂上的一个字段可以在没有被宣布的情况下设置,并且口译员可以通过指出你刚刚创建了一个名为" IsRequested"的新字段来帮助你。在你的班上为您节省一些打字费用,但是您需要花费翻译和IDE来防止您搞砸。