python---面向对象编程1
程序员文章站
2022-05-07 22:42:02
...
python—面向对象编程1
事例1(正常都是要绑定到事例上的):使用绑定事例上执行
root@kali:~/python/mod# python class.py
Traceback (most recent call last):
File "class.py", line 8, in <module>
p.info('xwb',22)
TypeError: info() takes exactly 2 arguments (3 given)
root@kali:~/python/mod# vi class.py
root@kali:~/python/mod# cat class.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def info(self,name,age):#必须给三个参数,self是python语言约定俗称的
print 'you name is %s,age is %s'%(name,age)
p = person()
p.info('xwb',22)
root@kali:~/python/mod#
root@kali:~/python/mod# vi class.py
root@kali:~/python/mod# python class.py
you name is xwb,age is 22
事例2:不使用绑定事例上执行
root@kali:~/python/mod# vi class.py
root@kali:~/python/mod# cat class.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def info(self,name,age):#必须给三个参数,self是python语言约定俗称的
print 'you name is %s,age is %s'%(name,age)
#--------绑定事例-----
#p = person()
#p.info('xwb',22)
#-------------------------
#-----不绑定实例---------
person().info('xwb',25)
#-----------------------
root@kali:~/python/mod# python class.py
you name is xwb,age is 25
root@kali:~/python/mod#
3、类的初始化
root@kali:~/python/mod# vi class1.py
root@kali:~/python/mod# cat class1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def __init__(self,nationality):
pass
def info(self,name,age):#必须给三个参数,self是python语言约定俗称的
print 'you name is %s,age is %s,you are%s'%(name,age,nationality)
#--------绑定事例-----
p = person('CN')
p.info('xwb',22)
#-------------------------
#-----不绑定实例---------
#person().info('xwb',25)
#-----------------------
root@kali:~/python/mod# python class1.py
Traceback (most recent call last):
File "class1.py", line 14, in <module>
p.info('xwb',22)
File "class1.py", line 9, in info
print 'you name is %s,age is %s,you are%s'%(name,age,nationality)
NameError: global name 'nationality' is not defined
root@kali:~/python/mod#
初始化
root@kali:~/python/mod# vi class1.py
root@kali:~/python/mod# cat class1.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:#这里的()是继承其他的类的
def __init__(self,nationality):#初始化
self.country = nationality#初始化变量
def info(self,name,age):#必须给三个参数,self是python语言约定俗称的
print 'you name is %s,age is %s,you are%s'%(name,age,self.country)
#--------绑定事例-----
p = person('CN')
p.info('xwb',22)
#-------------------------
#-----不绑定实例---------
#person().info('xwb',25)
#-----------------------
root@kali:~/python/mod# python class1.py
you name is xwb,age is 22,you areCN
root@kali:~/python/mod#
root@kali:~/python/mod# vi class2.py
root@kali:~/python/mod# cat class2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def __init__(self,name,nationality,age,sex,job):
self.Name = name
self.Country = nationality
self.Age = age
self.Sex= sex
self.Job = job
def talk(self,msg):
if msg != 0:
print '''Hello %s,
you are from %s
you are %s years old
you are %s
your job is %s.
Am i right?''' %(self.Name,self.Country,self.Age,self.Sex,self.Job)
#--------绑定实例-------------
#p = person('xuweibo','CN','19','Malse','IT Engineor')
#p.talk(2)
#----------------------------
#----------不绑定实例-------------------
person('xuweibo','CN','19','Malse','IT Engineor').talk(2)
root@kali:~/python/mod# python class2.py
Hello xuweibo,
you are from CN
you are 19 years old
you are Malse
your job is IT Engineor.
Am i right?
root@kali:~/python/mod#
使用绑定事例话,调用非常简单、方便
root@kali:~/python/mod# cat class2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def __init__(self,name,nationality,age,sex,job):
self.Name = name
self.Country = nationality
self.Age = age
self.Sex= sex
self.Job = job
def talk(self,msg):
if msg != 0:
print '''Hello %s,
you are from %s
you are %s years old
you are %s
your job is %s.
Am i right?''' %(self.Name,self.Country,self.Age,self.Sex,self.Job)
def skill(self):
if self.Job == 'IT Engineor':
print '''To be IT Engineor,you must many skills:\n
1.Linux
2.shell
3.database
4.python
'''
#--------绑定实例-------------
p = person('xuweibo','CN','19','Malse','IT Engineor')
p.talk(2)
p.skill()
#----------------------------
#----------不绑定实例-------------------
#person('xuweibo','CN','19','Malse','IT Engineor').talk(2)
#person('xuweibo','CN','19','Malse','IT Engineor').skill()
#--------------------------------------
root@kali:~/python/mod# python class2.py
Hello xuweibo,
you are from CN
you are 19 years old
you are Malse
your job is IT Engineor.
Am i right?
To be IT Engineor,you must many skills:
1.Linux
2.shell
3.database
4.python
root@kali:~/python/mod#
在python交换环境中调用类中自定义变量与变量方式:
root@kali:~/python/mod# vi class2.py
root@kali:~/python/mod# cat class2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def __init__(self,name,nationality,age,sex,job):
self.Name = name
self.Country = nationality
self.Age = age
self.Sex= sex
self.Job = job
def talk(self,msg):
self.msg = msg#定义msg变量,可以在python交换环境中调用
if self.msg != 0:
print '''Hello %s,
you are from %s
you are %s years old
you are %s
your job is %s.
Am i right?''' %(self.Name,self.Country,self.Age,self.Sex,self.Job)
def skill(self):
if self.Job == 'IT Engineor':
print '''To be IT Engineor,you must many skills:\n
1.Linux
2.shell
3.database
4.python
'''
#--------绑定实例-------------
p = person('xuweibo','CN','19','Malse','IT Engineor')
p.talk(2)
p.skill()
#----------------------------
#----------不绑定实例-------------------
#person('xuweibo','CN','19','Malse','IT Engineor').talk(2)
#person('xuweibo','CN','19','Malse','IT Engineor').skill()
#--------------------------------------
root@kali:~/python/mod# python class2.py
Hello xuweibo,
you are from CN
you are 19 years old
you are Malse
your job is IT Engineor.
Am i right?
To be IT Engineor,you must many skills:
1.Linux
2.shell
3.database
4.python
root@kali:~/python/mod# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
>>> import class2
Hello xuweibo,
you are from CN
you are 19 years old
you are Malse
your job is IT Engineor.
Am i right?
To be IT Engineor,you must many skills:
1.Linux
2.shell
3.database
4.python
>>> class2.
class2.__class__( class2.__file__ class2.__init__( class2.__reduce__( class2.__sizeof__( class2.person
class2.__delattr__( class2.__format__( class2.__name__ class2.__reduce_ex__( class2.__str__(
class2.__dict__ class2.__getattribute__( class2.__new__( class2.__repr__( class2.__subclasshook__(
class2.__doc__ class2.__hash__( class2.__package__ class2.__setattr__( class2.p
>>> class2.p.
class2.p.Age class2.p.Job class2.p.Sex class2.p.__doc__ class2.p.__module__ class2.p.skill(
class2.p.Country class2.p.Name class2.p.__class__ class2.p.__init__( class2.p.msg class2.p.talk(
当中class2.p.msg 变量就可以显示出来调用
类型函数功能可以相互调用
root@kali:~/python/mod# vi class2.py
root@kali:~/python/mod# python class2.py
Hello xuweibo,
you are from CN
you are 19 years old
you are Malse
your job is IT Engineor.
Am i right?
To be IT Engineor,you must many skills:
1.Linux
2.shell
3.database
4.python
Traceback (most recent call last):
File "class2.py", line 41, in <module>
p.action(4)
TypeError: action() takes exactly 1 argument (2 given)
root@kali:~/python/mod# vi class2.py
root@kali:~/python/mod# python class2.py
Hello xuweibo,
you are from CN
you are 19 years old
you are Malse
your job is IT Engineor.
Am i right?
To be IT Engineor,you must many skills:
1.Linux
2.shell
3.database
4.python
call this fuction!!
root@kali:~/python/mod# cat class2.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
class person:
def __init__(self,name,nationality,age,sex,job):
self.Name = name
self.Country = nationality
self.Age = age
self.Sex= sex
self.Job = job
def talk(self,msg):
self.msg = msg#定义msg变量,可以在python交换环境中调用
if self.msg != 0:
print '''Hello %s,
you are from %s
you are %s years old
you are %s
your job is %s.
Am i right?''' %(self.Name,self.Country,self.Age,self.Sex,self.Job)
def skill(self):
if self.Job == 'IT Engineor':
print '''To be IT Engineor,you must many skills:\n
1.Linux
2.shell
3.database
4.python
'''
def action(self):
if self.msg != 2:
print 'call this fuction!!'
else:
pass
#--------绑定实例-------------
p = person('xuweibo','CN','19','Malse','IT Engineor')
p.talk(1)
p.skill()
p.action()
#----------------------------
#----------不绑定实例-------------------
#person('xuweibo','CN','19','Malse','IT Engineor').talk(2)
#person('xuweibo','CN','19','Malse','IT Engineor').skill()
#--------------------------------------
root@kali:~/python/mod#
上一篇: MySQL的索引结构_MySQL
下一篇: (3):数据库完整性和约束_MySQL