zoukankan      html  css  js  c++  java
  • python day24

    面向对象

    类:把一类事物的想通动作跟特征整合到一起就是类,类是一个抽象的概念

    对象:就是基于类而创建的一个具体的事物(具体存在的),也是特征和动作整合到一起

    面向对象过程分析,例如:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 def school(name,addr,type):
     4     def kao_shi(school):
     5         print("%s 学校正在考试" % school['name'])
     6 
     7     def zhao_sheng(school):
     8         print('%s %s学校正在招生' % (school[type],school['name']))
     9     def init(name,addr,type):
    10         sch = {
    11             'name': name,
    12             'addr': addr,
    13             'type': type,
    14             'kao_shi': kao_shi,
    15             'zhao_sheng': zhao_sheng,
    16         }
    17         return sch
    18     return init(name,addr,type)
    19 s1 = school('oldboy','沙河','私立学校')
    20 print(s1['name'])
    21 s1['kao_shi'](s1)
    View Code

    类相关知识:

    在Python中声名函数和声名一个类很相似

    声名函数:

     1 def functionName(args):

    3   '函数文档字符串' 

    声名类:命名规范:首字母大写

     1 '''
     2 class  类名:
     3   文档字符串
     4   类体
     5 '''
     6 #我们创建一个类:
     7 class Data:
     8   pass
     9 #用类Data实例化出一个对象d1
    10 d1 = Data()

    实例:实例只有数据属性,没有函数属性(方法)

     1 #类属性:1、数据属性 2、函数属性(方法)
     2 class Chinese:
     3     dang=''
     4     def sui_di_tu_tan():
     5         print("朝着墙上就是一口痰")
     6 
     7     def cha_dui(self):
     8         print('插到了前边')
     9 
    10 print(Chinese.dang)#类属性.
    11 Chinese.sui_di_tu_tan()
    12 print(dir(Chinese))
    13 #查看类的属性字典
    14 Chinese.__dict__['dang']
    15 Chinese.__dict__['cha_dui'](1)
    View Code

    特殊的类属性:

    类的相关知识:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 # class Chinese:
     4 #     '这是一个中国人的类'
     5 #     pass
     6 # p1 = Chinese()
     7 # print(p1)
     8 
     9 #类属性:1、数据属性 2、函数属性(方法)
    10 class Chinese:
    11     dang=''
    12     def sui_di_tu_tan():
    13         print("朝着墙上就是一口痰")
    14 
    15     def cha_dui(self):
    16         print('插到了前边')
    17 
    18 # 特殊的类属性
    19 print(Chinese.__name__)
    20 print(Chinese.__doc__)
    21 print(Chinese.__base__)
    22 #
    23 # print(Chinese.dang)#类属性.
    24 # Chinese.sui_di_tu_tan()
    25 # print(dir(Chinese))
    26 # #查看类的属性字典
    27 # Chinese.__dict__['dang']
    28 # Chinese.__dict__['cha_dui'](1)
    View Code

    对象相关知识以及增删改查:

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 # class Chinese:
     4 #     '这是一个中国人的类'
     5 #     pass
     6 # p1 = Chinese()
     7 # print(p1)
     8 
     9 #类属性:1、数据属性 2、函数属性(方法)
    10 class Chinese:
    11     dang=''
    12     #实例化过程本质上就是调用__init__函数的运行
    13     def __init__(self,name,age,gender):
    14         print("开始实例化")
    15         self.mingzi = name
    16         self.nianling = age
    17         self.xingbie = gender
    18         print("实例化结束")
    19     def sui_di_tu_tan():
    20         print("朝着墙上就是一口痰")
    21 
    22     def cha_dui(self):
    23         print('插到了前边')
    24     def play_boy(self,ball):
    25         print("%s 正在打%s" %(self.name,ball))
    26 
    27 # p1 = Chinese('sd',28,'man')
    28 
    29 # print(p1.__dict__)
    30 # print(p1.__dict__['xingbie'])
    31 # print(p1.mingzi)
    32 # print(p1.dang)
    33 # #调用方法:
    34 #
    35 # Chinese.sui_di_tu_tan()
    36 # #更改
    37 # Chinese.dang='shidong'
    38 # #查看
    39 # print(Chinese.dang)
    40 # #增加
    41 # Chinese.dang='人'#增加数据属性
    42 #
    43 #
    44 # print(Chinese.dang)
    45 # print(p1.dang)
    46 # #删除
    47 # del Chinese.dang
    48 # print(Chinese.__dict__)
    49 # print(p1.__dict__)
    50 p1 = Chinese('sd',28,'man')
    51 def eat_food(self,food):
    52     print("%s 正在吃%s" % (self.mingzi,food))
    53 
    54 Chinese.eat=eat_food
    55 
    56 print(Chinese.__dict__)
    57 p1.eat('apple')
    58 
    59 def ceshi(self):
    60     print("test")
    61 
    62 Chinese.play_boy=ceshi
    63 p1.play_boy()
    View Code

     对象与实例属性调用:

     1 country='中国-------------'
     2 class Chinese:
     3     country='中国'#类的属性
     4     def __init__(self,name):
     5         self.name=name
     6         print('--->',country)#属性通过实例或者类的点调用,如果只是实例化那么就是一个
     7         #简单的属性而已,调用的country就是country='中国-------------'
     8 
     9     def play_ball(self,ball):
    10         print('%s 正在打%s' %(self.name,ball))
    11 
    12 p1=Chinese('alex')
    13 print(p1.country)
    14 p1.country='日本'
    15 print(p1.country)
    16 print(Chinese.country)
  • 相关阅读:
    javascript学习一
    对软件工程课程的认识
    人月神话读后感
    项目开发总结报告(GB8567——88)
    MFC双缓冲绘图
    QT连接MySQL
    [QT学习]拷贝文件
    Arduino入门笔记【1】
    《人月神话》读后感以及软件工程总结
    十天冲刺任务(第二次冲刺)
  • 原文地址:https://www.cnblogs.com/sd880413/p/9184418.html
Copyright © 2011-2022 走看看