zoukankan      html  css  js  c++  java
  • 学习python的类

    class Book:
        #类的成员变量
        hello = "world"
    
        #类初始化转换器,类在外部初始化的时候 把title的值传给__title  类似的举例
        def __init__(self,title,price):
            self.__title = title
            self.__price = price
    
        #定义属性 下面第一个price函数是 getter(获取器方法) 返回__price的值
        @property
        def price(self):
            return self.__price
    
        @price.setter
        # 设定器方法
        def price(self, price):
            self.__price = price
    
        @price.deleter
        # 删除器方法
        def price(self):
            self.__price = 0
    
        #类的成员方法
        def print_content(self,num):
            print(self.__title, self.__price * 2)
    
    #定义一个子类继承Book
    class ColorBook(Book):
    
        #子类成员变量
        color = '红色'
    
        #重写父类的方法
        #super表示代码全用父类的 然后在加了一条打印颜色的
        def print_content(self,num):
            super().print_content(num)
            print(self.color)
    
    #类实例化
    book = Book('标题',1000)
    #调用父类函数
    print(book.print_content(2))
    #父类设置器
    book.price = 1111
    print(book.print_content(2))
    
    #父类删除器
    del(book.price)
    
    
    #子类初始化对象
    book1 = ColorBook('标题',1001)
    #调通子类函数
    print(book1.print_content(3))
  • 相关阅读:
    注释
    选择器
    SQL语句中查找字符的位置
    SQL语句中截取字符串Substr
    IDENTITY(函数)
    SQL Server设置主键自增长列
    SQL语句操作ALTER
    表的主键
    南京夜市
    夜班
  • 原文地址:https://www.cnblogs.com/xlovepython/p/15458564.html
Copyright © 2011-2022 走看看