先来一段代码 表示互殴

class Gailun:
country='demaxia'
def __init__(self,name,age,life_value,att):
self.name=name
self.age=age
self.life_value=life_value
self.att=att
def attack(self,enemy):
enemy.life_value -= self.att class Riwen:
country='nocxus'
def __init__(self,name,age,life_value,att):
self.name=name
self.age=age
self.life_value=life_value
self.att=att
def attack(self,enemy):
enemy.life_value-=self.att g1=Gailun('nod','',1000,100) r1=Riwen('riwen','',800,200) while True:
if g1.life_value>=0:
g1.attack(r1)
print('riven的生命值是%s'%r1.life_value)
r1.attack(g1)
print('galn的生命值是%s' % g1.life_value)
if r1.life_value<0:
print('%s已经阵亡' % r1.name)
break
else:
print('%s已经阵亡,生命值为%s'%(g1.name,g1.life_value))
break

类的创建

#类体定义的代码在类的定义阶段就会运行

class Student:
school='oldboy'
def learn(self):
print('is learning')
def choose_course(self):
print('is choose') print(Student) #查看
print(Student.school)
print(Student.learn) #修改
Student.school='oldboyedu'
print(Student.school) #增加 Student.country='China'
print(Student.country) #删除
del Student.country
# print(Student.country) #调用函数
Student.learn('nod')
Student.learn() #如果不传参数会出错

类对象属性的查找

#类的所有数据属性是所有对象共享,所有对象都指向同一个地址
#类定义的函数就是给绑定给对象方法使用
#不同对象就是不同绑定方法
#绑定给谁 就应该由谁来调用 谁来调用就会把谁当做第一个参数传递给对应函数 class Student:
school='oldboyedu'
def __init__(self,name,age,sex):
self.Name=name
self.Age=age
self.Sex=sex
def learn(self):
print('%s is learning'%self.Name) def choose_course(self):
print('is choose_course') stu1=Student('nod','','M')
stu2=Student('luna','','W')
stu1.learn()
stu2.learn()
class Student:
school='oldboyedu'
def __init__(self,name,age,sex):
self.Name=name
self.Age=age
self.Sex=sex
def learn(self):
print('is learning') def choose_course(self):
print('is choose course')
#调用类的过程称之为实例化
#得到一个返回值 即对象 该对象是一个空的stu1
#Stuent.__init__(stu1,'nod','24','M')
stu1=Student('nod','','M')
print(stu1.__dict__) #__dict__ 查看自己的名称空间
print(type(stu1))
print(stu1.Name,stu1.Age,stu1.Sex)

Python类的部分的更多相关文章

  1. Python类中super()和__init__()的关系

    Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...

  2. LightMysql:为方便操作MySQL而封装的Python类

    原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...

  3. python 类属性与方法

    Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...

  4. python 类以及单例模式

    python 也有面向对象的思想,则一切皆对象 python 中定义一个类: class student: count = 0         books = [] def __init__(self ...

  5. Python类的特点 (1):构造函数与方法

    Python中,类的特点: #encoding:utf-8 class Parent(object): x=1 #x是Parent类的属性(字段) def __init__(self): print ...

  6. Python类属性,实例属性

    1.Python类数据属性:定义在类里面但在函数外面的变量,它们都是静态的. #一段很简单的代码,但反应了很多 >>> class A(): a=1 #一个类里面有个属性a > ...

  7. python类及其方法

    python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...

  8. python类的定义和使用

    python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...

  9. Python类的探讨

    我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法  Python类定义以关键字class开头,一个类定义例 ...

  10. python - 类成员修饰符

    在java,c#类的成员修饰符包括,公有.私有.程序集可用的.受保护的. 对于python来说,只有两个成员修饰符:公有成员,私有成员 成员修饰符是来修饰谁呢?当然是修饰成员了.那么python类的成 ...

随机推荐

  1. vscode之快速生成vue模板

    文件-首选项-用户代码片段-搜索“vue”点击进入vue.json 复制这个片段 { "Vue component": { "prefix": "vu ...

  2. select option 选中 取消js

    今天在写select option标签的过程中遇到一个问题,就是刷新页面自己选中的标签回显选择的值,清空表单,下拉选择默认的值: 1.这是默认的下拉框: 2.自己定义的下拉选项,红色方框中主要处理第一 ...

  3. 注册COMDLG32.OCX方法

    1.将下载的comdlg32.ocx文件拷贝到C:\Windows\SysWOW64(32位电脑为C:\Windows\System32)文件夹中. 2.按win+x打开快捷命令选项菜单: 再按“A” ...

  4. centos7安装doxygen

    编译 编译过程参考官网:https://www.stack.nl/~dimitri/doxygen/download.html 编译过程: git clone https://github.com/d ...

  5. two week summary

    from collections import Iteratorfrom collections import Iterabl dic = {'a':"a","91a&q ...

  6. learning makefile 定义命令包

  7. icon moon追加字体

    一.初始自定义字体为icon moon1@font-face { font-family: 'icomoon1'; src: url('fonts/icomoon1.eot?9fhn24'); src ...

  8. Binary Tree Path Sum

    Given a binary tree, find all paths that sum of the nodes in the path equals to a given number targe ...

  9. Ubuntu系统下安装免驱动摄像头

    最近想玩一下视频系列的深度学习模型,便网上淘了一个linux下免驱动的摄像头,直接插上usb接口就行,但是一般还不能直接使用,下面将简单说一下如何使用摄像头: 在你的ternimal下输入以下命令: ...

  10. python与sqlserver接口包pymssql

    包下载地址(对应着自己的电脑和Python的版本下载即可,我电脑是win32,Python是3.6的) https://pypi.python.org/pypi/pymssql/ 下载后我放到了d盘中 ...