码迷,mamicode.com
首页 > 其他好文 > 详细

day7 面向对象进阶

时间:2017-06-01 00:17:47      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:icm   traceback   并且   print   添加   div   传递参数   school   类变量   

    面向对象高级语法部分

    通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法。

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @staticmethod
    def talk():
        print("I like to study python")

class Teacher(SchoolMember):
    def __init__(self,name,age,sex,course,salary):
        super(Teacher,self).__init__(name,age,sex)
        self.course = course
        self.salary = salary

    def Teaching(self):
        print("Teacher %s is teaching %s." %(self.name,self.course))



s1 = Teacher("alex",22,"Femal","python",10000)

print("before:",s1.member_nums)
SchoolMember.member_nums = 12
print("before:",s1.member_nums)


s1.member_nums = 666     #是在类中重新生成一个变量

print("after:",s1.member_nums)
SchoolMember.member_nums = 12
print("after:",s1.member_nums)

    在上面代码中,member_nums是类变量,如果直接调用s1.member_nums的话,调用的是类里面的值;如果s1.member_nums = 666,等于在实例里面增加了一个新的变量,这个时候,修改类的值的时候,是不会影响实例里面变量的值的。上面代码的输出结果如下:

    before: 0
  before: 12
  after: 666
  after: 666

    类的静态方法@staticmethon:

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

    @staticmethod
    def walk(self):
        print("%s is walking......" %self.name)


#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.walk()
运行结果如下:
Traceback (most recent call last):
  File "/home/zhuzhu/day7/staticmethon方法.py", line 22, in <module>
    s1.walk()
TypeError: walk() missing 1 required positional argument: ‘self‘

    上面代码中,如果没有@staticmethon的话,代码执行肯定没有问题,但是当有了@staticmethod的时候,系统提示少了一个参数。如果我们把一个方法变成了一个静态方法,那么这个方法与实例就没有太大的关系了。

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

    @staticmethod        #让方法在类中剥离,与类没有关系,调用要传递参数
    def walk(self):
        print("%s is walking......" %self)


#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.walk("alex")

    @staticmethod静态方法是让类中的方法与类没有关联,调用的时候要传递参数才能调用。

    类方法

    类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    #@classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %self.name)

SchoolMember.member_nums
#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.talk()

    上面代码中,(1)类是不能直接访问实例里面的属性的;(2)@classmethod的作用是让程序只能访问类中的变量,比如上面代码中的SchoolMember.member_nums,这个是类的方法,我们可以在talk中进行访问,但是不能访问self.name,因为@classmethod只能访问类属性。

   

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %self.name)

SchoolMember.member_nums
#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.talk()

运行结果如下:
Traceback (most recent call last):
  File "/home/zhuzhu/day7/staticmethon方法.py", line 18, in <module>
    s1.talk()
  File "/home/zhuzhu/day7/staticmethon方法.py", line 13, in talk
    print("%s like to study python"  %self.name)
AttributeError: type object SchoolMember has no attribute name

    从上面可以看出,上面的代码@classmethon禁止了类中的实例变量,只能使用类变量。即不能使用self.name、self.age和self.sex,只能使用self.nember_nums和SchoolMember.member_nums类的变量。如下:

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

SchoolMember.member_nums
#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.talk()

运行结果如下:
0 like to study python

    属性方法

    属性方法的作用就是通过@property把一个方法变成一个静态属性

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

    @property     #把类的方法变成一个属性,调用要使用s1.walk不加括号直接调用,称为一个属性
    def walk(self):
        print("%s is walking......" %self.name)


#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.walk()
运行结果如下:
Traceback (most recent call last):
  File "/home/zhuzhu/day7/staticmethon方法.py", line 22, in <module>
    s1.walk()
TypeError: ‘NoneType‘ object is not callable

     如果不添加@property的话,程序是能够正常运行的,但是添加了@property之后,程序运行出现错误,是什么原因呢?因为@property是将类的方法变成了类的属性,调用的时候我们只需执行s1.walk()无需添加括号即可执行,如下:

class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    member_nums = 0
    def introduce(self):
        print("My name is %s,and I am %s year old." %(self.name,self.age))

    @classmethod        #类方法,不能访问实例变量
    def talk(self):
        print("%s like to study python"  %SchoolMember.member_nums)

    @property     #把类的方法变成一个属性,调用要使用s1.walk不加括号直接调用,称为一个属性
    def walk(self):
        print("%s is walking......" %self.name)


#SchoolMember.talk()    #不能调用,类是没有办法访问实例变量,只能访问自己
s1 = SchoolMember("Alex",22,"Female")  #实例化
s1.walk
运行结果如下:
Alex is walking......

    上面代码中,@property是将类的方法变成了成员的属性,我们可以直接使用s1.walk即可调用。

day7 面向对象进阶

标签:icm   traceback   并且   print   添加   div   传递参数   school   类变量   

原文地址:http://www.cnblogs.com/gengcx/p/6926369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!