zoukankan      html  css  js  c++  java
  • 面向对象 二

    面向对象:

     

    类中的成员:

    In [1]: class A:

    ...: name=10

    ...:

     

    In [2]: A.name

    Out[2]: 10

     

    In [3]: import inspect

     

    In [4]: inspect.getmembers(A)

    变量(字段),方法(函数):

            静态字段

    字段

            普通字段

     

            普通方法

    方法 静态方法

            类方法

     

    一.字段eg:

    1. class Student:  
    2.     grade =1  
    3.     
    4.     def __init__(self,name,age ) :  
    5.         self.name=name  
    6.         self.age=age  
    7.     def show(self):  
    8.         print(self.name,self.age,self.grade)  
    9. print(Student.grade)  
    10. # print(Student.age)   这个会报错
    11. tom = Student("tom",10)  
    12. tom.show()  

     

     

     

     

     

     

    在内存中存储的方式:

    In [8]: Student.__dict__

    Out[8]:

    mappingproxy({'__dict__': <attribute '__dict__' of 'Student' objects>,

    '__doc__': None,

    '__init__': <function __main__.Student.__init__>,

    '__module__': '__main__',

    '__weakref__': <attribute '__weakref__' of 'Student' objects>,

    'grade': 1,

    'show': <function __main__.Student.show>})

    In [10]: tom.__dict__

    Out[10]: {'age': 10, 'name': 'tom'}

    图片出自: http://www.cnblogs.com/wupeiqi/p/4766801.html

    静态字段的使用:通过修改静态字段,达到统一修改

    In [22]: Lili = Student("Lili",9)

    In [23]: Lili.grade

    Out[23]: 5

    In [24]: tom.grade

    Out[24]: 5

    In [25]: Student.grade=8

    In [26]: tom.grade

    Out[26]: 8

    In [27]: Lili.grade

    Out[27]: 8

     

     

    二.方法

    1. 普通方法:

    普通方法由.obj.function()调用:

    eg:

    2.静态方法

    1. class Student:  
    2.     grade = 1  
    3.     
    4.     def __init__(self, name, age):  
    5.         self.name = name  
    6.         self.age = age  
    7.     
    8.     def show(self):  
    9.         print(self.name, self.age, self.grade)  
    10.    
    11.     @staticmethod  
    12.     def static():  
    13.         print("静态方法")  
    14.     
    15. Student.static()  
    16. tom = Student("tom", 10)  
    17. tom.static()  

    静态方法

    静态方法 静态方法可以由classname.func()或obj.func()调用

    3.类方法

    1. class Student:  
    2.     grade = 1  
    3.     
    4.     def __init__(self, name, age):  
    5.         self.name = name  
    6.         self.age = age  
    7.     
    8.     def show(self):  
    9.         print(self.name, self.age, self.grade)  
    10.    
    11.     @staticmethod  
    12.     def static():  
    13.         sex = "VU"  
    14.     
    15.         print("静态方法")  
    16.    
    17.     @classmethod  
    18.     def clss_method(cls):  
    19.         print(cls.grade)  
    20.         # print(cls.age)  
    21.     
    22.         print("类方法")  
    23.     
    24.     
    25. # Student.clss_method()  
    26. #  
    27. # Student.static()  
    28. # Student.sex  
    29. tom = Student("tom", 10)  
    30. tom.clss_method()  

    注意:

    1. 可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的
    2. 类方法只能访问类变量,不能访问实例变量

    即:

    三:属性:

    1. class  Person:  
    2.     @property  
    3.     def name(self):  
    4.         return "tom"  
    5.    
    6.     @name.setter  
    7.     def name(self,a):  
    8.         # print(a)  
    9.         yield a  
    10.    
    11.    
    12.     @name.deleter  
    13.     def name(self, a):  
    14.         return a  

     

    写法2

    1. class Persoon1:  
    2.     def get_name(self):  
    3.         print("get_name")  
    4.     
    5.     def set_name(self, name):  
    6.         print("name", name)  
    7.     
    8.     def del_name(self):  
    9.         print("del_name")  
    10.     
    11.     name = property(get_name, set_name, del_name)  

     

  • 相关阅读:
    进程与线程
    HDOJ搜索专题之Catch That Cow
    HDOJ搜索专题之Red and Black
    COJ1026(过河卒)
    HDOJ搜索专题之Prime Ring Problem
    COJ1113(Emperor And His Knight)
    HDOJ搜索专题之胜利大逃亡
    HDOJ搜索专题之翻纸牌游戏
    HDOJ搜索专题之Counting Sheep
    HDOJ搜索专题之Robot Motion
  • 原文地址:https://www.cnblogs.com/twotigers/p/7773676.html
Copyright © 2011-2022 走看看