Day16:OOP:object-orinted-programing
时间:2020-04-11
本文章向大家介绍Day16:OOP:object-orinted-programing,主要包括Day16:OOP:object-orinted-programing使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1 # dog1= { 2 # 'name' : "aliu", 3 # 'gender' : 'female', 4 # 'type' : 'dog' 5 # } 6 # dog2= { 7 # 'name' : "zxver", 8 # 'gender' : 'male', 9 # 'type' : 'dog' 10 # } 11 # people= { 12 # 'name' : "zita", 13 # 'gender' : 'female', 14 # 'type' : 'people' 15 # } 16 # def chishi(dog): 17 # if dog['type'] == 'dog': 18 # print("the dog [%s] is eating shit" %dog["name"]) 19 # 20 # def jiao(dog): 21 # if dog['type'] == 'dog': 22 # print("the dog [%s] is barking" %dog["name"]) 23 # 24 # chishi(dog1) 25 # chishi(people) 26 #>>>>>>>>>>>>>>>>>>>>>>>>>It's repeatable upon 27 #>>>>>>>>>>>>>>>>>>>>>>>>>declare the fuctional area below 28 29 # def dog(name,gender,type): 30 # def chishi(dog): 31 # print("the dog [%s] is eating shit" % dog["name"]) 32 # 33 # def jiao(dog): 34 # print("the dog [%s] is barking" % dog["name"]) 35 # dog1 = { 36 # 'name': name, 37 # 'gender': gender, 38 # 'type': type, 39 # 'jiao': jiao, 40 # 'chishi': chishi, 41 # } 42 # return dog1 43 # # d1 = dog() 44 # # print(d1) 45 # # d1['chishi'](d1) 46 47 # d1 = dog('alex','female','dog') 48 # d2 = dog('zita','female','dog') 49 # print(d1) 50 # print(d2) 51 # d1['jiao'](d1) 52 # d2['chishi'](d2) 53 54 #>>>>>>>>>>>>>>>>>>>>>>>>>>use 'init' to initialize the function 55 56 def dog(name,gender,type): 57 def chishi(dog): 58 print("the dog [%s] is eating shit" % dog["name"]) 59 60 def jiao(dog): 61 print("the dog [%s] is barking" % dog["name"]) 62 #>>>>>>>>>Action of the class 63 def init(name,gender,type): 64 dog1 = { 65 'name': name, 66 'gender': gender, 67 'type': type, 68 'jiao': jiao, 69 'chishi': chishi, 70 } 71 return dog1 72 #>>>>>>>>>>the traits of the class 73 return init(name,gender,type) 74 75 76 d1 = dog('alex','female','dog') #>>>>>>>>>>>define the object:include the actions and the traits 77 d2 = dog('zita','female','dog') 78 print(d1) 79 print(d2) 80 d1['jiao'](d1) 81 d2['chishi'](d2)
the traits:name,addr,type
the actions:test,recruit,expel
conbine the actions and the traits
1 def school(name,addr,type): 2 def kao_shi(school): 3 print('%s is testing' %school['name']) 4 def zhao_sheng(school): 5 print('%s is recruting' %school['name']) 6 def init(name,addr,type): 7 sch = { 8 'name': name, 9 'addr': addr, 10 'type': type, 11 'kao_shi': kao_shi, 12 'zhao_sheng': zhao_sheng, 13 } 14 return sch 15 return init(name,addr,type) 16 s1 = school('oldboy','chongqing','privite') 17 s2 = school('tsinghua','beijing','public') 18 print(s1) 19 print(s2) 20 s1['zhao_sheng'](s1) 21 s2['kao_shi'](s2)
'''
the attribute of the class:
1:data;
2:fuction
#python为类内置的特殊属性
类名.__name__# 类的名字(字符串)
类名.__doc__# 类的文档字符串
类名.__base__# 类的第一个父类(在讲继承时会讲)
类名.__bases__# 类所有父类构成的元组(在讲继承时会讲)
类名.__dict__# 类的字典属性
类名.__module__# 类定义所在的模块
类名.__class__# 实例对应的类(仅新式类中)
'''
1 # class Chinese: #>>>>>>without () called classic(python2) 2 # 'this is a chinese class' 3 # pass 4 # print(Chinese) #>>>>>><class '__main__.Chinese'> 5 # 6 # #>>>>>>>>>>>>>>>>>what the instantiation do? 7 # p1 = Chinese() 8 # print(p1) #>>>>><__main__.Chinese object at 0x0000000001DD6A60> 9 # 10 # class Chinese(object): #>>>>with a () called new style(python3) 11 # 'this is a chinese class' 12 # pass 13 # print(Chinese) #>>>>>><class '__main__.Chinese'>
'''
the attribute of the instantiation:
1:data;
no function attribute
'''
1 # class Chinese: 2 # 'this is a chinese class' 3 # government = 'The Communist Party' 4 # country = 'china' 5 # def __init__(self,name,gender,age): 6 # self.name = name 7 # self.gender = gender 8 # self.age = age 9 # def split(self): 10 # print('%s is spliting '%(self.name)) 11 # 12 # def eat_food(self,food): 13 # print('%s is eating %s ' % (self.name,food)) 14 # 15 # p1 = Chinese('alex','female',18) 16 # print(Chinese.government) 17 # print(dir(Chinese)) 18 # print(Chinese.__dict__) 19 # print(Chinese.__dict__['government']) 20 # print(Chinese.__dict__['split'](p1)) 21 # print(p1.__dict__) 22 # print(p1.name) 23 # p1.split() 24 # print(p1.government) 25 # p1.eat_food('shit') 26 # def speak(self,language): 27 # print('%s is speaking %s'%(self.name,language)) 28 29 #>>>>>>>>>>the Class:add,del,modify,check: 30 # print(Chinese.government) 31 # Chinese.lotion = 'China' 32 # print(Chinese.lotion) 33 # # del Chinese.lotion 34 # # print(Chinese.lotion) 35 # Chinese.spk = speak 36 # # print(Chinese.__dict__) 37 # p1.spk('english') 38 39 #>>>>>>>>>>the instantiation :add,del,modify,check:pr 40 41 # print(p1.name) 42 # print(p1.split) 43 # p1.age =18 44 # print(p1.__dict__) 45 # p1.spk1=speak 46 # print(p1.__dict__) 47 # p1.spk1(p1,'english') 48 #Dont change the baselevel dict 49 # p1.__dict__['sex']='male' 50 # print(p1.__dict__) 51 # p1.age = 19 52 # print(p1.__dict__) 53 # del p1.age 54 # print(p1.__dict__) 55 56 #>>>>>>define a functional area: 57 # class MyDate: 58 # pass 59 # x=1 60 # y=10 61 # MyDate.x=1 62 # MyDate.y=2 63 # print(MyDate.__dict__) 64 65 #>>>>>>>>>>>>>>>>>>>>more complicated: 66 # print(Chinese.country) 67 # p1.country = 'us' 68 # print(p1.country) #>>>>>>>>us 69 # print(Chinese.country) #>>>>>>>>>>..china 70 country = 'china' 71 class Chinese: 72 country = 'js' 73 l=['a','b'] 74 def __init__(self,name): 75 # country = 'us' 76 print('------------>?',country) #>>>us 77 # name = input('please input the username:') 78 #>>>>youd better not write the input or output blank here!! 79 self.name = name 80 def split(self): 81 print('%s is spliting '%(self.name)) 82 83 def eat_food(self,food): 84 print('%s is eating %s ' % (self.name,food)) 85 86 p1 = Chinese('alx') 87 # # p1.split() 88 # print(Chinese.country) #>>>>>>>js 89 # p1.l =[1,2,3] #>>>>>>add the charameter l to p1 90 p1.l.append('c') #>>>>>>no adding so it changed the l of class 91 # print(p1) 92 print(p1.__dict__) 93 print(Chinese.__dict__)
原文地址:https://www.cnblogs.com/zxver/p/12680954.html
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
- Hadoop3.0通用版集群安装高可靠详细教程
- Hadoop3.0集群安装知识
- 分布式消息队列 RocketMQ 源码分析 —— Message 顺序发送与消费
- 深入解析快速排序算法的原理及其Go语言版实现
- Hadoop3.0: YARN Resource配置说明
- GO语言利用K近邻算法实现小说鉴黄
- Why Spring Boot
- 如何使用HammerDB进行MySQL基准测试
- 一个不可思议的MySQL慢查分析与解决
- Apache Spark 2.2中基于成本的优化器(CBO)
- golang的HTTP基本认证机制实例详解
- hdu----(3065)病毒侵袭持续中(AC自动机)
- Spark机器学习库(MLlib)指南之简介及基础统计
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 详解Linux手动释放缓存的方法
- 详解Centos7源码编译安装 php7.2之生产篇
- Centos7利用内存优化磁盘缓存读写速度的方法
- Redis过期策略和数据淘汰机制
- 图片由彩色渐变到黑白动画
- Kafka如何保证数据可靠性
- ubuntu中snap包的安装、更新删除与简单使用
- 学习在kernel态下使用NEON对算法进行加速的方法
- Centos7上Mesos和Marathon的安装和配置
- Redis高级数据类型-Bitmap和HyperLogLog
- Redis持久化策略
- 在linux下开启FTP服务方法介绍
- Linux中gpio接口的使用方法示例
- Three.js教程(1):初识three.js
- 解决Electron安装报错问题