Home  >  Article  >  Backend Development  >  Python类的用法实例浅析

Python类的用法实例浅析

WBOY
WBOYOriginal
2016-06-06 11:17:471262browse

本文实例讲述了Python类的用法。分享给大家供大家参考。具体如下:

先看一段代码:

#!/usr/bin/env python
class Test:
  def __init__(self,msg="hello"):
    self.wel=msg
    print "init"
  def go(self,name,do):
    print self.wel+"go! "+name+" "+do
d=Test("hi,")
d.go("naughty","fight")

上面的代码演示了:

1、构造函数以及带参数(参数有默认值)构造函数

2、构造类实例

3、使用类实例调用类方法

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn