zoukankan      html  css  js  c++  java
  • 面向对象3-析构函数和私有属性

    1 析构函数:在实例释放,销毁的时候自动执行的。通常用于做一些收尾工作,如关闭一些数据库链接,关闭一些打开的临时文件等。释放内存。

    Python的自动回收机制:隔一段时间,Python会扫描内存中那些没有被变量引用的值(有没有门牌号),如果没有被任何一个变量引用的话,就会自动删除。

    class Person:
        cn="China"
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def got_shot(self):
            print("ah...,I got shot...")
        def __del__(self): #析构函数,不能传任何参数
            print("彻底玩完了,析构函数执行")
    p1=Person('Lily',22)
    p1.got_shot()
    p2=Person('Jack',22)
    p2.got_shot()
    

     执行结果:

    ah...,I got shot...
    ah...,I got shot...
    Lily彻底玩完了,析构函数执行
    Jack彻底玩完了,析构函数执行

    2.用del主动释放内存, del 删除的是变量名(门牌号)。不是内存。当发现某个变量没有被引用时(没有门牌号了),这块内存才会被清除。

    class Person:
        cn="China"
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def got_shot(self):
            print("ah...,I got shot...")
        def __del__(self): #析构函数,不能传任何参数
            print("%s彻底玩完了,析构函数执行"%self.name)
    p1=Person('Lily',22)
    p1.got_shot()
    del p1
    p2=Person('Jack',22)
    p2.got_shot()
    

     运行结果:

    ah...,I got shot...
    Lily彻底玩完了,析构函数执行
    ah...,I got shot...
    Jack彻底玩完了,析构函数执行

    3.私有属性:通过p1.age可以任意修改P1的年纪,现在想办法使它变成一个私有属性。

       私有属性和私有方法一样,都是在前面加两个小下划线就可以了。

    __self.name=name

    __got_shot(self):

    class Person:
        cn="China"
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def got_shot(self):
            print("ah...,I got shot...")
    
    p1=Person('Lily',22)
    p1.age=23
    print(p1.age)
    
    p2=Person('Jack',22)
    

     把age改成私有属性以后,居然还可以正常赋值以及执行。郁闷啊啊啊啊啊啊啊啊啊啊-------------------,已经通过博问解决。参照最下面的截图。

    class Person:
        cn="China"
        def __init__(self,name,age):
            self.name=name
            self.__age=age
        def got_shot(self):
            print("ah...,I got shot...")
    
    p1=Person('Lily',22)
    p1.__age=23
    print(p1.__age)
    
    p2=Person('Jack',22)
    

     运行结果:

    23

    4.如何从外部访问私有属性:

    class Person:
        cn="China"
        def __init__(self,name,age):
            self.name=name
            self.__age=age
        def got_shot(self):
            print("ah...,I got shot...")
    
    p1=Person('Lily',22)
    #p1.__age=23
    print(p1.__age)
    
    p2=Person('Jack',22)
    

     运行结果:

    Traceback (most recent call last):
      File "C:/abccdxddd/Oldboy/Py_Exercise/Day6/aaa.py", line 11, in <module>
        print(p1.__age)
    AttributeError: 'Person' object has no attribute '__age'

    那么怎么样从外部去访问私有属性呢?在内部定义了一个方法 show_status(self):

    class Person:
        cn="China"
        def __init__(self,name,age):
            self.name=name
            self.__age=age
        def got_shot(self):
            print("ah...,I got shot...")
        def show_status(self):
            print("age is: %s"%self.__age)
    
    p1=Person('Lily',22)
    p1.show_status()
    

     运行结果:

    age is: 22

    关于私有属性的博问:https://q.cnblogs.com/q/97004/

  • 相关阅读:
    WeakHashMap、IdentityHashMap 、EnumMap简单了解——高淇JAVA300讲笔记之其他Map实现类
    Properties简介——高淇JAVA300讲笔记之Hashtable
    简单实现一个自定义的HashMap——高淇JAVA300讲笔记之HashMap
    简单实现一个自定义的HashSet——高淇JAVA300讲笔记之HashSet
    Enumeration接口——高淇JAVA300讲笔记之其他容器
    Queue接口——高淇JAVA300讲笔记之其他容器
    Collections类的常用方法——高淇JAVA300讲笔记之Collections类
    排序容器——高淇JAVA300讲笔记之TreeSet与TreeMap
    类的引用类型成员
    CentOS5.4下安装codeblocks 12.11
  • 原文地址:https://www.cnblogs.com/momo8238/p/7250932.html
Copyright © 2011-2022 走看看