Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Polymorphism/Python/polymorphism-1.py
Normal file
14
Task/Polymorphism/Python/polymorphism-1.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Point(object):
|
||||
def __init__(self, x=0.0, y=0.0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
def __repr__(self):
|
||||
return '<Point 0x%x x: %f y: %f>' % (id(self), self.x, self.y)
|
||||
|
||||
class Circle(object):
|
||||
def __init__(self, center=None, radius=1.0):
|
||||
self.center = center or Point()
|
||||
self.radius = radius
|
||||
def __repr__(self):
|
||||
return '<Circle 0x%x x: %f y: %f radius: %f>' % (
|
||||
id(self), self.center.x, self.center.y, self.radius)
|
||||
14
Task/Polymorphism/Python/polymorphism-2.py
Normal file
14
Task/Polymorphism/Python/polymorphism-2.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Point(object):
|
||||
def __init__(self, x=0.0, y=0.0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
def __repr__(self):
|
||||
return '<Point 0x%x x: %f y: %f>' % (id(self), self.x, self.y)
|
||||
|
||||
class Circle(Point):
|
||||
def __init__(self, x=0.0, y=0.0, radius=1.0):
|
||||
Point.__init__(self, x, y)
|
||||
self.radius = radius
|
||||
def __repr__(self):
|
||||
return '<Circle 0x%x x: %f y: %f radius: %f>' % (
|
||||
id(self), self.x, self.y, self.radius)
|
||||
24
Task/Polymorphism/Python/polymorphism-3.py
Normal file
24
Task/Polymorphism/Python/polymorphism-3.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
>>> from collections import namedtuple
|
||||
>>> class Point(namedtuple('Point', 'x y')):
|
||||
def __new__( _cls, x=0, y=0 ):
|
||||
return super().__new__(_cls, x, y)
|
||||
|
||||
|
||||
>>> class Circle(namedtuple('Circle', 'x y r')):
|
||||
def __new__( _cls, x=0, y=0, r=0 ):
|
||||
return super().__new__(_cls, x, y, r)
|
||||
|
||||
|
||||
>>> Point(), Point(x=1), Point(y=2), Point(3, 4)
|
||||
(Point(x=0, y=0), Point(x=1, y=0), Point(x=0, y=2), Point(x=3, y=4))
|
||||
>>> Circle(), Circle(r=2), Circle(1, 2, 3)
|
||||
(Circle(x=0, y=0, r=0), Circle(x=0, y=0, r=2), Circle(x=1, y=2, r=3))
|
||||
>>> p = Point(1.25, 3.87)
|
||||
>>> p
|
||||
Point(x=1.25, y=3.87)
|
||||
>>> p.x = 10.81
|
||||
Traceback (most recent call last):
|
||||
File "<pyshell#27>", line 1, in <module>
|
||||
p.x = 10.81
|
||||
AttributeError: can't set attribute
|
||||
>>>
|
||||
7
Task/Polymorphism/Python/polymorphism-4.py
Normal file
7
Task/Polymorphism/Python/polymorphism-4.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
>>> Point = namedtuple('Point', 'x y')
|
||||
>>> Circle = namedtuple('Circle', 'x y r')
|
||||
>>> Point(3, 4)
|
||||
Point(x=3, y=4)
|
||||
>>> Circle(x=1, y=2, r=3)
|
||||
Circle(x=1, y=2, r=3)
|
||||
>>>
|
||||
Loading…
Add table
Add a link
Reference in a new issue