鱼C论坛

 找回密码
 立即注册
查看: 1574|回复: 1

python子类属性方法案例,无法运行

[复制链接]
发表于 2018-1-3 08:41:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
class Car():
    """一次模拟汽车的简单尝试"""

    def _init_(self,make,model,year):
        self.make=make
        self.model=model
        self.year=year
        self.odometer_reading=0

    def get_descriptive_name(self):
        long_name=str(self.year)+' '+self.make+' '+self.model
        return long_name.title()

    def read_odometer(self):
        print("This car has"+str(self.odometer_reading)+"miles on it.")

    def update_odometer(self,mileage):
        if mileage>=self.odometer_reading:
            self.odometer_reading=mileage
        else:
            print("You can't roll back an odometer!")

    def increment_odometer(self,miles):
        self.odometer_reading+=miles


class ElectricCar(Car):
    """Represent aspects of a car,specific to electric vehicles."""
    def _init_(self,make,model,year):
        """电动汽车的独特之处
        初始化父类的属性,再初始化电动汽车特有的属性
        """
        super()._init_(make,model,year)
        

my_tesla=ElectricCar('tesla','model s',2016)
print(my_tesla.get_descriptive_name())

   

===================== RESTART: C:/python3.7/case9.3.3.py =====================
Traceback (most recent call last):
  File "C:/python3.7/case9.3.3.py", line 36, in <module>
    my_tesla=ElectricCar('tesla','model s',2016)
TypeError: ElectricCar() takes no arguments
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-1-3 09:10:23 | 显示全部楼层
init左右两边是两个下划线
__init__
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-26 21:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表