zoukankan      html  css  js  c++  java
  • Python基础之方法

    class woman:
        pass
    wangdama=woman()
    lidama=woman()
    #查看实例的属性
    #print wangdama.__dict__
    '''
    {}'''

    #为实例添加属性
    wangdama.toufa='yellow'
    #print wangdama.__dict__
    '''
    {'toufa': 'yellow'}
    '''
    #查看实例所属类的属性
    print wangdama.__class__.__dict__
    #实例所属类添加属性
    wangdama.__class__.xiezi="black"
    #wangdama.__class__表示实例所述的类
    print lidama.__class__.__dict__

    #创建方法
    class god:
        def a(self):
            print "sing everyday"
    zongguan=god()
    zongguan.a()
    god().a()#right,god()代表实例,可调用了
    #god.a() #error



    #隐藏属性与隐藏方法(外部无法调用)方法为加__
    class school:
        def __jiaoxuefangfa(self):
            print "%%%^&&%^"
    #school().jiaoxuefangfa() #出错 不可调用,去掉双下划线后可以



    #类常见的专有方法(不需定义系统自带)
    #__init__类一旦调用生成实例就会调用方法
    class people:
        def init(self):
            print 8899
        def __init__(self):
            a='how are you'
            b='fine thank you'
            print a+b
    people()
    #__del__
    class  friend:
        def hi(self):
            print 8989
        def __init__(self):
            print "init 最先执行"
        def __del__(self):
            de='最后调用删除对象'
            print de
    zhang=friend()
    #zhang.hi()#init 最先执行 8989 已执行del函数删除 不会输出
    friend()#没有具体事例,所以不会删除,所以输出了‘最后调用删除’
        

  • 相关阅读:
    非同名数据库导入sql
    mybatis中的异常
    使用MyBatis时为什么Dao层不需要@Repository
    springboot项目启动后执行一段程序的方式
    构建SpringBoot项目
    springboot项目添加webapp目录
    springboot项目启动异常
    使用restTemplate出现的异常
    Spring中Bean的属性赋值@Value()---使用类配置的方式
    使用BeanUtils复制Java对象
  • 原文地址:https://www.cnblogs.com/jietingting/p/7076959.html
Copyright © 2011-2022 走看看