程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The reference of Python classes and objects

編輯:Python

Questions in notes

import mathclass Point: def __init__(self, x=0.0, y=0.0): self.__x = x self.__y = y def getx(self): return self.__x def gety(self): return self.__y def distance_from_xy(self, x, y): return math.hypot(abs(self.__x - x), abs(self.__y - y))# In general, in brackets x,y Not all of them need to be defined first, for example self.__x and self.__y? Why? there self.__x Continue to use __init__ Inside  def distance_from_point(self, point): return self.distance_from_xy(point.getx(), point.gety())""" I'm a little confused here , Do you want to write an expression first self.__point=point stay return Before ? And a little bit more , May I use self.getx() and self.gety() Instead of point.getx() and point.gety()? """ point1 = Point(0, 0)point2 = Point(1, 1)print(point1.distance_from_point(point2))print(point2.distance_from_xy(2, 0))

Finally, all double underlined variables __, I know it is private It means , If you don't use it, you can wow


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved