python - 实例化 python 子类

标签 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())

谢谢你,为什么这不起作用的原因会很有帮助

最佳答案

你可能想这样做

ThisBook.__IsRequested = True

你不能这样做,因为 name mangling 。您可以编写另一个 setter 。

但是在你深入地编写大量的 getter 和 setter 之前,你应该意识到 Python 的方法是不使用它们。或者,如果需要其他逻辑,请使用 @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)

关于python - 实例化 python 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37346627/

相关文章:

python - 与队列一起使用时 "threads"列表变量的用途是什么

Python Pandas 逐行条件计算

java - 模拟已标记为可访问的私有(private)方法的返回值

java - 什么时候使用私有(private)构造函数有用?

JavaScript 私有(private)方法

python - 使用 Python 从 Azure Monitor 获取特定警报

python - 向 Django Rest 框架返回的 json 响应添加额外的属性

python - Pygame 和类

java - 如何访问在 java swing 中运行时实例化的对象的事件监听器

java - 为什么这不能是静态的?