Python - 类(class)的构造器(__init__) 详解 及 代码
2014-03-03 08:41
696 查看
类(class)的构造器(__init__) 详解 及 代码
本文地址: http://blog.csdn.net/caroline_wendy/article/details/20355577Python中, 类的构造器为__init__(双下划线), 里面传递成员变量.
使用self参数, 并把参数(argument)传递至self.val(泛指)的成员变量中.
成员函数, 可以使用self.val, 进行操作.
代码如下:
# -*- coding: utf-8 -*- #==================== #File: ClassExercise.py #Author: Wendy #Date: 2014-03-03 #==================== #eclipse pydev, python2.7 class Person: def __init__(self, name): self.name = name def sayHi(self): print('Hello, my name is ' + self.name) p = Person('Xu Qing') p.sayHi();
输出:
Hello, my name is Xu Qing
本文出自 “永不言弃” 博客,请务必保留此出处http://spikeking.blog.51cto.com/5252771/1387936
相关文章推荐
- Python - 类(class)的构造器(__init__) 详解 及 代码
- Python - 类(class)的变量 和 对象(object)的变量 详解 及 代码
- Python - 类(class)的变量 和 对象(object)的变量 详解 及 代码
- Python实现调度算法代码详解
- Python - 序列(sequence) 详解 及 代码
- Python&OpenCV - 读写(read&write)视频(video) 详解 及 代码
- Python代码的打包与发布详解
- Python __init__.py 作用详解
- Python 递归处理文件和文件夹代码详解
- JavaScript操作class和style样式代码详解
- 详解vue.js之绑定class和style的示例代码
- Python - 序列(sequence) 详解 及 代码
- Python中的 new-style class 及其实例详解
- Python模块搜索路径代码详解
- 详解Python中代码缩进(Indent):影响代码的内在逻辑关系和执行结果
- Python - 格式化(format())输出字符串 详解 及 代码
- python 构造器方法__init__的用法
- Python - 字典(dict) 详解 及 代码
- python -- why defined '__new__' and '__init__' all in a class
- Python 单例模式详解 __new__, import ,__dict__,__metaclass__