wzr83088776 2023-12-23 21:42 采纳率: 75%
浏览 10
已结题

class 模块出现问题

在学习class的时候,我遇到了以下问题:


class Restaurant:
    def _init_(self,restaurant_name,cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
    def describe_restaurant(self):
        print(restaurant_name,cuisine_type)
    def open_restaurant(self):
        print('The restaurant named',self,"is opening")

a_new_restaurant = Restaurant("fashion",5)

a_new_restaurant.describe_restaurant()

结果出错:


```php
Traceback (most recent call last):
  File "c:\Users\asus\Desktop\我的文件\class.py", line 10, in <module>
    a_new_restaurant = Restaurant("fashion",5)
                       ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Restaurant() takes no arguments

这是怎么回事?
请教各位求解!

  • 写回答

2条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-28 07:48
    关注

    【以下回答由 GPT 生成】

    class Restaurant:
        def __init__(self, restaurant_name, cuisine_type):
            self.restaurant_name = restaurant_name
            self.cuisine_type = cuisine_type
    
        def describe_restaurant(self):
            print(self.restaurant_name, self.cuisine_type)  # 使用self来访问实例变量
    
        def open_restaurant(self):
            print('The restaurant named', self.restaurant_name, "is opening")  # 使用self.restaurant_name来访问实例变量
    
    a_new_restaurant = Restaurant("fashion", 5)  # 实例化时需要传入restaurant_name和cuisine_type参数
    
    a_new_restaurant.describe_restaurant()
    

    这段代码中,我做了以下修改: 1. 在describe_restaurant方法中,使用self.restaurant_nameself.cuisine_type来访问实例变量。 2. 在实例化Restaurant类时,传入了restaurant_namecuisine_type参数。 这样就解决了原始代码中出现的TypeError问题。



    【相关推荐】



    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月23日

悬赏问题

  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音