028class_part2

1、成员修饰符    2、特殊成员   3、metaclass,类的祖宗   
###成员修饰符###
公有和私有

 1 #__author:_nbloser
 2 #date:2018/1/19
 3 
 4 #私有类、对象成员变量,方法;在前面加两个下划线'__'
 5 class  Person:
 6     __lover = '***'             # 私有类字段,外部不能够直接访问
 7     
 8     @staticmethod
 9     def  retu_lover():
10         return  Person.__lover
11         
12     def  __init__(self,name,age):
13         self.name = name
14         self.__age = age              # 私有对象字段,外部不能够直接访问
15 
16     @property
17     def  show_age(self):
18         return  self.__age
19 
20 obj = Person('x',21)
21 print(obj.name)
22 print(obj.show_age)          # 通过python属性获取
View Code

****子类也不能直接访问父类的私有字段

### 特殊成员###
#__call__方法

class  Person:
    def __init__(self):
        print('init')
    def __call__(self,*args,**kwargs):
        print('call')
obj = Person()
obj()          #  这个对应的是执行__call__方法,一般也不用

#__add__方法

class  Person:
    def  __add__(self,other):
        return 123  # self=obj,other=obj2,可以让这个两个对象的字段相加,或者其他操作

obj = Person()
obj2 = Person()
x = obj + obj2    # 两个对象相加时,自动执行第一个对象的__add__方法,并且将第二个对象当作参数传递进入
print(x,type(x)) # 123<class'int'>

#__dict__(重点)

class  Person:
    def  __init__(self, name,age):
        self.name = name
        self.age = age
obj = Person('x',21)
print(obj.__dict__)      # {'name':'x','age':21} 
# 通过字典把成员显示出来

## __getitem__、__setitem__、__delitem__方法、切片
只有__getitem__有返回值

class Person:
    def  __init__(self,name,age):
        self.name = name
        self.age = age

    def  __getitem__(self,item):
        return  item + 10

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

    def  __delitem__(self,key):
        print(key)

k = Person('x',21)
c = k[8]                            # 对应执行__getitem__方法
print(c)
k[8] = 235                      # 8    235     # 对应执行__setitem__方法
del   k[8]                       # 8             # 对应执行__delitem__方法

加上切片处理的,以__getitem__方法为例

def  __getitem__(self,item):
    # 如果item是基本类型:int,str,索引获取。如果是slice对象的话,切片
    if  type(item) == slice:
        print(item.start)
        print(item.stop)
        print(item.step)
        print('切片处理')
    else:
        print('索引处理')
    
c = k[8]         # 索引处理
c = k[8:10:2]       # 切片处理

##实现对象可以迭代

class  Person:
    def  __init__(self,name,age):
        self.name = name
        self.age = age
    
    def  __iter__(self):
        return  iter([11,22,33])

l = Person('x',21)
for i in  l:
    print(i)

这里只是介绍。

###metaclass,类的祖宗###

def function(self):
    print(213)
Foo = type('foo',(object,),{'func':function})
r = Foo()
r.func()

这样也是声明了一个类

这个我不知道为什么实现不了,所以我直接截图了。而且很懵,不知道用得多不多,暂时没有找别的博客,好像在Java里面好像没有看到使用太多这些。
先执行type的方法,才执行类的方法

 

obj真正是在__new__里面创建的
盗过来的创建对象流程图,作者:武沛齐

 

 

一些有关的代码:(有的话会继续编辑补上)

1、example_043  in  java_300

 1 public class Book
 2 {
 3     private String title;
 4     private String author;
 5     private double price;
 6     public Book(String title, String author, double price) {
 7         super();
 8         this.title = title;
 9         this.author = author;
10         this.price = price;
11     }
12     public String getTitle()
13     {
14         return title;
15     }
16     public String getAuthor()
17     {
18         return author;
19     }
20     public double getPrice()
21     {
22         return price;
23     }
24     
25 }
Book.java
 1 public class Test
 2 {
 3     public static void main(String[] args)
 4     {
 5         // TODO 自动生成的方法存根
 6         Book book = new Book("x", "_nblsoer", 99999.99);
 7         System.out.println("书名:"+book.getTitle());
 8         System.out.println("作者:"+book.getAuthor());
 9         System.out.println("价格:"+book.getPrice());
10     }
11 }
run.java
 1 # __author: _nbloser
 2 # date: 2018/1/22
 3 
 4 class Book:
 5     def __init__(self, book_name, author, price):
 6         self.__book_name = book_name
 7         self.__author = author
 8         self.__price = price
 9 
10     @property
11     def book_name(self):
12         return self.__book_name
13 
14     @book_name.setter
15     def book_name(self, val):
16         self.__book_name = val
17 
18     @property
19     def author(self):
20         return self.__author
21 
22     @author.setter
23     def author(self, val):
24         self.__author = val
25 
26     @property
27     def price(self):
28         return self.__price
29 
30     @price.setter
31     def price(self, val):
32         self.__price = val
book.py
 1 # __author: _nbloser
 2 # date: 2018/1/22
 3 
 4 import book
 5 book = book.Book('xxxx','_nbloser',964)
 6 print(book.book_name,book.author,book.price)
 7 book.book_name = 'change'
 8 book.author = 'a loser'
 9 book.price = 99999999.99
10 print(book.book_name,book.author,book.price)
run.py

2、

 

posted @ 2018-01-21 13:56  Alos403  阅读(211)  评论(0编辑  收藏  举报