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阅读 5,282评论 0 0
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,398评论 0 23
  • 对英语恨之入骨的痛,大概从第一次重视英语之后就埋下了痛根。 从小学六年级毕业后的补习,一直补习补到了高中毕业,每一...
    禾必阅读 1,261评论 2 1
  • 文 | 周大渔 前两天,表弟给我打电话,说他准备要来上海找工作,之前的工作已经辞了。待会儿就买来上海的火车票。让我...
    周大渔可持续发展阅读 2,532评论 0 0
  • $函数 绑定事件 click方法显示隐藏——show、hide方法 hover方法解决函数中的this问题call...
    大捕猎店阅读 2,490评论 0 0