Python learns to record seven---inheritance, polymorphism, and encapsulation

Source: Internet
Author: User

1. Create a class

Create File test7.py
#! /usr/bin/env python
Class Person:

def setName (self, name):
Self.name = Name
def getName (self):
Return Self.name
def greet (self):
Print "Hello, world! I am%s. " % Self.name

Foo = person ()
Foo.setname (' Yilia ')
Foo.getname ()
Foo.greet ()

2. Characteristics, Functions, methods
Private variable: Just precede its name with a double underline:
Class secretive:

def __inaccessible (self):
Print "Bet you can ' t see me"

def accessible (self):
Print "This secret message is:"
Self.__inaccessible () # Internal Call private method

s = secretive ()
S.accessible ()
S.__inaccessible () #外部调用私有方法

3. Namespaces for classes

>>> class Counter:
... b = 0
... def init (self):
... self.b + = 1
...
>>> NUM1 = Counter ()
>>> Num1.init ()
>>> counter.b
0
>>> Num1.init ()
>>> counter.b
0
>>> num1.b
2
>>> num2 = Counter ()
>>> Num2.init ()
>>> counter.b
0
>>> num2.b
1

4, specify the superclass to write the other class name in parentheses after the class statement can be specified in the superclass, multiple superclass with "," delimited

(1) using Super-class
Class Filter:
def init (self):
self.blocked = []
def filter (self, sequence):
return [x for x in sequence if x not in self.blocked]

Class Spamfilter (Filter):
def init (self):
self.blocked = [' SPAM ']

(2) See if a class is a subclass of another class, using Issubclass
>>> Issubclass (Spamfilter, Filter)
Ture
>>> Issubclass (Filter, Spamfilter)
False

(3) If you want to know the base class of a Class (you), you can use its special properties __bases__
>>> class Counter:
... b = 0
... def init (self):
... self.b + = 1
...

>>> counter.__bases__
()

>>> class AddCounter (Counter):
... def init (self):
... print "This is subclass"
...
>>> Issubclass (AddCounter, Counter)
True
>>> addcounter.__bases__
(<class __main__. Counter at 0x7fcd9b5db328>,)
>>>

5. Interface and Introspection

Python learns to record seven---inheritance, polymorphism, and encapsulation

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.