编程分为面向过程和面向对象,这两者是相辅相成的,python作为一门面向对象语言,类和对象的概念十分重要。
几个基本知识点
# python 中,一切皆对象
# #int是一种数据类型,简称类,1,2,3称为实例或者对象
# 类的组成,类属性,实例方法,静态方法,类方法,初始化方法
创建一个类
class Student: #类的命名,一般有一个或者多个单词组成,单词首字母大写,其余小写
place='chn' #在类里边定义变量,称为类属性
#初始化方法
def __init__(self,name,age):
self.name = name #self.name称为实例属性,赋值操作,将name的值赋给实例属性
self.age = age
#实例方法
def eat(self):
print('吃')
#静态方法
@staticmethod
def st():
print('st')
#类方法
@classmethod
def cm(cls):
print('cm')
print(Student)
对象的创建
# 对象的创建,又称类的实例化,有了实例就可以调用类中的内容
stu=Student('luchunchng',11)
print(stu.name)
print(stu.age)
print(stu.place)
stu.st()
stu.cm()
stu.eat() #实例方法的调用
Student.eat(stu) #第二种调用方法
类属性
#类属性
print(Student.place)
stu1=Student('li',11)
stu2=Student('lu',22)
print(stu1.place) # 不同的类对象,place属性值没有改变
print(stu2.place)
Student.place='鸦鸿桥'
print(stu1.place) # 改变place属性的值
print(stu2.place)
类方法和静态方法的调用
#类方法的调用 ,直接用类名定义
Student.cm()
#静态方法
Student.st()
动态绑定属性和方法
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print('西红柿炒蛋')
@classmethod
def cm(cls):
print('cm')
#
stu1=Student('lu',11)
stu2=Student('li',13)
print(stu1.age)
print(stu2.age)
stu1.cm()
#动态绑定属性
stu1.gender='女'
print(stu1.gender)
#动态绑定方法
def show():
print('这是一个函数')
stu2.show=show()
stu2.show
面向对象的三大特征-封装,继承,多态
封装
class Student:
def __init__(self,name,age):
self.name=name
self.__age=age
stu=Student('lu',12)
print(stu.name)
#print(stu.__age) #出错,不让访问
print(dir(stu))
print(stu._Student__age) #也可以访问(不要访问,靠自觉性)
继承
#继承及其实现方式
class Person(object): # 继承自object类
def __init__(self,name,age):
self.name=name
self.age=age
def inof(self):
print(self.name,self.age)
class Teacher(Person):
def __init__(self,name ,age,teac_year):
super().__init__(name,age)
self.teac_year=teac_year
def inof(self): # 方法重写
super().inof()
print('教龄',str(self.teac_year)+'年')
class Student(Person):
def __init__(self,name ,age,stu_year):
super().__init__(name,age)
self.stu_year=stu_year
tea=Teacher('lu',12,23)
#print(tea.age,tea.name,tea.teac_year)
tea.inof()
# 多继承
class A(object):
pass
class B(object):
pass
class C(A,B): #C继承了A,B
pass
# 重写
class Person():
def __init__(self,name,age):
self.name = name
self.age = age
def info(self):
print(self.name,self.age)
class Student(Person):
def __init__(self,name,age,xuehao):
super().__init__(name,age)
self.xuehao = xuehao
def info(self): # 重写父类中的方法
print(self.name,self.age,self.xuehao)
p = Person('lu',12)
s = Student('li',32,12)
s.info()
object类
# object类
# - object类是所有类的父类,因此所有类都有object类的属性和方法。
# ·内置函数dir()可以查看指定对象所有属性
# -Object有一个_str_()方法,用于返回一个对于“对象的描述”
# 对应于内置函数str()经常用于print()方法,帮我们查看对象的信息,所以我们经常会对_str_()进行重写
class Stuent:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self): #重写__str__(返回对象的描述) 方法
return '我叫{0},我今年岁{1}'.format(self.name,self.age)
stu=Stuent('lu',66)
print(stu) #默认调用__str__方法
多态
#多态(多态这里这个老师讲的不够清楚,以后有机会补充)
class Animal(object):
def eat(self):
print('动物吃')
class Cat(Animal):
def eat(self):
print('猫吃屎')
class Dog(Animal):
def eat(self):
print('狗也吃屎')
class Person:
def eat(self):
print('人也吃屎')
def fun(obj):
obj.eat()
fun(Animal()) #只要有eat()方法,就能被调用
fun(Dog())
fun(Cat())
fun(Person())
查看对象的属性和字典
#查看对象的属性和字典
class A(object):
pass
class B(object):
pass
class C(A,B): #C继承了A,B
def __init__(self,name,age):
self.name=name
self.age=age
a=C('lu',33)
print(a.__dict__) #{'name': 'lu', 'age': 33} 实例对象的属性字典
print(a.__class__) #<class '__main__.C'> 输出对象所属的类型
print(C.__base__) #类的基类
print(C.__bases__) #C类的父类类型
print(C.__mro__) #C类的层次结构
print(A.__subclasses__()) #子类列表
特殊方法
a=10
b=20
c=a+b
print(c)
d=a.__add__(b) #这是底层原理
print(d)
class Student:
def __init__(self,name):
self.name=name
def __add__(self, other): #在这里重写了__add__方法,所以对象属性可以相加了
return self.name+other.name
def __len__(self): #在这里重写了__len__函数,所以可以显示对象属性长度
return len(self.name)
stu1=Student('lu')
stu2=Student('li')
s=stu1+stu2
print(s)
str='asdf'
print(len(str))
print(str.__len__())
print(stu1.__len__())
# __new__和__init__ (new的作用是创建对象,init是输入实例属性)
class Person:
def __new__(cls, *args, **kwargs): # 先执行__new__
obj=super().__new__(cls)
return obj
def __init__(self,name,age): # 初始化赋值
self.name=name
self.age=age
stu1=Person('ll',23)
类的赋值,深拷贝和浅拷贝
#类的赋值
class Cpu:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
c1=Cpu()
c2=c1
print(id(c1))
print(id(c2)) # 只是形成两个变量,其实还是一个对象
# 类的浅拷贝 不拷贝子对象
print('-----------------------------------------')
cpu=Cpu()
disk=Disk()
comp1=Computer(cpu,disk)
import copy
comp2=copy.copy(comp1) #拷贝
print(comp1,comp1.cpu,comp1.disk)
print(comp2,comp2.cpu,comp2.disk)
#
# 深拷贝 拷贝子对象
print('--------------------------------------------')
comp3=copy.deepcopy(comp1)
print(comp1,comp1.cpu,comp1.disk)
print(comp3,comp3.cpu,comp3.disk)