实例化:基于类来创建对象
形参self——指向实例本身的应用
Example:
class Restaurant():
"""A class representing a restaurant."""
def __init__(self, name, cuisine_type):
"""Initialize the restaurant."""
self.name = name.title()
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""Display a summary of the restaurant."""
msg = self.name + " serves wonderful " + self.cuisine_type + "."
print("\n" + msg)
def open_restaurant(self):
"""Display a message that the restaurant is open."""
msg = self.name + " is open. Come on in!"
print("\n" + msg)
restaurant = Restaurant('the mean queen', 'pizza')
print(restaurant.name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
pizza
The Mean Queen serves wonderful pizza.
The Mean Queen is open. Come on in!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)