设为首页 收藏本站
查看: 797|回复: 0

[经验分享] Python学习(七)面向对象 ——类和实例

[复制链接]

尚未签到

发表于 2015-11-30 10:55:18 | 显示全部楼层 |阅读模式
Python 面向对象 —— 类和实例
  

  类
  虽然 Python 是解释性语言,但是它是面向对象的,能够进行对象编程。至于何为面向对象,在此就不详说了。面向对象程序设计本身就很值得深入学习,如要了解,请参阅网上其他的资料。
  面向对象最重要的概念就是类(Class)和实例(Instance),牢记 类 是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同。
  以Student类为例,在Python中,定义类是通过 class 关键字:
  注意:Python 2.X 需要在类名后面加 (object)  这边 pass 语句表示空语句段。
        class 后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是(object),表示该类是从哪个类继承下来的,继承的概念我们后面再讲,通常,如果没有合适的继承类,就使用object类,这是所有类最终都会继承的类。



class Student(object):
pass
  Python 3.X 则只需要类名,不需要加 (object)



class Student:
pass
  

  实例
  创建实例是通过类名+()实现的(若 __init__ 无或仅有self);如定义好了Student类,就可以根据Student类创建出Student的实例,如下:



class Student(object):
pass
May = Student()                    # 新建May实例
print(May)
可以看到,变量May指向的就是一个Student的object,后面的0x006C4770是内存地址,每个object的地址都不一样,而Student本身则是一个类。
  可以自由地给一个实例变量绑定属性,比如,给实例 May 绑定一个 name 属性,这个 name 属性是实例 May 特有的,其他新建的实例是没有 name 属性的



1 class Student(object):
2     pass
3 May = Student()                  # 新建May实例
4 print(May)
5 May.name = "May"                 # 给实例 May 绑定 name 属性为 "May"
6 print(May.name)
7 Peter = Student()                # 新建Peter实例
8 # print(Peter.name)              # 报错,因为Peter没有Name属性
  那么,如果我们需要类必须绑定属性,那如何定义呢?  请参见下文。
  

  __init__ 构造函数
  由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。 (注意 __init__ 双下划线)
  如对于Student类,我们定义 name 和 score 属性(所有Sudent 都须有的属性):
  __init__方法的第一个参数永远是self,表示创建的实例本身,因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。
  有了__init__方法,在创建实例的时候,就不能传入空的参数了,必须传入与__init__方法匹配的参数,但self不需要传,Python解释器自己会把实例变量传进去:



1 class Student(object):
2     def __init__(self, name, score):
3         self.name = name
4         self.score = score
5
6 May = Student("May",90)           # 须要提供两个属性
7 Peter = Student("Peter",85)
8 print(May.name, May.score)
9 print(Peter.name, Peter.score)
  

  __del__ 析构函数

  Just like the __init__ method, there is another special method __del__ which is called when an object is going to die i.e. it is no longer being used and is being returned to the computer system for reusing that piece of memory.
  The __del__ method is run when the object is no longer in use and there is no guarantee when that method will be run. If you want to explicitly see it in action, we have to use the del statement which is what we have done here.

  相对于 构造函数 Python 也有类似 C++ 中的析构函数 __del__ , Python的垃圾回收过程与常用语言的不一样,如果一定需要,最好需要使用del语句来激活。
  

  私有变量

  Note for C++/Java/C# Programmers
  All class members (including the data members) are public and all the methods are virtual in Python.
  One exception: If you use data members with names using the double underscore prefix such as __privatevar, Python uses name-mangling to effectively make it a private variable.
  Thus, the convention followed is that any variable that is to be used only within the class or object should begin with an underscore and all other names are public and can be used by other classes/objects. Remember that this is only a convention and is not enforced by Python (except for the double underscore prefix).

  Python 中定义私有变量,命名规则为前缀加两个下划线 “__” ,注意不可前后都包含__XXX__(该命名表示为类属性或内建变量);还有一种命名为单下划线 _XXX ,表示约定俗成不可访问的变量。



1 class Person(object):
2     def __init__(self,name,sex):
3         self.name = name
4         self.__sex = sex             # sex 定义为私有变量
5         
6     def print_title(self):
7         if self.sex == "male":
8             print("man")
9         elif self.sex == "female":
10             print("woman")
11
12 May = Person("May","female")
13 print(May.name)        
14 print(May.__sex)                     # 会报错

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-145298-1-1.html 上篇帖子: python学习:两个py文件间的函数调用 下篇帖子: Python函数小结(2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表