目录
1.类属性和实例属性
# -*- coding:utf8 -*-
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
p1 = Person('Bob')
print(Person.count) # 1
p2 = Person('Alice')
print(Person.count) # 2
p3 = Person('Tim')
print(Person.count) # 3
class Test(object):
# 类属性
a = 100
def __init__(self, b):
# 实例属性
self.b = b
t = Test(100)
print("t.a = %d" % t.a, id(t.a)) # t.a = 100 1679129152
print("Test.a = %d" % Test.a, id(Test.a)) # Test.a = 100 1679129152
print("t.b = %d" % t.b) # t.b = 100
# print("Test.b = %d"Test.b) #error 无法通过(类名.属性名)的方式访问实例属性
t.a = 10
Test.a = 8
print(t.a, id(t.a)) # 10 1679126272
print(Test.a, id(Test.a)) # 8 1679126208
# 这两个不一样了
class Test():
a = 100
def __init__(self):
pass
t1 = Test()
# 打印修改前的类和实例对象中的变量,函数
print(Test.__dict__) # {'__module__': '__main__', 'a': 100, '__init__': <function Test.__init__ at 0x0000016133DB66A8>, '__dict__': <attribute '__dict__' of 'Test' objects>, '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None}
print(t1.__dict__) # {}
t1.a += 100
# 通过修改实例对象修改a的值吗,并打印类和实例对象中的变量,函数
print(t1.a) # 200
print(Test.a) # 100
print(Test.__dict__) # {'__module__': '__main__', 'a': 100, '__init__': <function Test.__init__ at 0x0000016133DB66A8>, '__dict__': <attribute '__dict__' of 'Test' objects>, '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None}
print(t1.__dict__) # {'a': 200}
class Test(object):
dict_a = {1: "leo"}
def __init__(self):
pass
t1 = Test()
print("--------Before--------")
print(t1.dict_a, id(t1.dict_a)) # {1: 'leo'} 2744597127960
print(Test.dict_a, id(Test.dict_a)) # {1: 'leo'} 2744597127960
print(t1.__dict__) # {}
print(Test.__dict__) # {'__module__': '__main__', 'dict_a': {1: 'leo'}, '__init__': <function Test.__init__ at 0x0000027F06BC6730>, '__dict__': <attribute '__dict__' of 'Test' objects>, '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None}
t1.dict_a[1] = "mike"
t1.dict_a[2] = "mike"
t1.dict_a = Test.dict_a
t1.dict_a[3] = 'make'
Test.dict_a[4] = 'make'
print("--------After-------")
print(t1.dict_a, id(t1.dict_a)) # {1: 'mike', 2: 'mike', 3: 'make', 4: 'make'} 2744597127960
print(Test.dict_a, id(Test.dict_a)) # {1: 'mike', 2: 'mike', 3: 'make', 4: 'make'} 2744597127960
print(t1.__dict__) # {'dict_a': {1: 'mike', 2: 'mike', 3: 'make', 4: 'make'}}
print(Test.__dict__) # {'__module__': '__main__', 'dict_a': {1: 'mike', 2: 'mike', 3: 'make', 4: 'make'}, '__init__': <function Test.__init__ at 0x0000027F06BC6730>, '__dict__': <attribute '__dict__' of 'Test' objects>, '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None}
2.类方法、实例方法、静态方法
# -*- coding:utf8 -*-
class Test():
count = 0
def __init__(self, name, age, number): # 实例方法,必须传入自身实例(self)作为参数
self.name = name
self.age = age
self.__number = number
def get(self):
print(self.count, self.name, self.age, self.__number)
@classmethod
def classMethod(cls): # 类方法,传入类实例,因为类方法可以传参一个cls参数,所以可以调用类的相关信息,而静态方法是一个独立的函数,也是可以调用的,这里为了简单,没有调用静态方法
# print(cls.count, cls.name, cls.age, cls.__number) # type object 'Test' has no attribute 'name'
print(cls.count) # 只能访问count
# print(cls.age) # type object 'Test' has no attribute 'age'
@staticmethod
def staticMethod(x=2): # 静态方法,不能传递和类或实例相关的参数,如cls或self,但可以传递其他参数
print(x)
def normalMethod(): # 普通方法
print("I am a noemal method Because I have nothing")
t1 = Test(1, 2, 3)
t2 = Test(4, 5, 6)
t1.get() # 0 1 2 3
t2.get() # 0 4 5 6
t1.classMethod() # 0
t1.staticMethod() # 2
# t1.normalMethod() # TypeError: normalMethod() takes 0 positional arguments but 1 was given
t2.classMethod() # 0
t2.staticMethod() # 2
# t2.normalMethod() # TypeError: normalMethod() takes 0 positional arguments but 1 was given
t = Test(7, 8, 9)
Test.get(t1) # 0 1 2 3
Test.get(t2) # 0 4 5 6
Test.classMethod() # 0
Test.staticMethod() # 2
Test.normalMethod() # I am a noemal method Because I have nothing
3.类的公有和私有
class JustCounter:
__secretCount = 0 # 私有变量
publicCount = 0 # 公开变量
def count(self):
self.__secretCount += 1
self.publicCount += 1
print(self.__secretCount)
counter = JustCounter()
counter.count() # 1
counter.count() # 2
print(counter.publicCount) # 2
# print(counter.__secretCount) # 报错,实例不能访问私有变量
class Site:
def __init__(self, name, url):
self.name = name # public
self.__url = url # private
def who(self):
print('name : ', self.name)
print('url : ', self.__url)
def __foo(self): # 私有方法
print('这是私有方法')
def foo(self): # 公共方法
print('这是公共方法')
self.__foo()
x = Site('菜鸟教程', 'www.runoob.com')
x.who()
# name : 菜鸟教程
# url : www.runoob.com
x.foo() # 正常输出
# 这是公共方法
# 这是私有方法
# x.__foo() # 报错, AttributeError: 'Site' object has no attribute '__foo'
4.类的继承
# 类定义
class people:
# 定义基本属性
name = ''
age = 0
# 定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
# 定义构造方法
def __init__(self, n, a, w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说: 我 %d 岁。" % (self.name, self.age))
# 单继承示例
class student(people):
grade = ''
def __init__(self, n, a, w, g):
# 调用父类的构函
people.__init__(self, n, a, w)
self.grade = g
# 覆写父类的方法
def speak(self):
print("%s 说: 我 %d 岁了,我在读 %d 年级" % (self.name, self.age, self.grade))
# 另一个类,多重继承之前的准备
class speaker():
topic = ''
name = ''
def __init__(self, n, t):
self.name = n
self.topic = t
def speak(self):
print("我叫 %s,我是一个演说家,我演讲的主题是 %s" % (self.name, self.topic))
# 多重继承
class sample(speaker, student):
a = ''
def __init__(self, n, a, w, g, t):
student.__init__(self, n, a, w, g)
speaker.__init__(self, n, t)
test = sample("Tim", 25, 80, 4, "Python")
test.speak() # 我叫 Tim,我是一个演说家,我演讲的主题是 Python, 方法名同,默认调用的是在括号中排前地父类的方法
5.运算符重载
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)
6.方法重写
class Parent: # 定义父类
def myMethod(self):
print('调用父类方法')
class Child(Parent): # 定义子类
def myMethod(self):
print('调用子类方法')
c = Child() # 子类实例
c.myMethod() # 调用子类方法, 子类调用重写方法
super(Child, c).myMethod() # 调用父类方法, 用子类对象调用父类已被覆盖的方法