Python之类

# python 学习之类
# 语法:
#   class 类名():
#       def __init__(self, 属性名...)
#       def 方法名(self, 形参)
# 导入单个类: from 模块名 import 类名
# 可以在一个模块存储多个类
# 从一个模块中导入多个类: from 模块名 import 类名1,类名2
# 导入整个模块: import 模块名

print("python 类")

# 定义类
class Car():
    """定义汽车类"""    #文档归类字符串
    def __init__(self, make, modle, age):
        self.make = make
        self.modle = modle
        self.age = age

    def getCarInfo(self):
        print("Car: "+ str(self.make) + "\t"+str(self.modle)+"\t"+str(self.age))

car = Car("奔驰", "S300", 2018)
car.getCarInfo()


# 继承类
class ElecCar(Car):     #继承语法  class 子类名(父类名):
    def __init__(self, make, modle, age):
        super().__init__(make, modle, age) #super()获得超类引用
        self.car_class = "elec car"        #新加属性

    def getCarInfo(self):               #函数重载
        print(self.car_class + ": "+ str(self.make) + "\t"+str(self.modle)+"\t"+str(self.age))
car = ElecCar("1", "2", 2018)
car.getCarInfo()


        

 

posted @ 2018-09-24 22:51  前端人生  阅读(311)  评论(0编辑  收藏  举报