Python ?class合集

16.class it up
inside the Triangleclass:
1.create a variable named number_of_sidesand set it equal to 3.
2.create a method named return Trueif the sum of self.angle1,self.angle2,and self.angle3is equal 180,and False otherwise.
Hint:
the check_anglesmethod should look something like this:
def check_angles(self):
if self.angle1 +self.angle2 +self.angle3 == 180:
return True
else:
return False

17.instantiate an object
1.create a variable named my_triangleand set it equal to a new instance of your Triangle class.Pass it three angles that sum to 180(eg.90,30,60)
2.print out my_triangle.number_of_sides
3.print out my_triangle.check_angles()

Hint:
remember,we can instantiate an object like so:
instance = class(args)
whereargs are the arguments init__() takes,not including self

18.inheritance
1.create a class named Equilateral that inherits from Triangle
2.inside Equilateral,create a member variable named angle and set it equal to 60
3.create an init() function with only the parameter self ,and self .angle1,self.angle2,and self.angle3 equal to self.angle(since an equilateral triangle's angles will always be 60)

class Triangle(object):
def init(self,angle1,angle2,angle3):
self.angle1 =angle1
self.angle2 = angle2
self.angle3 = angle3
number_of_sides = 3
def check_angles(self):
if self.angle1 + self.angle2 + self.angle3 == 180:
return True
else:
return False
my_triangle = Triangle(90,60,30)
print my_triangle.number_of_sides
print my_triangle.check_angles()
class Equilateral(Triangle):
angle = 60
def init(self):
self.angle1 =self.angle
self.angle2 = self.angle
self.angle3 = self.angle

5.instantiating your first object
class Square(object):
def init__(self):
self.sides = 4
my_shape = Square()
print my_share.sides
1.first,we create a class named Square with an attribute sides.
2.outside the class definition,we create a new instance of square named my_shape and access that attribute using my_shape.sides.

8.a methodical approach
your method should look something like this:
def description(self):

前面有篇文章有詳細完整細節

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • #1996 AHSME ##1996 AHSME Problems/Problem 1 The addition ...
    abigtreenj閱讀 1,440評論 0 0
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,868評論 0 23
  • 對英語恨之入骨的痛,大概從第一次重視英語之后就埋下了痛根。 從小學六年級畢業后的補習,一直補習補到了高中畢業,每一...
    禾必閱讀 191評論 2 1
  • 文 | 周大漁 前兩天,表弟給我打電話,說他準備要來上海找工作,之前的工作已經辭了。待會兒就買來上海的火車票。讓我...
    周大漁可持續發展閱讀 328評論 0 0
  • $函數 綁定事件 click方法顯示隱藏——show、hide方法 hover方法解決函數中的this問題call...
    大捕獵店閱讀 325評論 0 0