面向对象OO
类和对象
属性和方法
继承、封装、多态
面向过程
模块、函数
参数传递
返回值
举一个例子:把一个大象装进冰箱中
面向过程OP
1、把冰箱门打开
2、把大象放进去
3、再把冰箱门带上
你可能必须知道怎么打开冰箱的门,怎么把大象放进去,怎么把冰箱门关上
面向对象OO
1、定义大象类和冰箱类
2、实例化一个大象
3、实例化一个冰箱
4、冰箱.开门
5、大象.走进冰箱
6、冰箱.关门
这些问题你都不需要关心,这是大象和冰箱的事情
继承:父类、字类(父类包含字类,例如 动物和大象)
子类能够继承父类所有的属性和行为
继承是能够无限继承下去的,但是在实际应用中应不超过3层,太过复杂的话不便于可读性和可维护性
1、类
定义类
用关键字 class
首字母要大写
class ClassDemo:
"""类的一些说明"""
pass
class ClassDemo:
"""类的一些说明"""
pass
myClass = ClassDemo()
print(myClass)
(main代表当前程序,ClassDemo是类,object是对象,at是在哪,后面是一串数字)
打印出myClass的类型和内存地址
class ClassDemo:
"""类的一些说明"""
pass
myClass = ClassDemo()
print(myClass)
print(type(myClass))
print(id(myClass))
将内存地址转化为16进制
print(hex(id(myClass)))
类的定义形式如下
class <类名>:
<语句1>
<语句2>
...
<语句3>
与函数定义相同,在使用类之前必须先定义类。类的定义一般放在脚本的头部。在 Python 中也可以在 if 语句的分支或者函数定义中定义类。下面的代码定义了一个 human 类,并定义了相关的属性。
class human: # 定义 human 类
name = '' # 定义 name 属性
age = 0 # 定义 age 属性
sex = '' # 定义 sex 属性
height = 0 # 定义 height 属性
weight = 0 # 定义 weight 属性
还可以通过继承的形式来定义
class <类名>(父类名):
<语句1>
<语句2>
...
其中圆括号中的父类名就是要继承的类。以下所示的代码是通过继承 human 类来生成一个新类。
class student(human): # 通过继承 human 类创建 student 类
name = '' # 定义 weight 属性
number = 0 # 定义 weight 属性
grade = 0 # 定义 weight属性
实例化:从一个类创建对象的一个过程
创建两个变量
myClass = ClassDemo()
myClass1 = ClassDemo()
print(myClass)
print(myClass1)
__init__调用已有的
class Person:
"""类的说明"""
def __init__(self):
# 构造函数
print('i am a Person object')
person = Person()
在类class里面定义的函数约定俗成叫方法
2、自定义方法
定义的时候用self,调用的时候不用self
class Person:
"""类的说明"""
def __init__(self):
# 构造函数
print('i am a Person object')
def say(self):
print("i am speaking")
person = Person()
person.say()
获取一个返回值
class Person:
"""类的说明"""
def __init__(self):
# 构造函数
print('i am a Person object')
def say(self):
print("i am speaking")
return 'haha'
person = Person()
print(person.say())
__init__只能return None,要不然报错,建议别写
3、定义属性
class Person:
height = 180
person = Person()
print(Person.height)
person = Person()
person1 = Person()
# print(person.say())
print(person.height)
print(person.height)
print(person1.height)
可以直接赋值
person = Person()
person1 = Person()
person.height = 150
person1.height = 170
# print(person.say())
print(person.height)
print(person1.height)
print(Person.height)
赋值的两个改变了,类里面的height没有被改变
打印两个对象的字典
print(person.__dict__)
print(person1.__dict__)
打印一下类的字典
(所以定义的height=180是定义到类这个层级上的)
print(Person.__dict__)
若注释掉赋值的,则计算机会到类里面去找,打印出180
person = Person()
person1 = Person()
# person.height = 150
# person1.height = 170
# print(person.say())
print(person.height)
print(person1.height)
print(Person.height)
print(person.__dict__)
print(person1.__dict__)
print(Person.__dict__)
实例属性没有的话,会被默认为同名的类属性
self.height 表示的是实例的类的属性
class Person:
height = 180 # 类属性
def __init__(self, height):
self.height = height # 实例属性
person = Person(150)
print(person.height) # 实例属性
print(Person.height) # 类的属性
实例属性的调用和多参数的传参
class Person:
def __init__(self, name, height):
self.height = height # 实例属性
self.name = name
def say(self):
print("i am " + self.name)
person1 = Person('小吴', 150)
person1.say()
person2 = Person('小红', 170)
person2.say()
不实例化,直接通过类名调用方法
class Person:
height = 180
name = '小曹'
def __init__(self, name, height):
self.height = height # 实例属性
self.name = name
def say(self):
print("i am " + self.name)
@classmethod # 装饰器
def classSay(cls):
print("i am" + cls.name)
Person.classSay()
4、访问权限的控制
通过外部可以改变类的属性的时候,这是非常不安全的
可以把变量的名字通过两个下划线开头 ,只在左边,让它变为私有变量,让其只在类的内部可以访问
self.__height = height
若想要访问,可以通过定义一个方法,
def getHeight(self):
return self.__height
对于一个类来说,双下划线开头的是一个私有的成员,只能被内部人访问
可以通过getHeight() 这个方法来访问
若两个访问同一个内容,访问内容一样,地址不一样
5、继承
还可以通过继承的形式来定义
class <类名>(父类名):
<语句1>
<语句2>
...
其中圆括号中的父类名就是要继承的类。以下所示的代码是通过继承 human 类来生成一个新类。
class student(human): # 通过继承 human 类创建 student 类
name = '' # 定义 weight 属性
number = 0 # 定义 weight 属性
grade = 0 # 定义 weight属性