面向对象的基础知识
面向过程:怎么做
面向对象:谁来做
1.确定职责 – 要做的事(方法)
2.对象内部封装不同方法(多个)
3.不同对象调用不同方法
类 对象
类:模板,对象都是根据这个模板创建出来的
类只需要有一个,对象可以有多个
类:属性(信息)和方法(你能干什么)
1.类名
2.属性
3.方法
有关面向对象和面向过程的区别,点击这里:https://blog.csdn.net/even160941/article/details/96125569
面向对象
1.面向对象的定义及其方法
-
面向对象
需要有意义的面向对象的代码,不是有了类就是面向对象
核心:类 对象 -
实例化:
方法:设计层面 函数:程序运形,过程式一种称谓
举例如下:
例一:
class Student():
name = 'even' #变量(属性): 数据成员
age = 0
def print_file(self):
print('name:' +self.name)
print('age:' +str(self.age))
student = Student()
student.print_file()
例二:
#1.如何定义类 class 类名(): pass
#2.类创建对象的过程称为实例化,把抽象的类具体化
# 对象名 = 类名(参数1、参数2...参数n)
#类:模板
class People():
name = '小张' #属性:类中的变量
age = 18
def eat(self):
print('吃')
def sleep(self):
print('睡')
p1 = People() #实例化:通过类创建对象
p2 = People()
print(id(p1))
print(id(p2))
print(p1.name)
print(p2.name)
p1.eat()
p2.eat()
2.类与对象
- 对象:
对象是现实世界或者思维世界中的实体在计算机中的反映,它将数据以及这些数据上的操作封装在一起 - 类:
一类事物的统称,对象是类具体化的产物,也就是实例化
不同的对象有什么不同特点:比如年龄,姓名
举例如下:
例一:
class Student():
name = 'even'
age = 18
def do_homework(self):
print('do homework')
student1 = Student()
student1.do_homework()
student2 = Student()
student2.do_homework()
student3 = Student()
student3.do_homework()
print(id(student1))
print(id(student2))
print(id(student3))
例二:
猫爱吃鱼,猫要喝水
实现如下:
class Cat():
def eat(self,name):
self.name = name
print('%s猫爱吃鱼' %self.name)
def drink(self,name):
print('%s猫爱喝水' %self.name)
tom = Cat()
tom.name = 'tom'
print(tom)
注意:self:哪一个对象调用的方法,self就是哪一个对象的引用,在类封装的方法内部,self就表示当前调用方法的对象自己
构造函数
例一:
class People():
def __init__(self):
print('self',self) #构造方法
p1 = People()
print('p1',p1)
例二:
class Student():
name = 'even'
age = 18
def __init__(self,name,age):
#构造函数
self.name = name
self.age = age
print('student')
def do_homework(self):
print('homework')
student1 = Student('zyw',10)
print(student1.name)
print(student1.age)
例三:
class Cat():
def __init__(self,new_name):
print('这是一个初始化方法')
self.name = new_name
def eat(self):
print('%s爱吃鱼' %self.name)
cat = Cat('tom')
print(cat.name)
hello_kitty = Cat('HK')
print(hello_kitty.name)
hello_kitty.eat()
注意:self.name(属性名) = 形参 接收外部传递的参数
Str方法
必须返回一个字符串
举例如下:
class People():
def __init__(self,name):
self.name = name
def __str__(self):
return '我是%s' %(self.name) #必须返回一个字符串
p1 = People('even')
print(p1)
练习:
需求
1.小明体重75.0公斤
2.小明每次跑步会减肥0.5公斤
3.小明每次吃东西体重会增加1公斤
需求
1.小明和小美都爱跑步
2.小美体重45.0公斤
3.小明体重75.0公斤
4.每次跑步都会减少0.5公斤
5.每次吃东西都会增加1公斤
class People():
def __init__(self,name,weight):
self.name = name
self.weight = weight
def __str__(self):
return '我是%s,我的体重%.2f' %(self.name,self.weight)
def run(self):
print('%s爱跑步' %self.name)
self.weight -= 0.5
def eat(self):
print('%s爱吃东西' %self.name)
self.weight += 1
xiaoming = People('小明',75)
xiaoming.run()
print(xiaoming)
xiaomei = People('小美',45)
xiaomei.eat()
print(xiaomei)
类属性和类方法
- 类属性:使用赋值语句在class关键字下定义
- 类方法:在类内部可以直接访问类属性或者其他类方法
举例如下:
class Student(object):
count = 0
def __init__(self,name):
self.name = name
Student.count += 1
@classmethod #装饰器
def Show_Student_count(self):
print('学生数量为: %d' %self.count)
student1 = Student('小张')
student2 = Student('小刘')
Student.Show_Student_count() #直接通过类调用类的方法
静态方法
举例:
class Cat(object):
@staticmethod
def call():
print('喵~')
Cat.call()
私有属性和私有方法
举例:
class Women():
def __init__(self,name):
self.name = name
self.__age = 18 #私有属性
def __secret(self): #私有方法
print('%s的年龄是%d' %(self.name,self.__age))
lily = Women('lily') #实例化一个对象lily
print(lily.name) #只能访问name属性
print(lily.__age) #私有属性和私有方法均不能访问和调用
lily.__secret()
我们可以通过以下的方式进行访问和修改:
class Student(object):
def __init__(self,name,score):
self.__name = name
self.__score = score
def get_name(self):
return self.__name
def get_score(self):
return self.__score
def set_score(self,score):
self.__score = score
tom = Student('Tom',100)
tom.set_score(99) #修改私有属性——成绩
print(tom.get_score())
print(tom.get_name())
注意:Python面向对象三大核心思想:封装,继承和多态,访问这里:https://blog.csdn.net/even160941/article/details/96183441