Python之类,,# 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()        

Python之类

评论关闭