Classes in Python

Source: Internet
Author: User

Classes in Python
Content Overview
Concepts of the class:
A class is an abstract set of methods and properties.
Property
instance variables (in each instance memory)
class variables (in-class memory)
Private attribute __var
Method
Construction method
destructor (the default is, the code is empty and the write is equivalent to refactoring it)
Other methods

Object: An instance of a class (an object that is obtained after instantiating a class)
Properties of the class:
Packaging:
The implementation details of some functions are not exposed to external
Inherited:
Inheritance order: (Overwrite, re-inherit, add again)
Inheritance: Code Reuse
How to Inherit:
Single inheritance
Multiple inheritance
2.7 Classic class Depth first
New class Breadth First
3.x all Breadth First
Polymorphic:
Interface reuse (one interface, multiple implementations)


---

1 Create a class:
------

Class keyword, Schoolmember class name (object) New class notation
Class Schoolmember (object):
member = 0 #类变量
def __init__ (self,name,age,sex):
Self.name = Name #实例属性
Self.age = Age
Self.__sex = Sex #定义私有属性
Self.register ()

def print_info (self):
Print ("---info%s start------"%self.name)
For k,v in Self.__dict__.items ():
Print ("\ T", k,v)
Print ("---info%s end------"% self.name)

def print_sex (self):
Print ("%s is%s"% (Self.name,self.__sex))
def register (self):
Schoolmember.member +=1
Print ("Now the member is%s"% schoolmember.member)

def __del__ (self):
"Destructor"
Print ("Del the%s"%self.name)
Schoolmember.member-=1
Print ("Now the member is%s"% schoolmember.member)

T1 = Teacher (' AA ', ' a ', ' M ', ', ' py ') #生成对象
T1.print_sex () #私有属性在类外部无法直接进行访问, can only be accessed through the class method.

Properties of the class:
-----

1. Package:
---

Encapsulation is an abstraction of a specific object that is hidden from the outside of the program, meaning that other programs cannot invoke it.

2. Inheritance:
---

2.1. Single Inheritance
Class Animal:
def __init__ (Self,name,voice=none): # Voice initialization defaults to None
Self.name = Name
Self.voice = Voice
def Say (self):
Print (Self.voice)
def Run (self):
Pass # NULL Action statement (no action)

Class Cdog (Animal): # Inherit class Animal
# def __init__ (Self,name,voice):
# super (Cdog,self) __init__ (Name,voice)
def setvoice (Self,voice): # Subclass Add Function Setvoice
Self.voice = Voice
def run (self): # Subclass overloaded Function Run
Print (' Running ')

Bobo = Cdog (' ala ')
Bobo. Setvoice (' Wang ')
Bobo. Say ()

2.2, more inherit the new class:
Class A (object):
def __init__ (self):
Pass
def f (self):
Print (' A ')

Class B (A):
def __init__ (self):
Pass
# def F (self):
# print (' B ')


Class C (A):
def __init__ (self):
Pass
def f (self):
Print (' C ')

Class D (B,C):
def __init__ (self):
Super (D, self). __init__ ()
# def F (self):
# print (' D ')

c = D ()
C.F ()
Print (d.__mro__)

Output information:
C
(<class ' __main__. D ';, <class ' __main__. B ';, <class ' __main__. C ';, <class ' __main__. A ';, <class ' object ' >)

The inheritance way of the new class: Breadth First, inherit the way of first left and right.
The multiple-inheritance approach of version 3.6 is breadth-first.
The 2.7 version of the new poetry succession is also the principle of breadth first.

Classic-Class Way:
Class A:
def __init__ (self):
Pass
def f (self):
Print (' A ')

Class B (A):
def __init__ (self):
Pass
# def F (self):
# print (' B ')


Class C (A):
def __init__ (self):
Pass
def f (self):
Print (' C ')

Class D (B,C):
def __init__ (self):
#super (D, self). __init__ ()
B.__init__ (self)
C.__init__ (self)
# def F (self):
# print (' D ')

c = D ()
C.F ()
#print (d.__mro__)


2.7 Version output A, the classic class is depth first.
3.6 version Output C, breadth First principle

Classes in Python

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.