2.25

Posted phyllissryo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.25相关的知识,希望对你有一定的参考价值。

 

 

技术图片
  1 # 练习9-1
  2 """9-1 餐馆 :创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名
  3 为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
  4 根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用前述两个方法。
  5 9-2 三家餐馆 :根据你为完成练习9-1而编写的类创建三个实例,并对每个实例调用方法describe_restaurant() 。
  6 9-3 用户 :创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名
  7 为describe_"""
  8 
  9 class Restaurant():
 10     """餐馆信息"""
 11 
 12     def __init__(self, name, type):
 13         """餐馆基本信息"""
 14         self.name = name
 15         self.type = type
 16 
 17     def describe(self):
 18         """sasas"""
 19         print(self.name.title() + " is " + self.type.title() + " restaurant.")
 20 
 21     def open_restaurant(self):
 22         print(self.name.title() + " is openning.")
 23 
 24 
 25 hitakaya = Restaurant("hitakaya", "Japanese")
 26 hitakaya.describe()
 27 hitakaya.open_restaurant()
 28 matsuya = Restaurant("matsuya","japanese")
 29 matsuya.describe()
 30 haidilao = Restaurant("haidilao","Chinese")
 31 haidilao.describe()
 32 
 33 class User():
 34     """储存用户信息"""
 35     def __init__(self,first_name,last_name):
 36         """初始化信息"""
 37         self.first_name = first_name
 38         self.last_name = last_name
 39 
 40     def describe(self):
 41         """pirnt uesrs‘s infos."""
 42         print(self.first_name.title() + " " + self.last_name.title())
 43 
 44     def greet_user(self):
 45         print(
 46             self.first_name.title() + " "  + self.last_name.title() +
 47             " Welcome!")
 48 
 49 phyllis_ryo = User("phyllis","ryo")
 50 phyllis_ryo.describe()
 51 phyllis_ryo.greet_user()
 52 """
 53 class Car():
 54 --snip--
 55   ? def update_odometer(self, mileage):
 56   """将里程表读数设置为指定的值"""
 57   self.odometer_reading = mileage
 58   my_new_car = Car(‘audi‘, ‘a4‘, 2016)
 59    print(my_new_car.get_descriptive_name())
 60 ? my_new_car.update_odometer(23)
 61  my_new_car.read_odometer()
 62 """
 63 
 64 class Restaurant():
 65     """餐馆信息"""
 66     def __init__(self, name, type):
 67         """餐馆基本信息"""
 68         self.name = name
 69         self.type = type
 70         self.number_served = 0
 71 
 72     def describe(self):
 73         """sasas"""
 74         print(self.name.title() + " is " + self.type.title() + " restaurant.")
 75 
 76     def open_restaurant(self):
 77         print(self.name.title() + " is openning.")
 78 
 79     # def number_served(self): # 此处函数名与参数名重合
 80     def print_number_served(self):
 81         print(str(self.number_served) + " people have been this restaurant.")
 82 
 83     def set_number_served(self,number_served):
 84         self.number_served = number_served
 85 
 86     def increment_number_served(self,increment_served):
 87         self.number_served += increment_served
 88 
 89 
 90 matsuya = Restaurant("matsuya","japanese")
 91 matsuya.describe()
 92 matsuya.print_number_served()
 93 matsuya.number_served = 200
 94 matsuya.print_number_served()
 95 matsuya.set_number_served(100)
 96 matsuya.print_number_served()
 97 matsuya.increment_number_served(10)
 98 matsuya.print_number_served()
 99 
100 
101 class User():
102     """储存用户信息"""
103     def __init__(self,first_name,last_name):
104         """初始化信息"""
105         self.first_name = first_name
106         self.last_name = last_name
107         self.login_attempts = 0
108 
109     def describe(self):
110         """pirnt uesrs‘s infos."""
111         print(self.first_name.title() + " " + self.last_name.title())
112 
113     def greet_user(self):
114         print(
115             self.first_name.title() + " "  + self.last_name.title() +
116             " Welcome!")
117 
118     def increment_login_attempts(self):
119         self.login_attempts += 1
120 
121     def reset_login_attempts(self):
122         self.login_attempts = 0
123 
124 phyllis = User("phyllis","ryo")
125 phyllis.describe()
126 phyllis.increment_login_attempts()
127 phyllis.increment_login_attempts()
128 phyllis.increment_login_attempts()
129 print(phyllis.login_attempts)
130 phyllis.reset_login_attempts()
131 print(phyllis.login_attempts)
132 
133 
134 class ElectricCar(Car):
135     """电动汽车的独特之处"""
136     def __init__(self, make, model, year):
137         """初始化父类的属性"""
138             super().__init__(make, model, year)
139 9.3.2 Python 2.7中的继承
140 在Python 2.7中,继承语法稍有不同,ElectricCar 类的定义类似于下面这样:
141 class Car(object):
142     def __init__(self, make, model, year):
143         --snip--
144     class ElectricCar(Car):
145         def __init__(self, make, model, year):
146             super(ElectricCar, self).__init__(make, model, year)
147             --snip--
View Code

 

以上是关于2.25的主要内容,如果未能解决你的问题,请参考以下文章

寒假作业,2.25,G - iBoard

2.25

2.23/2.24/2.25 find命令 2.26 文件名后缀

上周热点回顾(2.25-3.3)

2.25-3.3博客精彩回顾

2.25python属性和方法私有化