• [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)
    """
  • 相关阅读:
    学习进度表 06
    课堂练习第七周
    学习进度表 05
    学习进度表 04
    分组情况
    求子数组最大值
    codeforce 8A-8C
    nginx 设置服务,开机启动
    转 ubuntu 安装php
    Nginx小记
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8033559.html
Copyright © 2020-2023  润新知