Commit bbc8b132 by 大雄

课堂内容

parents
class Person: #这里Person后面要有冒号
name = "女娲"
age = 10000
__school = "class private school"
def __init__(self, name, age, home): #只能写一个构造函数
self.name = name
self.age = age
self.__home = home
print("有参数的构造函数运行")
#实例方法
def my_print(self):
print("my print run")
#私有方法
#只能允许这个类本身进行访问,连子类也不可以访问
def __creat_person(self): #后面要有(self):
print("私有方法——女娲创造了一个人")
#保护方法
#保护类型权限允许类本身和子类进行访问
def _my_protect_print(self):
print("ssss")
@classmethod #classmethod的位置有讲究,放到p = Person()后面就不对
def my_classmethod(cls, p):
print(cls.name)
print(p.name)
print("classmethod——类方法运行")
# p = Person("张三", "20", "家") #这里在Person后面要有括号()
# p = Person() #当有多个构造函数的时候,无参数的构造函数不起作用
"""
函数:在模块中声明的一般叫做函数
方法:在类中声明的叫做方法
"""
p = Person("伏羲", "15000", "家")
p.my_classmethod(p)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment