首页 > 代码库 > Python基础三:函数和类
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基础三:函数和类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。