本文共 796 字,大约阅读时间需要 2 分钟。
init | |
del |
Class Cat: def __init__(self,new_name): self.name = new_name printf("%s"%self.name) def __del__(self): printf("%s"%self.name)tom = Cat("Tom")print('-'*50)
子类必须在括号内指定父类的名称
与重写父类方法同名,python不会考虑这个父类方法
class ElectricCar(Car): def __init__: self.battery = Battery()
python允许将类存储在某个模块中,然后在主程序中导入所需要的模块
"""一个可用于表示汽车的类""""""存储在car.py"""class Car(): def __init__(self,make,model,year): self.make = male; self.model = model; self.year = year; self.odometer_reading = 0; def update_odometer(self,mileage): if mileage>= self.odometer_reading: self.odometer_reading = mileage; else: print("You can't roll back an odometer_reading")
from Car import Car
使用module_name.class_name访问所需要的类
from random import randitx = randit(1,6)
转载地址:http://nesh.baihongyu.com/