zoukankan      html  css  js  c++  java
  • python基础(16)私有类,类,类变量

    1.私有类:

    class My:
    def test(self):
    self.__password = 123456
    def say(self):
    print('password',self.__password)
    def __set_password(self):
    self.__password = 7890
    def update(self):
    self.__set_password()

    m = My()
    m.test()
    m.say()
    m.update()
    m.say()

    2.类:
    class My:  #经典类
    pass

    class My2(object): #新式类
    pass

    class My3(): #新式类
    pass

    country = 'China'

    class Person:
    def __init__(self,name):
    print('开始了',name)
    self.name = name # 实例变量
    self.age = 18
    # 人的名字是 马小
    self.data = '哈哈哈'
    self.addr = '南京'

    def cry(self,size):
    print('%s在%s哭。年龄是%s'%(size,self.name,self.age))
    self.size = '大哭'
    self.date = '20190106'
    def say(self):
    print('我叫%s,我住在%s.'%(self.name,self.addr))
    print(self.size)
    print('Jin天的日期',self.date)
    self.help()
    def help(self):
    print(self.date)
    mcb = Person('马小')
    cq = Person('陈大')
    mcb.grilfriend = cq #
    print('马小的女朋友',mcb.grilfriend.name)
    mcb.grilfriend.cry('大')
    # mcb = Person('马小')
    # mcb.addr = '北京'
    # mcb.cry('大') #cry(mcb,大)
    # mcb.say()
    #
    # def car(self,car_name): #
    # self.car_name = car_name
    # #函数里面给mcb加了一个变量
    #
    # car(mcb,'奔驰')#把mcb这个实例传给了self,self指的就是mcb这个实例
    #
    # print(mcb.car_name)

    # cq = Person('陈大')
    # cq.cry('大') #cry(cq,大)
    # print('cq....name',cq.name)
    # print('cq....name',cq.age)

    # print('mcb的内存地址',id(mcb))
    # print('mcb....name',mcb.name)
    # print('mcb....age',mcb.age)
    # print(mcb.data)

    # def abc(self,name):
    # print(self)
    # print(name)
    # abc(111,'马小')
    3.类变量
    class Car:
    wheel = 4 #类变量
    def __init__(self,color,p):
    self.color = color #实例变量
    self.p = p

    def help(self):
    print('汽车有%s个轮子'%self.wheel)
    print('汽车的颜色是%s'%self.color)
    print('牌子%s'%self.p)
    print('='*10)
    self.haha()
    self.check_wheel()

    @classmethod
    def check_wheel(cls):
    print('cls的内存地址',id(cls))
    print(cls.wheel)
    cls.haha()

    @classmethod
    def haha(cls):
    print('哈哈哈')
    cls.help2()

    @staticmethod
    def help2():
    print('这个类的作用是造汽车,它里面有xxx方法')

    @property
    def yesterday(self):
    import datetime
    res = datetime.date.today() + datetime.timedelta(-1)
    return str(res)

    def tom(self):
    import datetime
    res = datetime.date.today() + datetime.timedelta(1)
    return str(res)

    Car.help2()
    mcb = Car('赤橙黄绿青蓝紫','马春波')
    print(mcb.yesterday)

    print(mcb.tom())

    # mcb.wheel = 28
    # mcb.haha()
    # mcb.help()
    # mcb.help2()
    #

    # print('Car的内存地址',id(Car))
    # Car.check_wheel()



    # Car.wheel= 19
    # print(Car.wheel)

    #
    # benc = Car('黑色','奔驰')
    # benc.help()
    #
    # bmw = Car('白色','宝马')
    # bmw.help()


  • 相关阅读:
    LCA算法
    poj1364(差分约束系统)
    dij算法为什么不能处理负权,以及dij算法变种
    差分约束系统
    最短路专辑
    LightOJ1348 树链剖分
    FZU2082树链剖分
    HYSBZ1036 树链剖分
    poj3237 树链剖分 暴力
    poj2763 树链剖分(线段树)
  • 原文地址:https://www.cnblogs.com/yulinlincoding/p/10752967.html
Copyright © 2011-2022 走看看