python类

# # -*- coding:UTF-8 -*-
# # -*- coding: UTF-8 -*-
# # 注意类名后面有个冒号,在block块里面就可以定义属性和方法了。当一个类定义完之后,就产生了一个类对象。类对象支持两种操作:引用和实例化。引用操作是通过类对象去调用类中的属性或者方法,而实例化是产生出一个类对象的实例,称作实例对象。
# class people:
# name = 'jack'
# age = 12
# __name= 'xiaojack'#私有属性
# __age = 11
# def getName(self):
# return self.__name
# def getAge(self):
# return self.__age
# p = people()
# print(p.name,p.age)
# print(p.getAge(),p.getName())#访问私有属性的方法
# #私有属性的访问?
# # __new__():__new__()在__init__()之前被调用,
# # 用于生成实例对象。利用这个方法和类属性的特性可以实现
# # 设计模式中的单例模式。单例模式是指创建唯一对象吗,单例模
# # 式设计的类只能实例化一个对象
# # 单例模式????
# class Singleton(object):
# __instance = None
# def __init__(self):
# pass
# def __new__(cls,*args,**kwg):
# if Singleton.__instance is None:
# Singleton.__instance = object.__new__(cls,*args,**kwg)
# return Singleton.__instance
# class Fruit(object):
# def __init__(self,color="red",price=0):
# self.__color = color
# self.__price = price
# def __getattribute__(self, item):
# return object.__getattribute__(self,item)
# def __setattr__(self, key, value):
# self.__dict__[key] = value
# if __name__ == "__main__":
# fruit = Fruit("blue",10)
# print(fruit.__dict__.get("_Fruit__color"))
# fruit.__dict__["_Fruit__price"] = 5
# print(fruit.__dict__.get("_Fruit__price"))
# class FruitShop:
# def __getitem__(self, i):
# return self.fruits[i]
# if __name__ == "__main__":
# shop = FruitShop()
# shop.fruits =["apple","banana"]
# print(shop[1])
# for item in shop:
# print(item)
# #多个序列呢?
# class Fruit:
# '''Fruit类'''
# def __str__(self):
# return self.__doc__
# if __name__ == "__main__":
# fruit =Fruit()
# print(str(fruit))
# print(fruit)
# # __call__():在类中实现__call__()方法,可以在对象创建时直接返回__call__()的内容。使用该方法可以模拟静态方法。代码例子
# class Fruit:
# class Growth:
# def __call__(self):
# print("grow ...")
# grow = Growth()
# if __name__ == '__main__':
# fruit =Fruit()
# fruit.grow()
# fruit.grow()

posted on 2017-10-25 08:59  Jt00  阅读(307)  评论(0编辑  收藏  举报

导航