面向对象类的工作方式与其他语言中的类非常相似。了解如何创建和使用它们,了解类变量和实例变量之间的区别,创建类方法,并了解如何创建从其他类继承的类。#ball.pyclassBall:def__init__(self,radius,color,weight):self.radius=radiusself.color=colorself.weight=weight"""fromballimportBallb=Ball(22,'red',10)"""classFootball:"""Astandard,regulationNFL球"""def__init__(self,diameter,color,pressure):self.diameter=diameterself.color=colorself.pressure=pressuredefinflate(self,psi):self.pressure=self.pressure+psidefdeflate(self,psi):self.pressure=self.pressure-psi#inheritFootballclassPatriotsBall(Football):definflate(self,psi):"""覆盖默认方法"""self.pressure=self.pressure-psi““”从球进口PatriotsBallpb=PatriotsBall(22,'blue',10)”””
