python基础学习-类(class)
来源:互联网 发布:js的object对象 编辑:程序博客网 时间:2024/06/05 09:25
python类的基本用法
ython 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2Type "copyright", "credits" or "license()" for more information.
>>> class People:
age = 0
name = ""
sex = True
def __init__(self,age=21,name="xiaobing",sex=True):
self.age = age
self.name = name
self.sex = sex
>>> p = People()
>>> p
<__main__.People instance at 0x7facc0766680>
>>> p.age
21
>>> p.name
'xiaobing'
>>> p.sex
True
>>> p = People(22,"xiaohong",False)
>>> p.age
22
>>> p.name
'xiaohong'
>>> p.sex
False
>>> del People
>>> class People:
def say(self,something="nothing"):
print "say"+str(something)
>>> del p
>>> p = People()
>>> p.say("hello world)
SyntaxError: EOL while scanning string literal
>>> p.say("hello world")
sayhello world
>>> p.say()
saynothing
python类的成员访问权限问题
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> class Test:
def __test(self):
print "test"
>>> t = Test()
>>> t._Test__test
<bound method Test.__test of <__main__.Test instance at 0x7f613cb1a248>>
>>> t.test()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
t.test()
AttributeError: Test instance has no attribute 'test'
>>> class Test:
def __init__(self):
self.__t = 0
self.tt = 0
def _test(self):
print self.tt
def setT(self,t):
self.__t = t
def getT(self):
return self.__t
>>> test = Test()
>>> test.tt
0
>>> test._t
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
test._t
AttributeError: Test instance has no attribute '_t'
>>> test.t
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
test.t
AttributeError: Test instance has no attribute 't'
>>> test.get
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
test.get
AttributeError: Test instance has no attribute 'get'
>>> test.getT()
0
>>> test.setT(10)
>>> test.getT()
10
python的静态成员属性和静态成员方法
>>> class Test:
t = 0
@staticmethod
def test():
print "test"
>>> Test.t
0
>>> Test.test()
test
>>>
python类的继承
>>> class MyList(list):
def myPrint(self):
for item in self:
print item
>>> mList = MyList()
>>> mList.myPrint()
>>> mList.append(1)
>>> mList.myPrint()
1
>>> mList = mList + [1,2,3,4]
>>> mList
[1, 1, 2, 3, 4]
- python基础学习-类(class)
- Python基础学习篇—类(Class)
- Python基础-class类
- python基础篇--Class(类)
- python类class学习笔记
- python学习笔记-10(类class)
- 十五、python class 类学习笔记
- Python学习笔记:类(class)
- Python学习——Class类
- Python 学习之类(Class)
- Python 学习笔记:class
- python基础学习九:类基础
- Python学习----Python基础
- Python之类(Class)的学习
- python 学习之---class (inheritance)
- python学习笔记1---class
- Java,Class类,类装载,反射等基础学习笔记
- 黑马程序员-java学习笔记-反射基础类Class
- Unity3D4.* NGUI制作动态字库
- python import sys sys.path.append()
- 分页概念
- 解决Fiddler "creation of the root certificate was not successful”的问题
- ViewPager + Fragment实现懒加载
- python基础学习-类(class)
- 网络请求Cookie
- 表示数值的字符串
- html中:frameset标签的使用
- 电脑常见问题之-右键无新建文件夹选项
- 第一节补充 配置PYTHON编码格式
- MySQL的列值自增长:auto_increment(一)
- while/do...while循环(笔记整理)
- 【强化学习】MCTS (Monte Carlo Tree Search)