class Student:
#定义在类中方法外的熟属性
info = "nihao"
#实例属性:定义在初始化函数中的属性
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
#实例方法
def getage(self):
# print(info)
#实例方法只能调用实例属性
return self.age
#静态方法
@staticmethod
def staticfunc():
print("这是一个静态方法,不能调用实例属性,也不能调用实例方法")
#类方法
@classmethod
def classfunc(cls):
# print(info)
#这是一个静态方法,不能调用实例属性,也不能调用实例方法
pass
s1 = Student("xiao wang", 18, "nv")
#实例属性和方法,使用对象名打点进行调用
print(s1.classfunc())
#类属性 直接使用类名 打点调用
print(Student.info)
#类方法、静态方法
Student.classfunc()
Student.classfunc()
动态绑定属性和方法
class Student:
#定义在类中方法外的熟属性
info = "nihao"
#实例属性:定义在初始化函数中的属性
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def show(self):
print("name is:", self.name)
print("age is:", self.age)
atu1 = Student("xiao wang", 18, 0)
stu2 = Student("xiao li", 20,1)
#stu2动态绑定一个实例属性
stu2.number = 101
#动态绑定方法
def intorduce():
print("我是一个普普通通的函数")
stu2.func = intorduce #函数的赋值
stu2.func()
面向对象的三大特征
class Student:
# 首尾双下划线
def __int__(self, name,age,number):
self._name = name # protected
self__age = age # private
self.number = number #public
def _func1(self):
print("protected func")
def __func2(self):
print("private func")
def shouw(self):
print("public func")
属性的设置
class Student:
# 首尾双下划线
def __init__(self, name,age,number):
self._name = name # protected
self.__age = age # private
self.number = number #public
def _func1(self):
print("protected func")
def __func2(self):
print("private func")
def shouw(self):
print("public func")
# 使用@property 修改方法,将方法转化为属性使用
# 只能读取,不能修改
@property
def age(self):
return self.__age
# 将age属性设置为可写属性
@age.setter
def age(self, value):
self.__age = value
stu = Student("xiaowang", 19, 1)
stu.age = 89
print(stu.age)
继承
python当中的单继承:
class Person: #默认继承object
def __init__(self, name, age):
self.name = name
self.age = age
def show(self):
print("my name is:", self.name, "wo age is:", self.age)
# Student类继承person
class Student(Person):
# 编写初始化方法
def __init__(self, name, age, gender):
super().__init__(name, age) #调用父类的初始化方法
self.gender = gender
stu = Student("laoshi",18,1)
stu.show()
python当中的多继承
class FatherA():
def __init__(self, name):
self.name = name
def show(self):
print("this is class father A")
class FatherB():
def __init__(self, age):
self.age = age
def show(self):
print("this is class father B")
class Son(FatherA, FatherB):
def __init__(self, name, age, number):
#需要调用两个父类的构造犯法
FatherA.__init__(self, name)
FatherB.__init__(self, age)
self.nuber = number
son = Son("xiaowang",18,1)
son.show()
方法重写:
class Person():
def __init__(self, name):
self.name = name
def show(self):
print("this is class person")
class Student(Person):
def __init__(self, name, number):
Person.__init__(self, name)
self.num = number
def show(self):
Person.show(self)
print("this is class student")
class Doctor(Person):
def __init__(self,name, level):
Person.__init__(self,name)
self.level = level
def show(self):
super().show()
print("this is class doctor")
stu = Student("xiao li", 12)
stu.show()
Python中的多态:
class Person():
def eat(self):
print("人什么都吃")
class Cat():
def eat(self):
print("猫吃鱼和老鼠")
class Dog():
def eat(self):
print("够吃肉")
# 这三个类中有一个同名的方法,eat
# 编写函数
def fun(obj):
obj.eat() # 通过对象调用eat方法
pr = Person()
cat = Cat()
# pyton 中的多态,不关心对象的数据类型,只关系堆笑是否具有同名方法
fun(pr)
fun(cat)
import copy
class CPU():
pass
class Disk():
pass
class Computer():
def __init__(self, cpu, disk):
self.cpu = cpu
self.disk = disk
cpu = CPU()
disk = Disk()
com = Computer(cpu,disk)
#类对象的赋值,指向的是同一个对象
com1 = com
print(com, "com子对象的内存地址:", com.cpu, com.disk)
print(com1, "com1子对象的内存地址:", com1.cpu, com1.disk)
#类对象的浅拷贝, computer2是新产生的对象。computer2的子对象,cpu、disk不变
com2 = copy.copy(com)
print(com, "com子对象的内存地址:", com.cpu, com.disk)
print(com2, "com2子对象的内存地址:", com2.cpu, com2.disk)
#类对象的浅拷贝, computer3是新产生的对象。computer3的子对象,cpu、disk也会重新产生
com3 = copy.deepcopy(com)
print(com, "com子对象的内存地址:", com.cpu, com.disk)
print(com3, "com2子对象的内存地址:", com3.cpu, com3.disk)