Python Study Notes (7) More abstract

Source: Internet
Author: User

This chapter describes how to create your own objects:

7.2 create your own class

First, you can use a simple class.

_metaclass_=type>>> class Person:def setName(self,name):self.name=namedef getName(self):return self.namedef greet(self):print ("hello %s " %self.name)

The self parameter is equivalent to referencing the object itself. For details, refer to the this pointer in C ++.

>>> daxiao=Person()>>> daxiao.setName("daxiao")>>> daxiao.getName()'daxiao'>>> daxiao.greet()hello daxiao 


7.2.3 privatization

By default, a program can access the features of an object from outside. As in the example above.

Python does not support direct private, but some tips can be used to achieve the effect of private features.

 

class Secretive:def __inaccessible(self):print("belt you can't see me")def accessible(self):self.__inaccessible()>>> a=Secretive()>>> a.__inaccessible()Traceback (most recent call last):  File "
 
  ", line 1, in 
  
       a.__inaccessible()AttributeError: 'Secretive' object has no attribute '__inaccessible'>>> a.accessible()belt you can't see me
  
 


Yes, it's just adding double underscores before the function name. In this way, the function can only be called internally by the class.

In fact, in the internal calls of the class, all names starting with double underscores are translated into the form of single underscores and class names. To access a "private" function, you only need

 a._Secretive__inaccessible()belt you can't see me


In short, it is impossible to ensure that others do not access the object's methods and features.

7.2.5 specify the superclass

Sub-classes can extend the definition of superclass. You can specify the superclass by writing other class names in parentheses after the class statement.

class father:    def a(self):       print("i am man")    def b(self):    print("i am older")class son(father):    def b(self):        print("i am younger")        
b=son()>>> a.a()i am man>>> a.b()i am older>>> b.a()i am man>>> b.b()i am younger

Of course, multiple superclasses can also be inherited.

class father:    def a(self):        print("i am man")    def b(self):        print("i am older")class mother:    def c(self):        print("i am human")class son(father,mother):    def b(self):        print("i am younger")
>>> b=son()>>> b.a()i am man>>> b.b()i am younger>>> b.c()i am human


Note: The methods in the inherited superclass will be overwritten and then the methods in the inherited superclass

7.2.8 interfaces and Provinces

In the face of multi-State objects, you may need to determine which methods are available. For example, the object B in the preceding example contains three methods: a, B, and c.

>>> hasattr(b,'a')True>>> hasattr(b,'d')False


Summary

Object

Class

Polymorphism

Encapsulation

Inheritance

Interface and Introspection





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.