首页 / PYTHON / python 面向对象
python 面向对象
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python 面向对象,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4303字,纯文字阅读大概需要7分钟。
内容图文
1.面向对象设计(OOD):将一类具体事物的数据和动作整合到一起
2.面向对象编程(OOP):用定义类和实例或者对象方法实现面向对象的设计
3.类属性:数据属性 函数属性(实例只有属性对象 无函数属性 它调用类函数属性)
########################面向对象############### 1 .面向对象设计(OOD):将一类具体事物的数据和动作整合到一起 2 .面向对象编程(OOP):用定义类和实例或者对象方法实现面向对象的设计 语法 类名首写字母大写 类数据属性 类函数属性(方法) python为类内置的特殊属性 类名.__dic__查看类属性 类名.__name__# 类的名字(字符串) 类名.__doc__# 类的文档字符串 类名.__base__# 类的第一个父类(在讲继承时会讲) 类名.__bases__# 类所有父类构成的元组(在讲继承时会讲) 类名.__dict__# 类的字典属性 类名.__module__# 类定义所在的模块 类名.__class__# 实例对应的类(仅新式类中) 类的特殊属性(了解即可) 经典类 class 类名: x = 1 # 类数据属性 def __init__(self): # self实例化对象自己本身 pass def func(self): # 函数属性 pass 新式类 class 类名(object): def __init__(self): pass def func(self): pass
# !/usr/bin/python env # encoding: utf-8 class People: # 类名 country = ‘China‘# 类数据属性def__init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性print("%s eat %s" % (self.name, self.food)) return"eat"def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性print(p1.__dict__) # 查看实例属性 {‘food‘: ‘apple‘, ‘name‘: ‘lisi‘, ‘gender‘: ‘man‘} # p1.dark() # 报错 TypeError: dark() takes 0 positional arguments but 1 was given p1.eat() # self = p1实例化对象 People.eat(p1) # 相当于 p1.eat()
4.类 和 实例属性增删改查
类属性操作
# !/usr/bin/python env # encoding: utf-8 # 类属性增删改查 class People: # 类名 country = ‘China‘# 类数据属性def__init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性print("%s eat %s" % (self.name, self.food)) return"eat"def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性# 查看类属性print(People.country) # 修改类属性 People.country = "Chinese"print(People.country) # 添加类属性 People.sex1 = "man"print(People.sex1) # manprint(p1.sex1) # man# 删除类属性del People.sex1 # print(People.sex1) # AttributeError: type object ‘People‘ has no attribute ‘sex1‘# 函数属性操作def dark(): print("dark") People.dark=dark People.dark() # dark
实例属性操作
# !/usr/bin/python env # encoding: utf-8 # 实例属性增删改查 实例属性只有数据属性 无函数属性 class People: # 类名 country = ‘China‘# 类数据属性def__init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性print("%s eat %s" % (self.name, self.food)) return"eat"def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性# 查看print(p1.country) # 修改 p1.country = "Chinese"print(p1.country) def dark(): print("dark") p1.dark=dark p1.dark() # dark# 添加 p1.age = "12"print(p1.age) # 12# 删除del p1.age print(p1.age) # AttributeError: ‘People‘ object has no attribute ‘age‘
5.对象和实例属性
# 对象和实例属性 class People: # 类名 country = ‘China‘# 类数据属性def__init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性print("%s eat %s" % (self.name, self.food)) return"eat"def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性 p1.country = "Chinese"print(p1.country) # Chineseprint(People.country) # China country = "Chinese"class China: country = ‘China‘ l = [1,2,3] def__init__(self, name): self.name = name print(‘country:‘, country) def eaat(self): pass# "."的调用和类 实例属性有关 不用点和它们没关系print(China.__dict__) # {‘country‘: ‘China‘, ‘eaat‘: <function China.eaat at 0x013DCDB0>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘China‘ objects>, ‘__init__‘: <function China.__init__ at 0x013DCD68>, ‘__doc__‘: None, ‘__module__‘: ‘__main__‘, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘China‘ objects>} c = China(‘zhangsan‘) # country: Chineseprint(c.__dict__) # {‘name‘: ‘zhangsan‘}print("----------") # 注意 赋值是新增属性,不是更改属性 # c.l = [2,3] # print(c.l) # [2,3] # print(China.l) # [1, 2, 3] # c.l.append(4) # print(c.l) # [2, 3,4] # print(China.l) # [1, 2, 3]print(c.l) print(China.l) c.l.append(4) print(c.l) print(China.l) # 显示 # [1, 2, 3] # [1, 2, 3] # [1, 2, 3, 4] # [1, 2, 3, 4]
原文:https://www.cnblogs.com/icemonkey/p/10450229.html
内容总结
以上是互联网集市为您收集整理的python 面向对象全部内容,希望文章能够帮你解决python 面向对象所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。