面向对象编程

一:什么是面向过程?

面向过程的核心是过程,过程就是把一件事分成几个步骤来完成,然后把每一个步骤给完成,这件事就完成了!

优点:复杂问题简单化

缺点:后期扩展性差,牵一发而动全身

二:什么是面向对象?

对象就是把数据和功能封装在一起,基于这种思想写的程序,就是整合程序

优点:扩展性比较好

缺点:简单问题复杂化

1:如何基于面向对象的思想编写程序
# 学生的数据
studnet_name = 'Alen'
student_age = 19
student_gender = 'mali'

# 学生的功能
def choose_course(name,age,gender):
    print(f'{name}-{age}-{gender}正在选课')


choose_course(student_name,student_age,student_gender)

2:用面向对象的思想来进行修改
student_obj = {	
    'name': 'alen',
   	'age': 18,
    'gender':'male',
    'choose_course':choose_course,
}

print(student_obj['name'])
student_obj['choose'](stu_obj)



3:如何基于面向对象来写程序?
学生的数据
student_name = 'alen'
student_age = 18
student_gender = 'male'

# 学生的功能
def choose_course(name,age,gender):
    print('{name}{age}{gender}正在选课')

choose_course(student_name,student_age,student_gender)
4:基于对象式的思想进行改写
def choose_course(obj):
    print('{name}{age}{male}正在选课')

student_obj = {
    'name':'alen',
    'age':'18'
    'gender':'male'
	'choose_course':choose_course
}

print(student_obj['name'])
print(student_obj['choose'](**student_obj))

三:在python中如何定义类?

# 类体代码会在类定义阶段立即执行,然后将产生的名字都放到类的名称空间中
Class Student:
    school = '日本校区'
    
    def choose_course(self):
        print("正在选课")
    
    print('正在选课!')
    
s1 = Student()
s2 = Student()
s3 = Student()
s1.age = 10
s1.name = '李四'
s1.gender = 'male'
s2.name = '小过'
s2.age = 88
s2.gender = 'female'
s3.name = '山鸡'
s3.age = 200
s3.gender = 'male'
print(s1.__dict__)
print(s2.__dict__)
print(s3.__dict__)
1:类的初始化方法
class Student:
    school = '东京校区'
    
    def choose_school(self):
        print("正在选课")
    
s1 = student()
s2 = student()
s1.name = 'alen'
s1.age = 18
s1.gender = 'male'
s2.name = 'alen_'
s2.age = 12
s2.gender = 'male'

class Student:
    school = '海南校区'
    
    def choose_school(self):
        print("正在选课")
        
s1 = Student()
s2 = Student()
def init(obj,x,y,z):
    obj.name = x
    obj.age = y
    obj.gender = z
    
init(s1,'alen',187,'male')
init(s2,'alen1',11,'female')

class Student:
    school = '山东校区'
    
    def __init__(self,name,age,gender):
        self.name=name
        self.age = age
        self.gender = gender
       
    def choose_course(self):
        print(f'{self.name}正在选课’)
              
s1 = Student('alex',18,'男')
s2 = Student('jack',88,'female')
2:属性查找

先从对象的内存里查找,没有去类的内存中查找

class Student:
    school = '南方校区'
    
    def __init__(x,y,z):
        self.name = x
        self.age = y
        self.gender = z
    
    def choose(self):
        print("{self.name}正在选课")
s1 = Student('jack',88,'female')
s2 = Student('alen',18,'male')
s3 = Student('arry',88,'male')
类中的数据是直接共享给所以对象使用
print(id(s1.school))
print(id(s2.school))
print(id(s3.school))
Student.school = '北方校区'
print(s1.school)
print(s2.school)
print(s3.school)

print(Student.choose)
print(s1.choose)
print(s2.choose)
print(s3.choose)
`

类中定义的函数时绑定给所以对象使用的,绑定谁,就应该有哪个对象来调用

对象.绑定方法()会把对象当作第一个参数传入

类.函数就是一个函数的玩法,没有自动传参的效果

print(Student.choose('alex'))
s1.choose()
s2.choose()
s3.choose()
posted @ 2020-09-08 19:48  为了等  阅读(101)  评论(0编辑  收藏  举报