day24 Pythonpython 面向对象设计 类
将一些相同特征和动作的成为类,现有类才能创建对象,对象就是特征和动作的结合体
类:把一类事物的相同特征和动作整合到一起就是类。类是一个抽象概念
对象:就是基于类而创建的一个具的事物(具体存在的),也是特征和动作给的结合
类是用来描述 一类事物,类的对象指的是这一类书屋中的一个个个体,是事物就要有属性属性分为
1:数据属性:就是变量
2:函数属性:就是函数,在面向对象里通常称为方法
注意:::类和对象均用点来访问自己的属性
class Chinese:
'这是一个中国人'
dang='共青团'
def sui_di_tu_tan():
print('正在吐')
#调用这个函数一定要加参数,只不过还没定义参数的意义
def cha_dui(self):
print('插到了前面')
#数据属性
print(Chinese.dang)
#函数属性
Chinese.sui_di_tu_tan() print(dir(Chinese))
#查看类的属性字典
print('=========================================')
print(Chinese.__dict__)
print('=============================================')
print(Chinese.__dict__['dang'])
print('=========================================')
print(Chinese.cha_dui(1))
print('==============================')
Chinese.__dict__['sui_di_tu_tan']()
Chinese.__dict__['cha_dui']("you") 结果:
共青团
正在吐
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'cha_dui', 'dang', 'sui_di_tu_tan']
=========================================
{'__module__': '__main__', '__doc__': '这是一个中国人', 'dang': '共青团', 'sui_di_tu_tan': <function Chinese.sui_di_tu_tan at 0x7f7d87a91158>, 'cha_dui': <function Chinese.cha_dui at 0x7f7d87a911e0>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>}
=============================================
共青团
=========================================
插到了前面
None
==============================
正在吐
插到了前面
对象由类实例化而来,类实例化的结果称为一个实例或者称为一个对象
class Chinese:
'这是一个中国人'
dang='共青团'
def sui_di_tu_tan():
print('正在吐')
def cha_dui(self):
print('%s插到了前面' %self.名字)
#初始化:__init__定制对象的属性,__init__会自动return所以要是自己加上return回报错
def __init__(self,name,age,gender):
print('初始化开始')
print('-------------')
print(self)#========>p1
print('-------------')
self.名字=name #p1.名字=name
self.年龄=age #p1.年龄=age
self.性别=gender #p1.性别=gender
print('初始化结束')
#class自动帮你return了
def eat_foot(self,food):
print('%s is eating %s' % (self.名字,food))
#实例化会出发__init__函数运行
p1 = Chinese('charon',12,'F')
print(p1.__dict__)
print(p1.名字)
#p1产生的字典由__init__产生,p1调用dang在__init__作用域没找到,就去外一层Chinese作用域找dang
print(p1.dang)
#print(Chinese.__dict__)
Chinese.sui_di_tu_tan()
Chinese.cha_dui(p1)
#p1.sui_di_tu_tan()
p1.cha_dui()
#与self内春地址相同
print(p1)
p1.eat_foot('shit')
print('=========================================')
p2 = Chinese('pluto',100,'F')
p2.eat_foot('apple') 结果:
China
Japan
{'name': 'charon'}
Japan
eventment
eventment
{'__module__': '__main__', 'country': 'Japan', '__init__': <function Chinese.__init__ at 0x7fc8ed2ff158>, 'paly_ball': <function Chinese.paly_ball at 0x7fc8ed2ff1e0>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None, 'dang': 'eventment'}
===================================================
{'__module__': '__main__', 'country': 'Japan', '__init__': <function Chinese.__init__ at 0x7fc8ed2ff158>, 'paly_ball': <function Chinese.paly_ball at 0x7fc8ed2ff1e0>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}
类的增删改查
# -*- coding:utf8 -*-
class Chinese:
country = 'China'
def __init__(self,name):
self.name=name
def paly_ball(self,ball):
print('%s in playing %s' % (self.name,ball))
#查看
print(Chinese.country)
#修改
Chinese.country = 'Japan'
print(Chinese.country)
#用这个类生成一个实例
p1 = Chinese('charon')
print(p1.__dict__)
print(p1.country)
#增加
Chinese.dang='eventment'
print(Chinese.dang)
print(p1.dang)
#删除
#del Chinese.dang
print(Chinese.__dict__)
print('===================================================')
del Chinese.dang
print(Chinese.__dict__) 结果:
China
Japan
{'name': 'charon'}
Japan
eventment
eventment
{'__module__': '__main__', 'country': 'Japan', '__init__': <function Chinese.__init__ at 0x7f29a1607158>, 'paly_ball': <function Chinese.paly_ball at 0x7f29a16071e0>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None, 'dang': 'eventment'}
===================================================
{'__module__': '__main__', 'country': 'Japan', '__init__': <function Chinese.__init__ at 0x7f29a1607158>, 'paly_ball': <function Chinese.paly_ball at 0x7f29a16071e0>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}
实例属性增删改查
class Chinese:
country='China'
def __init__(self,name):
self.name=name
def paly_ball(self,ball):
print('%s 正在打 %s' % (self.name,ball))
#生成一个实例
p1 = Chinese('charon')
#查看实例属性字典
print(p1.__dict__) #查看实例属性
print(p1.name)
print(p1.paly_ball)
p1.paly_ball('football')
#增加实例属性
p1.age = 18
print(p1.__dict__)
print(p1.age)
#属性分为有函数属性跟数据属性
#实例只有数据属性,他的函数属性是调类的
#修改实例属性
p1.age=200
print(p1.__dict__)
print(p1.age)
#删除
del p1.age
print(p1.__dict__) 结果:
{'name': 'charon'}
charon
<bound method Chinese.paly_ball of <__main__.Chinese object at 0x7fafb2db8550>>
charon 正在打 football
{'name': 'charon', 'age': 18}
18
{'name': 'charon', 'age': 200}
200
{'name': 'charon'}
对象和实例属性
# class Chinese:
# country='China'
# def __init__(self,name):
# self.name=name
# def paly_ball(self,ball):
# print('%s 正在打 %s' % (self.name,ball))
# p1=Chinese('charon')
# print(p1.country)
# p1.country='Japan'
# #print(p1.__dict__)
# #print(Chinese.__dict__)
# print(p1.country)
# print(Chinese.country)
# print('类的--->',Chinese.country)
# print('实例的--->',p1.country) # class Chinese:
# country = '中国'
# def __init__(self,name):
# self.name=name
#
# def play_ball(self,ball):
# print('%s 正在打 %s' %(self.name,ball))
# p1=Chinese('alex')
# print(p1.country) country='中国-------------------'
class Chinese:
country='中国'
def __init__(self,name):
self.name=name
#这个country是个变量,类是点加country
print('--->',country) def play_ball(self,ball):
print('%s 正在打 %s' %(self.name,ball)) #print(Chinese.__dict__)
#print(Chinese.country)
p1=Chinese('alex')
print(Chinese.country)
print(Chinese.__dict__) 结果:
---> 中国-------------------
中国
{'__module__': '__main__', 'country': '中国', '__init__': <function Chinese.__init__ at 0x7f298ab72158>, 'play_ball': <function Chinese.play_ball at 0x7f298ab721e0>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}
换个姿势搞你,就不信你不爽
class Chinese:
country='China'
l=['a','b']
def __init__(self,name):
self.name=name
print(self.name) def play_ball(self,ball):
print('%s 正在打 %s' %(self.name,ball)) p1=Chinese('charon')
# p1.l=['a','b','c']
# print(p1.l)
# print(Chinese.l)
# print(Chinese.__dict__)
print(p1.l)
p1.l.append('c')
print(p1.l)
print(p1.__dict__)
print(Chinese.l) 结果:
charon
['a', 'b']
['a', 'b', 'c']
{'name': 'charon'}
['a', 'b', 'c']
day24 Pythonpython 面向对象设计 类的更多相关文章
- day24:面向对象设计与面向对象编程、类和对象
一.三大编程范式: 面向过程: 面向函数: 面向对象: 二.程序的进化论: 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 2.从上述的指令中提取重复的代码块或逻辑,组织到一起(比方说,你定 ...
- matlab面向对象设计---类的概念和使用
代码: classdef MadgwickAHRS < handle %MADGWICKAHRS Implementation of Madgwick's IMU and AHRS algori ...
- python函数的面向对象——面向对象设计
通过几个函数式编号演进,理解面向对象设计 def01.py dog1 = { 'name':'元昊', 'gender':'母', 'type':'藏獒' } dog2 = { 'name':'李李' ...
- day24 面向对象设计part1
#!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...
- UML类图与面向对象设计原则
1. 引言 从大一开始学习编程,到如今也已经有两年了.从最初学习的Html,Js,JaveSe,再到JavaEE,Android,自己也能写一些玩具.学习过程中也无意识的了解了一些所谓的设计模 ...
- <九>面向对象分析之UML核心元素之设计类,类,属性,方法,可见性
设计类
- C#基础第八天-作业答案-设计类-面向对象方式实现两个帐户之间转账
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#基础第八天-作业-设计类-面向对象方式实现两个帐户之间转账
要求1:完成以下两种账户类型的编码.银行的客户分为两大类:储蓄账户(SavingAccount)和信用账户(CreditAccount),两种的账户类型的区别在于:储蓄账户不允许透支,而信用账户可以透 ...
- Virtools元素、类和面向对象设计
无意中发现了在某个不存在的网站( https://sites.google.com )上,还存有09年写的一些半成品教材,下面这篇文章就是其中一部分. 概述 Virtools将元素(Element)组 ...
随机推荐
- 微信小程序使用wxParse,解决图片显示路径问题
我们经常用到发布文章,用的是UEditor百度富文本编辑器,方便排版,存储的也是html代码,这样小程序解析出来的也是排版的样式,但是使用wxParse解析html的时候,因为存储的是图片的相对路径, ...
- HDU1521 排列组合(生成函数 背包)
题意 链接 Sol 可以用生成函数做,也可以用组合数做. 生成函数就是无脑算一下阶乘暴力背包,然后最后再乘上\(M\)的阶乘 组合数的方法就是用类似背包的转移,转移的时候考虑当前放的这几个的方案数即可 ...
- 2018-08-13 Head First OO分析设计一书略读与例子中文化
注: 此笔记仅为个人学习此教程的布局和材料组织之用. 如有兴趣请自行详阅. 第一章是以吉他商店的存货系统作例子. 第二章设计有狗洞的门. 第三章对第二章基础上, 更改需求后对应设计. 第四章继续改进此 ...
- java设计模式之模板模式以及钩子方法使用
1.使用背景 模板方法模式是通过把不变行为搬到超类,去除子类里面的重复代码提现它的优势,它提供了一个很好的代码复用平台.当不可变和可变的方法在子类中混合在一起的时候, 不变的方法就会在子类中多次出现, ...
- python 标准类库-并行执行之subprocess-子进程管理
标准类库-并行执行之subprocess-子进程管理 by:授客QQ:1033553122 1.使用subprocess模块 以下函数是调用子进程的推荐方法,所有使用场景它们都能处理.也可用Popen ...
- 微信小程序中如何获取for循环的item相关值到JS页面的问题
今天小程序开发过程中,遇到了这个棘手的问题.由于我没有前端基础,只是知道一点儿基本的HTML标签,所以卡了好久,特此分享,望后来的你,可以有所收获. measure step 1 *.WXML: ...
- Visual Studio Team Services 动手实验
Visual Studio Team Services 动手实验 概述 为Visual Studio Team Services提供的动手实验,要完成实验首先需要满足以下条件: Visual Stud ...
- django数据查询之聚合查询和分组查询
<1> aggregate(*args,**kwargs): 通过对QuerySet进行计算,返回一个聚合值的字典.aggregate()中每一个参数都指定一个包含在字典中的返回值.即在查 ...
- Win10改AHCI无需重装系统(无需改注册表)的方法
下面就开始:1.开机后按下WIN键 加 R键2.输入 msconfig3.如图中所示进行点击.1 引导界面 2安全引导打钩 .最小打钩 3 下面的确定.4.点击重新启动5.在重启时连续按 F2 进入B ...
- C++实现第三方资源释放与载入过程(以DLL为例)
简介 我们经常看见有一些程序开始执行时会释放一些文件,以便于后续操作.例如一些病毒为了便于传播和隐藏,经常把一些需要用的动态库或是驱动文件打包进一个可执行文件中,再由需要使用的时候,再临时释放和加载. ...