1,类的定义
class People():
pass
people=People()
print(type(people))
2.实例方法
class People():
def sayHello(self): #self是默认参数,一定要有
print(‘Hello!’)
people=People()
people.sayHello()
3,类方法
class People():
sum=0
def init(self,sex):
self.sex=sex
self.class.sum+=1
print(‘现在有%d个人.’%self.class.sum)
def sayHello(self):
print(‘Hello!’)
@classmethod
def plus(cls):
cls.sum+=1
print(‘现在有%d个人.’%cls.sum)
people1=People('man')
People.plus()
people2=People('woman')
People.plus()
4,静态 方法,
class People():
sum=0
def init(self,sex):
self.sex=sex
self.class.sum+=1
@staticmethod
def sta():
People.sum+=1
print(‘现在有%d个人.’%People.sum)
people1=People('man')
People.sta()
5,
父类
class Father(object):
def init(self):
self.num = 0
def jia(self):
self.num += 1
print(self.num)
子类
class So(Father):
# 子类重写父类
def jia(self):
self.num += 20
print(self.num)
print('调用父类')
def supermethod(self):
print('我是super方法')
class Grandson(So):
def func(self):
super().supermethod()
print(‘我是 多继承’)
fa = Father()
s = So()
g = Grandson()
s.jia()
g.func()