huakai 2019-12-22
创建关于汽车的类
class Cars():
def __init__(self, brand, country):
self.brand = brand
self.country = country
def show_brand(self):
print(self.brand.title() + " is a popular automobile brand.")
def show_country(self):
print("Its headquater is in " + self.country.title() + ".")
my_car = Cars(‘toyota‘, ‘Japan‘)
my_car.show_brand()
my_car.show_country()输出
Toyota is a popular automobile brand. Its headquater is in Japan.
class Singleton: def __new__: # 关键在于这,每一次实例化的时候,我们都只会返回这同一个instance对象 if not hasattr: cls.instance =