PYTHON的类和对象

# 类和对象
'''
创建一个类
class 类名:
    类里面的内容
'''
class cl1():
    pass
'''
实例化一个类
a = cl1()
'''

# 构造函数
# self:在类中的方法必须加上self
# __init__(self,参数)
# 构造函数实际意义:初始化
class cl2:
    def __init__(self):
        print("111")
b = cl2()


# 给类加上参数:给构造方法加上参数
class cl3:
    def __init__(self,name,job):
        print("My name is "+name+"   My job is "+job)

c = cl3("aa","teacher") 


# 属性:类里面的变量
class cl4():
    def __init__(self,name,job):
        self.myname = name
        self.myjob = job

d = cl4("aa","teacher")
print(d.myjob)

# 方法:类里面的函数:def 方法名(self , 参数)
class cl5():
    def myfunc1(self,name):
        print("hello "+name)
f = cl5()
f.myfunc1("aa")

class cl6():
    def __init__(self,name):
        self.myname = name
    def myfunc1(self):
        print("hello "+self.myname)
h = cl6("aa")
h.myfunc1()

 

posted @ 2019-07-24 14:33  Peach~  阅读(183)  评论(0编辑  收藏  举报