面向对象

面向过程:核心是过程二字

过程的终极奥义就是将程序流程化

过程是‘‘流水线’’,是用来分步骤解决问题的

面向对象:核心是‘‘对象’’二字

对象的终极奥义就是将程序‘‘整合’’            

对象就是‘‘容器’’,用来盛放数据与功能的

类也是‘‘容器’’,该容器用来存放同类对象共有的数据与功能

类是对象相似数据与功能的集合体,所以类体中最常见的事变量与函数的定义,但是类体中其实是可以包含任意其他代码的  主注意:类体代码会在定义阶段就会立即执行

类定义:

 

class  Student:  # 固定class +驼峰体变量名

  stu_school='oldboy'   #定义变量(重合的部分)

  def tell_stu_info(stu_obj):

    print('学生信息:名字:%s 年龄:%s 性别:%s'(     
      stu_obj['stu_name'],

      stu_obj['stu_age'],

      stu_obj['stu_gender']

))

   def set_info(stu_obj x,y,z):

    stu_obj['stu_name']=x

    stu_obj['stu_age']=y

    stu_obj['stu_gender']=z

print(Sutdent.stu_school)   #Student.__dict__['stu_school']

print(Sutdent.set_info)  #Sutdent.__dict__['set_info']

 

Sutdent.x=1111 #Sutdent.__dict__[x]=111  增加值

print(Sutdent.__dict__)

 

 

 

    

 

产生对象

属性访问的语法

1.访问数据属性

 

peint(Sutdent.stu_school)#Sutdent.__dict__['stu_school']

 

 

 

2.访问函数属性

 

peint(Sutdent.set_info)#Sutdent.__dict__['set_info']

 

 

 

 

二、产生类以后再调用类产生对象

 

stu1_obj=Student()#造出来一个空字典

stu2_obj=Student()

stu3_obj=Student()

 

#print(stu1_obj.__dict__)

#print(stu2_obj.__dict__)

#print(stu3_obj.__dict__)

 

stu1_obj.stu_name='egon'     #stu1_obj.__dict__['stu_name']='egon'  用到的是增加功能 #Sutdent.__dict__[x]=111  增加值

stu1_obj.stu_age=18             #stu2_obj.__dict__['stu_age']=18

stu1_obj.stu_gender='male'    #stu3_obj.__dict__['stu_gender']='male'

print(stu1_obj.__dict__)#把以上姓名年龄性别存入空字典中  但是如果学生个数过多会出现代码重复

 

 

 

 

 

一、类中存放的事数据和功能,类可以访问①类的数据属性②类的函数属性print(Student.tell_stu_info)     print(Student.set_info)

二、但其实类中的对象是给对象用的1.类的数据属性收共享给所有对象用的,大家访问的地址都一样

 

 

学生的数据:

 

stu_name='egon'

stu_age=18

stu_gender='male'

#学生的功能

def tell_stu_info():

print('学生信息:名字:%s年龄:%s性别:%s%(stu_name,stu_age,atu_gender)')

 def set_info()

def set_info():

stu_name='EGON'

stu_age=19

stu_gender'female'

 

 

 

#课程的数据

 

course_name='python'

course_period='6mons'

course_score=10

 

 

 

#课程的功能

 

def tell_coure_info():

print(''课程名字:名字:%s周期:%s学分:%s%(

course_name,course_period,course_score))

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

\

posted @ 2023-08-22 09:54  朱饱饱  阅读(14)  评论(0)    收藏  举报