python类的简单记录

类:

class ClassName:
    '类的信息' #类文档字符串
    class_suite # 类的内容

类文档字符串可以通过ClassName.__doc__查看。class_suite由类成员、方法、数据属性组成。

类对象

类对象支持两种操作:属性引用和实例化。属性引用使用和Python中所有属性引用一样的标准语法:obj.name。类对象创建后,类命名空间中所有命名都是有效属性名。

class student:
    """
    简单的学生类
    """
    stuNum = 0
    def __init__(self, name, score):
        self.name = name
        self.score = score
        student.stuNum += 1
    
    def getNum(self):
        print('the number of students is %d' % student.stuNum)
    
    def getStudent(self):
        print('Name: ', self.name, ', score: ', self.score)

x = student('tom', 94)
x.getNum()
x.getStudent()
y = student('jane', 100)
y.getNum()
y.getStudent()
the number of students is 1
Name:  tom , score:  94
the number of students is 2
Name:  jane , score:  100
  • stuNum变量是一个类变量,它的值在这个类的所有实例之间共享。可以在内部类或者外部类使用student.stuNum访问。
  • _init_()方法是一种特殊的方法,称为类的构造函数,在类创建的时候调用该方法。
  • self代表类的实例,self在定义类的方法时是需要有的。

self代表类的实例,而不是类

类的方法和普通函数的区别是,类的方法必须有一个额外的第一参数名称,通常为self。

class test:
    '''
    测试类
    '''
    def prt(self):
        print(self)
        print(self.__class__)
        
t = test()
t.prt()
<__main__.test object at 0x0000025072E554C8>
<class '__main__.test'>

可以看出,self代表类的实例,表示当前对象的地址。

self不是python关键字,可以换成其他。(按照习惯,使用self)

class test:
    '''
    测试类
    '''
    def prt(aaa):
        print(aaa)
        print(aaa.__class__)
        
t = test()
t.prt()
<__main__.test object at 0x0000025072E4E448>
<class '__main__.test'>

继承

python支持类的继承。派生类定义如下:

class 派生类名(基类名):
    ...

python中继承的一些特点:

  • 如果在子类中需要父类的构造方法就显式的调用父类的构造函数,或者不重写父类的构造函数。
  • 在调用基类方法时,需要加上基类的类名前缀,且需要带上self参数变量。区别在于类中调用普通函数不需要带上self参数。
  • python总是首先找到对应类型的方法,如果它不再派生类中,则去基类中寻找。

python执行多类继承。

class 派生类名(base1, base2, ..., baseN):
    ...

需注意括号中父类的顺序,若父类中有相同的方法名,而在子类使用时为指定,python从左到右搜索。(即在子类中没找到的时候,从左往右查找父类中是否包含)

class people:
    name = ''
    age = 0
    # 定义私有属性
    __weight = 0
    # 构造函数
    def __init__(self, name, age, weight):
        self.name = name
        self.age = age
        self.__weight = weight
    
    def speak(self):
        print('%s 说: 我%d岁。' % (self.name, self.age))

# 单继承
class student(people):
    grade = ''
    def __init__(self, name, age, weight, grade):
        # 调用父类的构造函数
        people.__init__(self, name, age, weight)
        self.grade = grade
    
    # 重写父类的方法
    def speak(self):
        print('%s 说: 我%d岁,读%d年级。' % (self.name, self.age, self.grade))

# 另一个类
class speaker():
    topic = ''
    name = ''
    def __init__(self, name, topic):
        self.name = name
        self.topic = topic
    
    def speak(self):
        print('我是%s, 我演讲的主题是%s' % (self.name, self.topic))

# 多类继承
class sample(speaker, student):
    a = ''
    def __init__(self, name, age, weight, grade, topic):
        student.__init__(self, name, age, weight, grade)
        speaker.__init__(self, name, topic)

test = sample('tom', 10, 44, 4, 'class')
test.speak()
我是tom, 我演讲的主题是class

方法重写

可以在子类中重写父类的方法。

class parentClass:
    def myMethod(self):
        print('调用父类的方法')

class childClass(parentClass):
    def myMethod(self):
        print('调用子类的方法')

child = childClass()
child.myMethod()
super(childClass, child).myMethod()
调用子类的方法
调用父类的方法

super()函数时用于调用父类的一个方法。

类属性与方法

python内置类属性

  • _dict_ : 类的属性(包含一个字典,由类的数据属性组成)
  • _doc_ : 类的文档字符串
  • _name_ : 类名
  • _module_ : 类定义所在的模块(类的全名是_main_.className, 如果类位于一个导入模块mymod中,那么className.__module__等于mymod)
  • _bases_ : 类的所有父类构成元素(包含了一个由所有父类组成的元组)
class Employee:
   '所有员工的基类'
   empCount = 0
 
   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):
     print("Total Employee %d" % Employee.empCount)
 
   def displayEmployee(self):
      print("Name : ", self.name,  ", Salary: ", self.salary)
 
