python类初探
class human:
is_alive=True
is_man=True
def __init__(self,age):
print('this is __init__() method, which will be executed automaticly!')
self.nianling=age
print('the age is: ',self.nianling)
def setdata(self,name):
self.name=name
def printdata(self):
print(self.name)
h=human(22)
#the running result is:
'''
this is __init__() method, which will be executed automaticly!
the age is: 22
'''
类的定义格式是:class leiming:
类的__init__(self)方法被任何实例自动调用,我称此方法为类的初始化函数。self是啥,这个参数是Python特有的,当__init__()被自动调用的时候,实例对象作为第一个参数被传递了进去。
class dog():
def __init__(self):
print('init the class dog')
print(self)
dog1=dog()
print(dog1)
'''
init the class dog
<__main__.dog object at 0x000000000234F278>
<__main__.dog object at 0x000000000234F278>
可以看到self的地址和dog1对象的地址是一样的。
'''
类中包含属性和方法。所谓属性就是对类的特点进行定义。比如定义一个鸟类,这个类具有属性或者说特点:都有翅膀。
class bird:
def __init__(self):
print('This is __init__() method, which will be executed automaticly!','\n'
'All of us have wing!')
h=bird()
#the running result is:
'''
This is __init__() method, which will be executed automaticly!
All of us have wing!s
'''
类的属性可以定义在类内,也可以定义在类外,比如:
class xin:
pass
xin.name='杨过'
xin.age=22
xin.sex='male'
x=xin()
print('我的名字是:',x.name)
print('我的性别是:',x.sex)
print('我的年龄是:',x.age)
'''
我的名字是: 杨过
我的性别是: male
我的年龄是: 22
'''
类的属性在类内定义,实例的属性通过self.shuxing=... 赋值定义。
class sky:
color='blue'
def __init__(self,name):
print('我会被自动执行的哦!')
self.mingzi=name
def f1(self,age):
self.nianling=age
def display(self):
print('我的年龄是:',self.nianling)
p1=sky('杨过')
print(sky.color)
print('我的名字是:',p1.mingzi)
p1.f1(23)
p1.display()
'''
我会被自动执行的哦!
blue
我的名字是: 杨过
我的年龄是: 23
'''
每个实例对象都会继承类的属性,并且可以在方法内通过self做赋值产生每个实例自己的属性。
class sky:
color='blue'
def __init__(self,name):
print('我会被自动执行的哦!')
self.mingzi=name
def f1(self,age):
self.nianling=age
def display(self):
print('我的年龄是:',self.nianling)
p1=sky('杨过')
p2=sky('小龙女')
print(sky.color,p1.color)
print('我的名字是:',p1.mingzi)
print('我的名字是:',p2.mingzi)
p1.f1(23)
p2.f1(22)
p1.display()
p2.display()
'''
我会被自动执行的哦!
我会被自动执行的哦!
blue blue
我的名字是: 杨过
我的名字是: 小龙女
我的年龄是: 23
我的年龄是: 22
'''
继承:
class first:
def me(self):
print('I am first!')
class second(first):
def me(self):
print('I am second!')
first.me(self)#你(类second)继承了它(类first),你便可以调用它的方法
print('.....end......')
s1=second()
s1.me()
'''
I am second!
I am first!
.....end......
'''
'类属性'这三个字的理解:
为什么要叫类属性呢,因为这个属性是和类绑定的,并不是和实例绑定的。胎生这个属性是全人类共有的,并不是某个人特殊拥有的属性。
class Human:
taisheng = True
p1=Human()
print(Human.taisheng)
'''
True
'''
类内的初试化方法,每个类实力都可以随意修改里面的参数,这使得代码不健壮,封装性不好。此时把__init__()方法里面的属性之前附加两个下划线就可以阻止实例访问此方法了。
class human:
def __init__(self,name):
self.name=name
def walk(self):
print(self.__name+' is running!')
p1=human('liudehua')
print(p1.name)
'''
实例正常访问__init__()方法中定义的实例属性
liudehua
''' class human:
def __init__(self,name):
self.__name=name
def walk(self):
print(self.__name+' is running!')
p1=human('liudehua')
print(p1.__name)
'''
__init__()中定义实例的属性时,属性变量之前附加两个下划线,实例就无法再访问了。
Traceback (most recent call last):
File "D:/good/s12_1/star/c.py", line 7, in <module>
print(p1.__name)
AttributeError: 'human' object has no attribute '__name'
'''
如果仍想访问,就再增加个get接口。
class human:
def __init__(self,name):
self.__name=name
def get_name(self):
return self.__name
p1=human('林志玲')
print('One of the first beauty in society is :',p1.get_name())
'''
__init__()中定义实例的属性时,属性变量之前附加两个下划线,实例就无法再访问了。
但可以再定义一个改变各个实例属性的接口get_name()函数:
One of the first beauty in society is : 林志玲
'''
如果想修改已定义的实例的属性,就再定义个修改的接口:
#如果想修改实例的属性,就再定义一个修个的接口:
class human:
def __init__(self,name):
self.__name=name
def get_name(self):
return self.__name
def set_name(self,name):
self.__name=name
p1=human('林志玲')
print(p1.get_name())
p1.set_name('王菲')
print(p1.get_name()) '''
林志玲
王菲
'''
哺乳动物是动物的一种,哺乳动物是动物的子类,子类拥有父类的属性、方法,即继承。同时又可以拥有父类没有的属性和方法,即多态。
class human:
def __init__(self,name):
self.__name=name
def walk(self):
print(self.name+' is running!')
def get_name(self):
return self.__name
def set_name(self,name):
self.__name=name
class man(human):#此为继承
def __init__(self,name,has_wife):
super(man,self).__init__(name)#super(Man, self).__init__(name)等价于调
# 用了父类Human的构造函数,就不用再复制粘贴一遍self.__name=name了。
self.has_wife=has_wife
def smoke(self):
print('Man like smoking!')
def mustache(self):
print('Man have to remove mustache everyday!')
class woman(human):#此为继承
def __init__(self,name,has_husband):
super(woman,self).__init__(name)
self.has_hasband=has_husband
def shopping(self):
print('Woman like buying ,buying and buying!')
def make_up(self):
print('Woman like making up!')
m1=man('周杰伦','Yes')
m2=woman('林志玲','No')
print(m1.has_wife)
m1.smoke()
m1.mustache()
print(m2.has_hasband)
m2.shopping()
m2.make_up()
'''
Yes
Man like smoking!
Man have to remove mustache everyday!
No
Woman like buying ,buying and buying!
Woman like making up!
'''
python类初探的更多相关文章
- 进击的Python【第一章】:Python背景初探与Python基础(一)
Python背景初探 一.Python起源 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做 ...
- Python 3 初探,第 2 部分: 高级主题
Python 3 是 Guido van Rossum 功能强大的通用编程语言的最新版本.它虽然打破了与 2.x 版本的向后兼容性,但却清理了某些语法方面的问题.本文是这个由两部分组成的系列文章中的第 ...
- Python类中super()和__init__()的关系
Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...
- LightMysql:为方便操作MySQL而封装的Python类
原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...
- python 类属性与方法
Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...
- python 类以及单例模式
python 也有面向对象的思想,则一切皆对象 python 中定义一个类: class student: count = 0 books = [] def __init__(self ...
- Python类的特点 (1):构造函数与方法
Python中,类的特点: #encoding:utf-8 class Parent(object): x=1 #x是Parent类的属性(字段) def __init__(self): print ...
- Python类属性,实例属性
1.Python类数据属性:定义在类里面但在函数外面的变量,它们都是静态的. #一段很简单的代码,但反应了很多 >>> class A(): a=1 #一个类里面有个属性a > ...
- python类及其方法
python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
随机推荐
- zoj3329--One Person Game(概率dp第六弹:形成环的dp,带入系数,高斯消元)
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- 应用设置Setting的实现
有非常多应用都在iOS设置中有相关的设置.例如以下图: 通过这个设置能够方便的相应用的一些主要的设置进行更改. 要完整的实现这个设置功能,有下面几方面问题须要解决: 1)设置的编写(实现设置的 ...
- vue2.0 引用qrcode.js实现获取改变二维码的样式
vue代码 <template> <div class="qart"> <div id="qrcode" ref="qr ...
- 理解支持向量机(三)SMO算法
在支持向量机模型的求解中,我们用到了SMO算法来求解向量α. 那么什么是SMO算法?在讲SMO算法之前.我们须要先了解下面坐标上升法. 1.坐标上升法 如果有优化问题: W是α向量的函数.利用坐标上升 ...
- Scanner遇上UnmappableCharacterException
上周末的时候.朋友约好去KTV,鉴于我这样的不怎么听歌的孩子伤不起啊,灵机一动就把我的酷狗歌单导出来了,XML文件嘛,内容太多,我仅仅想要歌名足已. 于是写了一个java去输出歌名. 岂料我受 ...
- Oracle 修改字段注释
修改字段注释SQL: COMMENT ON COLUMN 表名.字段名 IS '注释内容'; 批量修改所有表的字段注释,这里是修改所有没有注释的字段 附件: 实现步骤: ...
- SpringBoot学习之常用注解
@SpringBootAppliaction:通常注解写在SpringBoot启动类中,主要包括三个作用: 1.@Configuration表示将该类作用springboot配置文件类. 2.@Ena ...
- WinForm搭载ScintillaNET时文本由于发生偏移被隐藏解决方案
项目用ScintillaNet搭载到WinForm以满足文本编辑的需求,在用FindReplace.Scintilla.Text=“显示内容”输出文本内容的时候会碰到文本被WinForm边框隐藏的情况 ...
- iOS多线程与网络开发之小文件上传
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. /** 取得本地文件的MIMEType */ 2 - (void) getMIMEType { 3 // Socket 实现断点上传 4 5 //apa ...
- MFC——9.多线程与线程同步
Lesson9:多线程与线程同步 程序.进程和线程是操作系统的重点,在计算机编程中.多线程技术是提高程序性能的重要手段. 本文主要解说操作系统中程序.进程和线程之间的关系,并通过相互排斥对象和事件对象 ...