面向对象的演示

from tkinter.messagebox import *
class girl:
    def __init__(self,name,birthday,height,mobile,email,homeaddress):
        """
        构造方法,用来初始化这个对象的属性
        """
        self.name=name
        self.birthday=birthday
        self.height=height
        self.mobile=mobile
        self.email=email
        self.homeaddress=homeaddress
    def say_hello(self):
        showinfo(self.name+"向大家打招呼","大家好,我叫"+self.name+",我的生日是"+self.birthday+".希望大家支持我")
    def eat(self):
        showinfo("正在吃东西","真好吃")
    def dance(self):
        showinfo("跳舞,"+self.name+"跳完了","希望大家喜欢")
#上面的girl是一个类,记录了一些属性(静态特征)和方法(动态特征)

if __name__=="__main__":
    alice=girl("Alice","1994-10-10",168,"12345567","[email protected]","上海市淘宝里226号")
    candy=girl("Candy","1998-8-10",188,"12045567","[email protected]","上海市闵行区226号")

print(alice)  #<__main__.girl object at 0x102782358>
print(candy)  #<__main__.girl object at 0x102782390>

#属性的访问:girl是一个类,alice,candy就是两个由girl这个类生成的(实例化的两个对象)
#对象的属性(静态特征的访问):对象的名称.属性的名称
print("姓名:"+alice.name,"出生日期:"+alice.birthday,"身高:"+str(alice.height))
print("姓名:"+candy.name,"出生日期:"+candy.birthday,"身高:"+str(candy.height))

#对象方法(动态特征)调用:对象名称.函数名称()
print(alice.say_hello())#Alice 在打招呼
print(candy.say_hello())
print(alice.dance())  #alice在跳舞

#[1]类是一种复杂的数据类型
#[2]类就是个容器,可以容纳基本的数据类型,基本的属性,容纳一些函数

猜你喜欢

转载自blog.csdn.net/weixin_40446764/article/details/80720000