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
'''
View Code

类的定义格式是: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对象的地址是一样的。
'''
View Code

类中包含属性和方法。所谓属性就是对类的特点进行定义。比如定义一个鸟类,这个类具有属性或者说特点:都有翅膀。

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
'''
View Code

类的属性可以定义在类内,也可以定义在类外,比如:

class xin:
    pass
xin.name='杨过'
xin.age=22
xin.sex='male'
x=xin()
print('我的名字是:',x.name)
print('我的性别是:',x.sex)
print('我的年龄是:',x.age)
'''
我的名字是: 杨过
我的性别是: male
我的年龄是: 22
'''
View Code

类的属性在类内定义,实例的属性通过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
'''
View Code

每个实例对象都会继承类的属性,并且可以在方法内通过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
'''
View Code

继承:

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......
'''
View Code

'类属性'这三个字的理解:

为什么要叫类属性呢,因为这个属性是和类绑定的,并不是和实例绑定的。胎生这个属性是全人类共有的,并不是某个人特殊拥有的属性。

class Human:
    taisheng = True
p1=Human()
print(Human.taisheng)
'''
True
'''
View Code

类内的初试化方法,每个类实力都可以随意修改里面的参数,这使得代码不健壮,封装性不好。此时把__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'
'''
View Code

如果仍想访问,就再增加个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 : 林志玲
'''
View Code

如果想修改已定义的实例的属性,就再定义个修改的接口:

#如果想修改实例的属性,就再定义一个修个的接口:
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())

'''
林志玲
王菲
'''
View Code

哺乳动物是动物的一种,哺乳动物是动物的子类,子类拥有父类的属性、方法,即继承。同时又可以拥有父类没有的属性和方法,即多态。

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!
'''
View Code

猜你喜欢

转载自www.cnblogs.com/yibeimingyue/p/9357109.html