Object oriented classes work much like classes in other languages. Learn how to create them and use them, learn the difference between class variables and instance variables, creating class methods, and learn how to create classes that inherit from other classes.,
代码语言:javascript复制#ball.py
class Ball:
代码语言:txt复制def __init__(self, radius, color, weight):
代码语言:txt复制 self.radius = radius
代码语言:txt复制 self.color = color
代码语言:txt复制 self.weight = weight
代码语言:javascript复制"""
from ball import Ball
b = Ball(22, 'red', 10)
"""
class Football:
代码语言:txt复制"""A standard, regulation NFL ball"""
代码语言:txt复制def __init__(self, diameter, color, pressure):
代码语言:txt复制 self.diameter = diameter
代码语言:txt复制 self.color = color
代码语言:txt复制 self.pressure = pressure
代码语言:txt复制def inflate(self, psi):
代码语言:txt复制 self.pressure = self.pressure psi
代码语言:txt复制def deflate(self, psi):
代码语言:txt复制 self.pressure = self.pressure - psi
inherit Football
代码语言:javascript复制class PatriotsBall(Football):
代码语言:txt复制def inflate(self, psi):
代码语言:txt复制 """ overwrite default method """
代码语言:txt复制 self.pressure = self.pressure - psi
代码语言:javascript复制"""
from ball import PatriotsBall
pb = PatriotsBall(22, 'blue', 10)
"""