print("Employee.__doc__:", Employee.__doc__)
print("Employee.__name__:", Employee.__name__)
print("Employee.__module__:", Employee.__module__)
print("Employee.__bases__:", Employee.__bases__)
print("Employee.__dict__:", Employee.__dict__)
Employee.__doc__: 所有员工的基类
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: (<class 'object'>,)
Employee.__dict__: {'__module__': '__main__', '__doc__': '所有员工的基类', 'empCount': 0, '__init__': <function Employee.__init__ at 0x0000022D18F164C8>, 'displayCount': <function Employee.displayCount at 0x0000022D18F16558>, 'displayEmployee': <function Employee.displayEmployee at 0x0000022D18F165E8>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>}

python对象销毁

python使用引用计数来跟踪和回收垃圾。
在python内部记录着所有使用中的对象各自有多少引用。
一个内部跟踪变量,称为引用计数器。

当对象被创建时,就创建一个引用计数,当这个对象不再需要时,也就是,这个对象的引用计数变为0时,它被垃圾回收。但是回收不是立即的。由解释器在适当的时机,将垃圾对象占用的内存空间回收。

a = 10 # 创建对象10
b = a  # 增加引用<10>的计数
c = [b] #增加引用<10>的计数

del a # 减少引用
b = 100 # 减少引用<10>的计数
c[0] = -1 # 减少引用<10>的计数

垃圾回收机制不仅针对引用计数为0的对象,同样也可以处理循环引用的情况。循环引用:两个对象相互引用,但是没有其他变量引用它们,这种情况下,仅使用引用计数是不够的。python的垃圾收集器实际上是一个引用计数器和一个循环垃圾收集器。作为引用计数的补充,垃圾收集器也会留心被分配的总量很大的对象(即未通过引用计数销毁的对象)。这种情况下,解释器会暂停下来,试图清理有未引用的循环。

class test:
    def __init__(self):
        print('test创建')
    
    def __del__(self):
        class_name = self.__class__.__name__
        print(class_name, '销毁')

t1 = test()
t2 = t1
t3 = t1
print(id(t1), id(t2), id(t3))
del t1
del t2
del t3
test创建
2392715274760 2392715274760 2392715274760
test 销毁

类的私有属性

__private_attrs: 两个下划线开头,声明该属性为私有,不能再类的外部被使用或直接访问。再在类内部的方法中使用self.__private_attrs。

类的私有方法

__private_method: 两个下划线开头,声明该方法为私有方法,只能在类的内部调用,不能再类的外部调用。self.__private_method。

class Counter:
    __private_count = 0 #私有变量
    public_count = 0 
    
    def count(self):
        self.__private_count += 1
        self.public_count += 1
        print(self.__private_count)
    
    def __foo(self):
        print('私有方法')
    
    def foo(self):
        print('共有方法')

cnt = Counter()
cnt.count()
cnt.count()
print(cnt.public_count)
cnt.foo()
print(cnt.__private_count) #报错

1
2
2
共有方法



---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-6-fc834782acf3> in <module>
     19 print(cnt.public_count)
     20 cnt.foo()
---> 21 print(cnt.__private_count) #报错
     22 cnt.__foo()


AttributeError: 'Counter' object has no attribute '__private_count'
cnt.__foo() #私有方法
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-7-255781e816e6> in <module>
----> 1 cnt.__foo()


AttributeError: 'Counter' object has no attribute '__foo'

python不允许实例化的类访问私有数据,但可以使用object._className__attrName(对象名._类名__私有属性名)访问属性。

class test:
    __private = 'private'

t = test()
print(t._test__private)
private

单下划线、双下滑线、头尾双下划线:

  • _foo_: 定义的是特殊方法,一边是系统定义,类似_init_()。
  • _foo : 单下滑线开头,表示的是protected类型的变量,只能允许其本身与子类进行访问,不能用于from module import *
  • __foo : 双下滑线便是的是私有类型变量,只能允许这个类本身进行访问。

python类中的方法其实也是类属性的一种(动态属性),区别于类静态属性的是方法可以有参数输入。

类的专有方法

  • _init_ : 构造函数,在生成对象时调用
  • _del_ : 析构函数,释放对象时调用
  • _repr_ : 转化为供解析器读取的形式
  • _str_ : 用于将数值转化为适合人阅读的形式
  • _cmp_: 对象比较
  • _call_: 函数调用
  • _len_ : 获得长度
  • _add_ : 加运算
  • _sub_ : 减运算
  • _mul_ : 乘运算
  • _truediv_: 除运算
  • _mod_ : 求余运算
  • _pow_ : 乘方

运算符重载

python同样支持运算符重载,我们可以对类的专有方法进行重载。

class vector:
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def __str__(self):
        return 'Vector(%d, %d)' % (self.a, self.b)
    
    def __add__(self, other):
        return vector(self.a + other.a, self.b + other.b)

v1 = vector(2, 10)
v2 = vector(5, -2)
print(v1 + v2)
Vector(7, 8)

https://www.runoob.com/python3/python3-class.html

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值