python面向对象基础之类与实例


#类用class,命名每个单词首字母大写,表示一类事物
class Model:
    """
    输出模特
    名称
    国籍
    性别
    身高
    体重
    喜欢的地方
    """
#属性 =  变量

    name = "x"
    nationality = "x"
    six = "x"
    height = 0
    weight = 0
    love_place = ()

#方法:self为形参,也可以设其他值,self代表类的实例,并非类
    def dance(self, person):
        print(f"和{person}跳舞")
    def sing(self, person):
        print("唱歌")
    def basketball(self):
        print("打篮球")
    def eat(self):
        print("吃饭")
    def swim(self):
        print("她在游泳")

if __name__ == "__main__":
#建实例对象,cool_model是一个Model这类事物的实例
    cool_model = Model()
    cool_model.name = "周杰伦"
    print(cool_model.name)
    cool_model.dance("刘畊宏")
    cool_model.love_place = ("重庆", "巴黎", "纽约", "上海")
    print("这位模特喜欢的地方", cool_model.love_place)

    print("*******************来一个华丽的分割线哈哈哈*******************")
#创建实例对象,beautful_model是一个Model这类事物的实例
    beauutful_model = Model()
    beauutful_model.name = "昆凌"
    print(beauutful_model.name)
    beauutful_model.six = "女"
    print("性别是", beauutful_model.six)
    beauutful_model.height = 170
    print("这位模特的海拔是", beauutful_model.height)
    beauutful_model.swim()


原创文章 96 获赞 330 访问量 2万+

猜你喜欢

转载自blog.csdn.net/hanhanwanghaha/article/details/106035958
今日推荐