• Python :类中设置默认属性并修改


    class Restaurant():
      def __init__(self,restaurant_name,cuisine_type):

       “”“初学很容易出现 TypeError: __init__() takes exactly 3 arguments (4 given)的错误"""

       """究其原因,是要明白__init__()函数里只设置了两个形参“”“

         ”“”而你在接下来调用方法descript()时,直接设置了三个实参“”“

         ”“”你可以通过设置三个形参或者增加另外的方法,如下面的set_number_served()“”“

         ”“”通过调用set方法设置属性值后再调用descript()即可“”“
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 300
      def describe_restaurant(self):
        print(self.restaurant_name + " "
           + self.cuisine_type + " "
           + str(self.number_served) + ": ")
      def open_restaurant(self):
        print(" is openning! ")
      def set_number_served(self,number):
        self.number_served = number
      def adding(self,num):

      “”“这里要注意先将adding_number置0,如果直接adding_number自加的话会得到你设置值的两倍"""
        self.adding_number = 0
        if self.adding_number >= 0:
          self.adding_number = self.adding_number + num
          print("We can serve: " + str(self.adding_number) + " person!")
        else:
          print("You can below adding_number!")
    restaurant = Restaurant('Jerry&Eurus','sweetmeats')
    restaurant.describe_restaurant()
    restaurant.set_number_served(350)
    restaurant.describe_restaurant()
    restaurant.adding(50)

  • 相关阅读:
    mysql数据库汉字首字母简拼全拼
    window.showModalDialog刷新父窗口和本窗口的方法及注意
    c#.net语句e.Row.RowType == DataControlRowType.DataRow是什么含义?
    SQL 拿到一天内的数据
    在线脚本编辑器
    输入正确的邮箱地址
    jquery中的$(document).ready()方法和window.onload方法区别
    转载从XML文件中读取数据绑定到DropDownList
    GridView中DropDownList联动
    For 循环 和Foreach 的区别
  • 原文地址:https://www.cnblogs.com/hhjfighting/p/7834043.html
Copyright © 2020-2023  润新知