class Restaurant():#创建一个类
def __init__(self,restaurant_name,cuisine_type):#定义初始化属性,2个形参
self.restaurant_name = restaurant_name#通过self 赋值给方法
self.cuisine_type = cuisine_type
def describe_restaurant(self):#定义另一个方法
print(self.restaurant_name)
print(self.cuisine_type)
def open_restaurant(self):
print(self.restaurant_name + " is runging")
my_restaurant = Restaurant(‘alex‘,‘rung‘)#创建一个实例,虽然类没有return ,但但Python自动返回一个表示这个的实例
print(my_restaurant.restaurant_name)
print(my_restaurant.cuisine_type)
my_restaurant.describe_restaurant()#创建一个实例后,可以用.来调用方法
my_restaurant.open_restaurant()
one_restaurant = Restaurant(‘one‘,‘close‘)
one_restaurant.describe_restaurant()
two_restaurant = Restaurant(‘two‘,‘aaa‘)
two_restaurant.describe_restaurant()
shere_restaurant = Restaurant(‘shere‘,‘asdqwe‘)
shere_restaurant.describe_restaurant()
class User():
def __init__(self,first_name,ast_name,age):
self.first_name = first_name
self.ast_name = ast_name
self.abb = age
def describe_user(self):
print(self.first_name)
print(self.abb)
myname = User(‘alex‘,‘wei‘,31,)
myname.describe_user()
print(myname.first_name + " " + myname.ast_name)
原文:https://www.cnblogs.com/upuser/p/13203921.html