码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础三:函数和类

时间:2017-03-05 17:55:24      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:类的继承   bsp   eth   ini   lda   name   调用   info   div   

  • 函数的定义和调用
     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基础三:函数和类

标签:类的继承   bsp   eth   ini   lda   name   调用   info   div   

原文地址:http://www.cnblogs.com/nightcatcher/p/6505920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!