Python basic programming: Python class definitions, inheritance and class object to use simple tutorial

This article introduces the definition of Python classes, inheritance and class object to use simple tutorial, we use easy to understand language to explain the definition of the class, use inheritance and class objects, very practical and easy to understand, a friend in need can refer to under
the concept of Python programming classes can be likened to describe a collection of some type, such as "humans" can be seen as a class, and then define each specific human person with this class - you, me, him, etc., as its object. Class also has properties and features some of the features of the property that is the class itself, as human beings have names, attributes such as height and weight, while the specific value will be based on each person is different; it is a function of the type of conduct that can be achieved, as humans have eat, walk and sleep and other functions. Specific forms of the following:

# 例:类的概念
class 人类:
  名字 = '未命名' # 成员变量
  def 说话(内容): # 成员函数
    print 内容      # 成员变量赋初始值 
 
某人 = 人类()    # 定义一个人类对象某人
某人.名字 = "路人甲"
某人.说话  ('大家好')  # 路人甲说话
>>> 大家好!      # 输出

Python defined and used in the form of the class is: class class name [(parent name)]: [member functions and member variables], the name of the class name of the class, and parent class called optional, but the definition of the parent class name after the appropriate subclass has the properties and methods of the parent class. When used as an object class definition, will first call the constructor __init__ to initialize the object of each attribute, each attribute (member variables) of the class can be defined in the constructor, as long as the definition of the object pointer plus enough . And when the object is destroyed, it will call the destructor __del__, a member function of class definition, must be a default variable (similar to C ++, the this pointer) represents an object class definition itself, the name of the variable definable examples will be used below the self variable represents the variable class object.

# 例:类定义及使用
class CAnimal:
  name = 'unname' # 成员变量 
  def __init__(self,voice='hello'):  # 重载构造函数
    self.voice = voice      # 创建成员变量并赋初始值
  def __del__(self):       # 重载析构函数
    pass        # 空操作
  def Say(self):
    print self.voice
 
t = CAnimal()    # 定义动物对象t
t.Say()    # t说话
>> hello      # 输出
dog = CAnimal('wow')  # 定义动物对象dog
dog.Say()      # dog说话
>> wow      # 输出

Python programming classes can bear stepfather class property, in the form of class class name (parent), a subclass can inherit all the methods and properties of the parent class can be overloaded member function and properties of the parent class, it should be noted that the subclass If the member function overloaded parent (ie, the same name), will be used sub-class member functions

# 例:类的继承
class CAnimal:
  def __init__(self,voice='hello'): # voice初始化默认为hello
    self.voice = voice
  def Say(self):
    print self.voice
  def Run(self):
    pass  # 空操作语句(不做任何操作)
 
class CDog(CAnimal):    # 继承类CAnimal
  def SetVoice(self,voice): # 子类增加函数SetVoice
    self.voice = voice
  def Run(self,voice): # 子类重载函数Run
    print 'Running'
 
bobo = CDog()
bobo.SetVoice('My Name is BoBo!')   # 设置child.data为hello
bobo.Say()
bobo.Run()
 
>> My Name is BoBo!
>> Running

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data every day to explain the timing of Python programmers technology, and share some learning methods need to pay attention to small detailsHere Insert Picture Description

Published 20 original articles · won praise 0 · Views 3617

Guess you like

Origin blog.csdn.net/chengxun02/article/details/104998702