面向对象中类的成员



字段:
静态字段
普通字段
PS:静态字段代码加载时候,已经创建

方法
所有的方法属于类
1、普通方法:至少一个self,对象执行
2、静态方法:任意参数, 类执行(对象执行)
3、类方法 :至少一个cls, 类执行(对象执行)
属性
不论不类的东西
具有方法的写作形式,具有字段访问形式


def func(arg):
print(arg)

func(1)
func("alex")
func([11,22,33])



C#/Java

def func(int arg):#指定类型
print(arg)


func(123)
func("alex") #报错



如何实现?

class A:
pass

class B(A):
pass

class C(A):
pass


#arg参数,必须是A类型或A的子类型

def func(A arg):
print(arg)


obj = B()
obj = C()
obj = A()

func(obj)


字段



class Foo:
CC = 123 #静态字段

def __init__(self):
self.name = 'alex' #普通字段


def show(self):
print(self.name)
字段:
一般情况下,自己访问自己的字段

规则:
普通字段只能用对象访问
静态字段用类访问(万不得已的时候才可以使用对象访问)


方法:
普通方法 至少一个self, 对象执行
静态方法 任意参数, 类执行 (对象执行)
类方法 至少一个cls 类执行 (对象执行)



@staticmethod
def f1(a1,a2):
print(a1,a2)

def f2(cls):
print(cls) #cls类名,()创建对象

obj = Province('beijing')

obj.show


属性

class Pager:

def __init__(self, all_count):
self.all_count = all_count

@property #属性的获取
def all_pager(self):
a1, a2 = divmod(self.all_count, 10)
if a2 == 0:
return a1
else:
return a1 +1

@all_pager.setter
def all_pager(self, value):
print(value)
@all_pager.deleter:
print('del all_pager')



p = Pager(101)

#p.all_count#字段
print(p.all_count)

p.all_count = 102

del p.all_count


ret = p.all_pager

print(ret)

p.all_pager = 111


del p.all_pager



属性2



class Pager:

def __init__(self, all_count):
self.all_count = all_count

def f1(self):
pass

def f2(self):
pass

def f3(self):
pass

foo = property(fget=f1,fset=f2,fdel=f3)

p = Pager(101)

result = p.foo
print(result)

p.foo = 'alex'

del p.foo



成员修饰符


私有:
只能类自己本身成员内部访问


公有
pass



class Foo:

#构造方法
def __init__(self, name, age)
self.name = name
self.age = age

#析构方法
def __del__(self):
pass


def __call__(self):
print('call')

def __str__(self):
return "%s - %s" %(self.name, self.age)



obj = Foo()

obj()#对象() 执行call方法

#Foo()()


obj1 = Foo('alex',77)

ret = str(obj1) #str 方法

print(ret)

#__dict__获取对象中封装的数据
ret = obj1.__dict__

print(ret)



def __getitem__(self, item):


#item.start,item.stop,item.step
return 123

def __setitem__(self, key, value):
print('setitem')

def __delitem__(self, key):
print('del item')

dic = {"k1":123} #dic = dict(k1=123)

dic['k1'] #dic()

dic['k1'] = 123

del dic['k1']




#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Alex Li
class Foo:

# 构造方法
def __init__(self, name,age):
self.name = name
self.age = age

# 析构方法
def __del__(self):
pass

def __call__(self):
print('call')

def __str__(self):
return "%s - %d" %(self.name, self.age)

def __add__(self, other):
temp = "%s - %d" %(self.name,other.age)
return temp

def __getitem__(self, item):
# print(type(item),item)
# item.start item.stop item.step
print(type(item))
return 123

def __setitem__(self, key, value):
# key.start key.stop key.step
print(type(key),type(value))

def __delitem__(self, key):
# key.start key.stop key.step
print(type(key))


obj = Foo('alex', 73)
# obj() # call
# 语法对应关系
# ret1 = obj['ad']
ret2 = obj[1:4:2]
obj[1:4] = [11,22,33,44,66]
del obj[1:4]
# print(ret)
# obj['k1'] = 111
# del obj['k1']

# dic = {'k1': 123} # dic = dict(k1=123)
# dic['k1'] # dic()
# dic['k1'] = 123
# del dic['k1']



obj = Foo()
obj() # 对象() 执行call
# Foo()()

obj1 = Foo('alex', 73)
obj2 = Foo('eric', 84)
# 获取对象中封装的数据
ret = obj1.__dict__
print(ret)

# print(Foo.__dict__)

# print(obj1)
# print(obj2)

# ret = str(obj1)
# print(ret)

# ret = obj1 + obj2
# print(ret)
# __dict__



class C1:
def f1(self):
print('c1,f1')
return 123


class C2(C1):
def f1(self):
# 主动执行父类f1方法
ret = super(C2, self).f1()

print('c2.f1')

return ret

# C1.f1(self)


obj = C2()

obj.f1()




#有序字典

class MyDict(dict):

def __init__(self):
self.li = []
super(MyDict, self).__init__()

def __setattr__(self, key, value):
self.li.append(key)
super(MyDict, self).__setattr__()

def __str__(self):
temp_list = []
for key in self.li:
value = self.get(key)
temp_list.append("'%s':%s'" %(key,value,))
temp_str = "{" + ",".join(temp_list) + "}"
return temp_str

obj = MyDict()
obj['k1'] = 123
obj['k2'] = 456

print(obj)

"""


class Foo:

instance = None

def __init__(self,name):
self.name = name

@classmethod
def get_instance(cls):

#cls类名
if cls.instance:
return cls.instance
else:
obj = cls('alex')
cls.instance = obj
return obj


obj1 = Foo.get_instance()
print(obj1)
obj2 = Foo.get_instance()
print(obj2)
posted @ 2016-07-02 12:10  粗哥  阅读(250)  评论(0编辑  收藏  举报