Python Object-oriented

Source: Internet
Author: User

First, composition: Methods and properties.

    1. Class definition:

Class MyClass ():

Def say (self):

Print ("Hello")

Test=myclass ()

Test.say ()

2.

Property:

Self.attr = value

Normal properties:

Self.attr=value

Static properties:

Attr=value

Method:

def func (self):

Common methods:

def func (self):

Only object. Func to invoke

Class method:

def func (CLS):

In principle, the class name. Func should be called, in fact the class name. Func and object. Func can all be called

static method:

def func ():

Object. Func or class name. Func

3. Special methods:

constructor function:

def __init__ (self, name):

Self.name = Name

Other special functions

Len (x) # # x.__len__ ()

A+b # # a.__add__ (b)

a[b]## a.__getitem__ (b)

eg

Class MyClass ():

def __init__ (self, value):

Self.value = value

def __add__ (self, B):

Self.value = Self.value + b.value

def print_value (self):

Print (Self.value)


Class YourClass ():

def __init__ (self, value):

Self.value = value


Test=myclass (1)

Test+yourclass (2)

Test.print_value ()


Second, characteristics: encapsulation, inheritance, polymorphism.

1. Inheritance:

Class Fclass ():

Pass

Class S1class (Fclass):

Pass

Class S2class (Fclass):

Pass

Multiple inheritance:

Class Ssclass (S1class, S2class):

Pass

Multiple inheritance involves the selection order of the methods in the parent class, first of all to understand the noun classic class and the new style class.

Classic Class: Class Fclass ():

New Style classes: Class Fclass (object):

Classic classes are selected in the order of depth preference, i.e. Ssclass->s1class->fclass->s2class

New style classes are chosen in the order of breadth preference, i.e. Ssclass->s1class->s2class->fclass

As much as possible with the new style class, the classic class does not support Super,proterty and so on.

2. Polymorphism:

Python does not support polymorphism. For the already familiar Java, C + +, when using polymorphic, must pass in a specified type, that is, Java and C + + is a strongly typed language, so there is a concept of polymorphism.

For Python implementations, because you do not need to specify a type, you can implement it very simply:

eg

Class Fclass ():

Pass


Class S1class (Fclass):

Def show (self):

Print ("Show1")


Class S2class (Fclass):

Def show (self):

Print ("Show2")


Def show (f):

F.show ()


Show (S1class ())

Show (S2class ())


Python Object-oriented

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.