读书会 《Python3 面向对象编程》读书勘误

膨化先生 · 2018年03月03日 · 887 次阅读

《Python3 面向对象编程》读书勘误

第 2 章 Python 对象

p24:

class MyFirstClass:
    pass

p31:

class Point:
    def __init__(self,x,y):
        self.move(x,y)
    def move(self,x,y):
        self.x=x
        self.y=y
    def reset(self):
        self.move(0,0)
point=Point(3,5)
print(point.x,point.y)

p32:

import math

class Point:
    'Represents a point in two-dimensional geometric coordinates.'
    def __init__(self,x=0,y=0):
        '''Initialize the poisition of a new point.The x and y coordinates
        can be specified.If they are not,the point defaults to the origin.'''
        self.move(x,y)
    def move(self,x,y):
        'Move the point to a new location in two-dimensional space.'
        self.x=x
        self.y=y
    def reset(self):
        'Reset the point back to the geometric origin:0,0'
        self.move(0,0)
    def calculate_distance(self,other_point):
        '''Calculate the distance from  this point to a second point passed as
        a parameter.
        This function uses the Pythagorean Theprem to calculate the distance
        between the two point.The distance is returned as a float.'''
        return math.sqrt(
            (self.x-other_point.x)**2+
            (self.y-other_point.y)**2
        )
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册