Python基础三:函数和类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础三:函数和类相关的知识,希望对你有一定的参考价值。
- 函数的定义和调用
1 def fun(x): 2 if x > 0: 3 print "x>0" 4 else: 5 print "x<=0" 6 7 a = 3.5 8 b = -1 9 fun(a) 10 fun(b)
- 类的定义和基本调用
1 class Student: 2 def __init__(self, name, age): 3 self.name = name 4 self.age = age 5 6 def studying(self): 7 print self.name, "is studying" 8 9 def playing(self, something): 10 print self.name, "is playing", something 11 12 def info(self): 13 print "Info:", "\n",14 "Name:", self.name, "\n",15 "Age:", self.age 16 17 studentA = Student("studentA", 16) 18 studentA.studying() 19 studentA.playing("football") 20 studentA.info()
- 类的继承:支持重写,多类继承,不直接支持重载
1 class A: 2 def __init__(self): 3 print "A is a parent class" 4 5 def funA(self): 6 print "funA is in class A" 7 8 class subA(A): 9 def __init__(self): 10 print "subA is a child class" 11 12 def funsubA(self): 13 print "funsubA is in class subA" 14 15 parentA = A() 16 childA = subA() 17 childA.funA() 18 childA.funsubA()
以上是关于Python基础三:函数和类的主要内容,如果未能解决你的问题,请参考以下文章