PYTHON--CLASS

简介:
复制代码
class Robot:
    population = 0
    def __init__(self, name):
        self.name = name
        print("(Initializing {0})".format(self.name))
        Robot.population += 1

    def die(self):
        print("{0} is being destroyed!".format(self.name))
        Robot.population -= 1

        if  Robot.population == 0:
            print("{0} was the last one.".format(self.name))
        else:
            print("There are still {0:d} robots working.".format(Robot.population))
    def  sayHi(self):
        print("Greetings, my masters call me {0}.".format(self.name))
    
    @classmethod
    def howMany(cls):
        print("We have {0:d} robots.".format(cls.population))

droid1 = Robot("R2-D2")
droid1.sayHi()
Robot.howMany()

droid2 = Robot("C-3P0")
droid2.sayHi()
Robot.howMany()

print("\nRobots can do some work here.\n")
print("Robot have finished their work. So let's destroy them.\n")

droid1.die()
droid2.die()

Robot.howMany()

print("=================")

class SchoolMember:
    def __init__(self, name, age):
        self.name =name
        self.age =age
        print('(Initialized SchoolMember: {0})'.format(self.name))

    def tell(self):
        print('Name:"{0}" Age:"{1}"'.format(self.name, self.age), end = " ")

class Teacher(SchoolMember):
    """docstring for ClassName"""
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print('(Initialized Teacher: {0})'.format(self.name))

    def  tell(self):
        SchoolMember.tell(self)
        print('Salary:"{0:d}"'.format(self.salary))

class Student(SchoolMember):
    """docstring for ClassName"""
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print('(Initialized Student: {0})'.format(self.name))
        
    def  tell(self):
        SchoolMember.tell(self)
        print('Marks:"{0:d}"'.format(self.marks))


t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 25, 75)

print()

members = [t, s]
for member in members:
    member.tell()

        
复制代码

目录
相关文章
|
4月前
|
API Python
python 详细理解 import ---- openstack自动import class 到 特定命名空间
python 详细理解 import ---- openstack自动import class 到 特定命名空间
46 0
|
8天前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
32 0
|
30天前
|
Python
Python类(class)中self的理解
Python类(class)中self的理解
18 0
|
1月前
|
存储 设计模式 Python
Python中的类(Class)和对象(Object)
Python中的类(Class)和对象(Object)
29 0
|
8月前
|
数据挖掘 Linux Python
Python学习笔记丨函数和类相关基础知识总结和易错点分享,包括def、lambda、class等
Python学习笔记丨函数和类相关基础知识总结和易错点分享,包括def、lambda、class等
|
4月前
|
Kotlin Python
Python(三十一)python类class继承与多态
Python是一门面向对象语言 面向对象的三大特征:封装、继承、多态 我们这里来看一下python 类的继承 通过继承创建的新类称为子类或派生类,被继承的类称为基类、父类或超类。 一:类的继承 1 :继承语法 arduino 复制代码 class 派生类名(基类名) ... 通常我们定义的类没有指定继承对象都是继承自object类,另外,python是支持多继承的。 下边我们使用代码来演示一下python的继承: 我们定义一个person类,其中有name、age两个属性,getInfo方法,为父类。 我们定义一个school类,其中getSchoolInfo方法,为父类。 定义一
30 0
|
4月前
|
Kotlin Python
Python(三十)python中的类class
Python是一门面向对象语言。 Class类是面向对象中非常重要的一个元素 什么是类呢? Class类是一个功能的合集。 一:类的创建 1 :类的构造函数 python 复制代码 # 定义类 class tests: '测试创建类' # 类文档字符串 projectName='' def __init__(self,name): print("初始化类调用了构造函数") self.projectName = name print("构造函数接收到了参数:",self.projectName) T = tests('时间里
35 0
|
9月前
|
Python
Python Class 08-使用模块和标准库
Python Class 08-使用模块和标准库
|
9月前
|
Python
Python Class 07-再讲函数(闭包与递归)
Python Class 07-再讲函数(闭包与递归)
|
9月前
|
Python
Python Class 06-组合数据类型
Python Class 06-组合数据类型

热门文章

最新文章