码迷,mamicode.com
首页 > 其他好文 > 详细

类定义

时间:2016-05-15 12:42:14      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:python class

1.类的定义

class Role(object):
    def __init__(self,name,role,weapon,life_value):
        self.name = name
        self.role = role
        self.weapon = weapon
        self.life_val = life_value

    def buy_weapon(self,weapon):
        print("%s is buying [%s]" %(self.name,weapon))
        self.weapon = weapon

p1 = Role("san","Police","B10",90)
t1 = Role("liwen","Terrorist","B11",100)

p1.buy_weapon("AK47")
t1.buy_weapon("B51")

print("P1:",p1.weapon)
print("T1:",t1.weapon)


2.类的继承

#!/usr/bin/env python
#-*- coding:utf-8 –*-


class SchoolMember(object):
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex

    def enroll(self):
        print("SchoolMember [%s] is enrolled!" %self.name)


class Teacher(SchoolMember):
    def __init__(self,name,age,sex,course,salary):
        super(Teacher,self).__init__(name,age,sex)
        self.course = course
        self.salary = salary

    def teaching(self):
        print("Teacher [%s] is teaching [%s]" %(self.name,self.course))


class Student(SchoolMember):
    def __init__(self,name,age,sex,course,tution):
        super(Student,self).__init__(name,age,sex)
        self.course = course
        self.tution = tution

    def pay_tution(self):
        print("cao,student [%s] paying tution [%s]" %(self.name,self.tution))


#t1 = Teacher("alex",21,"F","PY",1000)
s1 = Student("san",24,"M","PY",500)


本文出自 “AMY” 博客,请务必保留此出处http://amy2015.blog.51cto.com/11407620/1773513

类定义

标签:python class

原文地址:http://amy2015.blog.51cto.com/11407620/1773513

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