Python-OOP学习笔记-1

1 python标准库:python 3.9.4

(1)format():

format(value[, format_spec])

将 value 转换为 format_spec 控制的“格式化”表示。format_spec 的解释取决于 value 实参的类型,但是大多数内置类型使用标准格式化语法:格式规格迷你语言

默认的 format_spec 是一个空字符串,它通常和调用 str(value) 的结果相同。

调用 format(value, format_spec) 会转换成 type(value).__format__(value, format_spec) ,所以实例字典中的 __format__() 方法将不会调用。如果搜索到 object 有这个方法但 format_spec 不为空,format_spec 或返回值不是字符串,会触发 TypeError 异常。

(2)hash():

hash(object)

返回该对象的哈希值(如果它有的话)。哈希值是整数。它们在字典查找元素时用来快速比较字典的键。相同大小的数字变量有相同的哈希值(即使它们类型不同,如 1 和 1.0)。

注解: 如果对象实现了自己的 __hash__() 方法,请注意,hash() 根据机器的字长来截断返回值。另请参阅 __hash__()

2 OOP学习

2.1 类与对象

1.类:对现实世界某一类事物的抽象

#教师类
class Teacher:

    #属性:名字,所授课程
    def __init__(self,name,course):
        self.name=name
        self.course=course

    #方法:打印名字和所授课程
    def show(self):
        print("Name: {}.course: {}".format(self.name, self.course))

2.对象:类的实例化

teacher1=Teacher("Jone","Math")
teacher2=Teacher("Hance","Computer")

在这个案例中,teacher是类,teacher1和teacher2是我们创建的具体的教师对象。当我们输入上述代码时,Python会自动调用默认的__init__初始构造函数来生成具体的对象。关键字self是个非常重要的参数,代表创建的对象本身。也就是self可以指代任意实例化对象。

2.2 类变量与实例变量

1.类变量:位于类内且函数体外,是所有实例化对象共有的。

2.实例变量:位于函数体内,是当前实例对象所有。

3.访问:

类变量:类名.变量名或者self.__class__.变量名。self.__class__自动返回每个对象的类名。

实例变量:对象名.变量名或者self.变量名.

 4. 例子:

#教师类
class Teacher:

    num=0 #类变量

    #属性:名字,所授课程
    def __init__(self,name,course):
        self.name=name
        self.course=course
        Teacher.num=Teacher.num+1

    #方法:打印名字和所授课程
    def show(self):
        print("Name: {}.course: {}".format(self.name, self.course)

#实例化
teacher1=Teacher("Jone","Math")
teacher2=Teacher("Hance","Computer")
#调用类对象
print(Teacher.num) #打印方式1
print(teacher1.__class__.num)#打印方式2

2.3 类方法

只属于类的方法,不属于具体实例。属于类的方法不使用self参数, 而使用参数cls,代表类本身,类方法常加上@classmethod修饰符做说明。

#教师类
class Teacher:

    num=0 #类变量

    #属性:名字,所授课程
    def __init__(self,name,course):
        self.name=name
        self.course=course
        Teacher.num=Teacher.num+1

    #方法:打印名字和所授课程
    def show(self):
        print("Name: {}.course: {}".format(self.name, self.course)
    
    # 定义类方法,打印老师的数量
    @classmethod
    def total(cls):
        print("Total: {0}".format(cls.num))


#实例化
teacher1=Teacher("Jone","Math")
teacher2=Teacher("Hance","Computer")
#调用类方法
Teacher.total()

2.4 类的私有属性和私有方法

类的私有属性和私有方法以双下划线__开头。私有属性或方法不能在类的外部被使用或直接访问。

class Teacher:

    num=0 #类变量

    #属性:名字,所授课程
    def __init__(self,name,course):
        self.__name=name ##将name声明为私有变量
        self.course=course
        Teacher.num=Teacher.num+1

    #方法:打印名字和所授课程
    def __show(self):##将show声明为私有属性
        print("Name: {}.course: {}".format(self.__name, self.course)
   
#实例化
teacher1=Teacher("Jone","Math")

#以下命令将报错
print(teacher1.__name)
teacher1.__show()

2.5 封装-@property装饰器

@property装饰器可将方法伪装为属性进行调用。

class Teacher:

    num=0 #类变量

    #属性:名字,所授课程
    def __init__(self,name,course):
        self.__name=name ##将name声明为私有变量
        self.course=course
        Teacher.num=Teacher.num+1

    #方法:打印名字和所授课程
    @property
    def show(self):##
        print("Name: {}.course: {}".format(self.__name, self.course)
   
#实例化
teacher1=Teacher("Jone","Math")
teacher1.show  
##给函数加上装饰器@property后,调用函数不用加括号可以直接调用函数 
#Name:Jone  course:Math

2.6 类的继承

先对基类(Base class)或父类(Parent class)定义,再通过class 子类名(父类名)来创建子类(Child class)。

一个例子,老师和学生同属学校成员,都有姓名和年龄的属性,但老师有所授课程这个专有属性,学生有分数这个专有属性。可以定义一个学校成员父类,两个子类。

# 创建父类学校成员SchoolMember
class SchoolMember:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def print(self):
        # 打印个人信息
        print('Name:"{}" Age:"{}"'.format(self.name, self.age), end=" ")


# 创建子类老师 Teacher
class Teacher(SchoolMember):

    def __init__(self, name, age, course):
        SchoolMember.__init__(self, name, age) # 利用父类进行初始化
        self.course = course

    # 方法重写
    def print(self):
        SchoolMember.print(self)
        print('course: {}'.format(self.course))


# 创建子类学生Student
class Student(SchoolMember):

    def __init__(self, name, age, score):
        SchoolMember.__init__(self, name, age)
        self.score = score

    def tell(self):
        SchoolMember.tell(self)
        print('score: {}'.format(self.score))


teacher1 = Teacher("Jone", 44, "Math")
student1 = Student("Mary", 12, 99)

teacher1.print() 
student1.print()

super()关键字调用父类方法:

在子类中可以通过super关键字来直接调用父类相应方法,简化代码。在下面例子中,学生子类调用了父类的print()方法。super().print()等同于SchoolMember.print(self)。当你使用Python super()关键字调用父类方法时时,注意去掉括号里self这个参数。

# 创建子类Student
class Student(SchoolMember):

    def __init__(self, name, age, score):
        SchoolMember.__init__(self, name, age)
        self.score = score

    def tell(self):
        super().print() # 等同于 SchoolMember.print(self)
        print('score: {}'.format(self.score))

今天的学习就到这里了,以上均为学习记录,仅供参考!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值