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):

前面有篇文章有详细完整细节

你可能感兴趣的:(Python class合集)