关于人工智能:Python-Use-Python-Classes

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.,

 

#ball.py

class Ball:
    def __init__(self, radius, color, weight):
        self.radius = radius
        self.color = color
        self.weight = weight

"""
from ball import Ball
b = Ball(22, 'red', 10)
"""

class Football:
    """A standard, regulation NFL ball"""
    def __init__(self, diameter, color, pressure):
        self.diameter = diameter
        self.color = color
        self.pressure = pressure

    def inflate(self, psi):
        self.pressure = self.pressure + psi

    def deflate(self, psi):
        self.pressure = self.pressure - psi

# inherit Football
class PatriotsBall(Football):
    def inflate(self, psi):
        """ overwrite default method """
        self.pressure = self.pressure - psi
    
"""
from ball import PatriotsBall
pb = PatriotsBall(22, 'blue', 10)
"""

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理