Classes
Seymore4Head
Seymore4Head at Hotmail.invalid
Fri Oct 31 19:32:13 EDT 2014
On Fri, 31 Oct 2014 19:22:13 -0400, Seymore4Head
<Seymore4Head at Hotmail.invalid> wrote:
>On Fri, 31 Oct 2014 18:57:31 -0400, Dennis Lee Bieber
><wlfraed at ix.netcom.com> wrote:
>
>>On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
>><Seymore4Head at Hotmail.invalid> declaimed the following:
>>
>>>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
>>><rgaddi at technologyhighland.invalid> wrote:
>>>
>>>
>>>>Define a Square class, subclassed from Rectangle. Use getters/setters
>>>>to enforce that the length and width must be equal. Confirm that
>>>>length and width remain locked, and that perimeter() and area() work
>>>>correctly.
>>>
>>>class Rectangle:
>>> def __init__(self,length,width):
>>> self.length=length
>>> self.width=width
>>> def area(self):
>>> return self.length*self.width
>>> def perimeter(self):
>>> return 2*self.length+2*self.width
>>>class Square(Rectangle):
>>> def set_side (self):
>>> if self.length!=self.width:
>>>
>> Where's the rest of that -- not to mention you called it "set_side" but
>>never pass a side into it for use.
>>
>>-=-=-=-=-=-=-=-
>>
>>class Rectangle(object):
>> def __init__(self, length, width=None):
>> self.length = length
>> if width is None:
>> self.width = length
>> else:
>> self.width = width
>> def area(self):
>> return self.length * self.width
>> def perimeter(self):
>> return 2 * (self.length + self.width)
>>
>Thanks for posting that. I had given up on trying it. I follow the
>changes you made up this point. I will have to think some more to get
>the rest of this.
>The way you provided a catch for not having a width, I don't
>understand the purpose of a Square subclass. Couldn't side just be
>length?
>BTW I am willing to forget any mention of getter/setter. We can just
>pretend that never happened.
>
I took your instruction and change it to this:
That answers my own question.
Thanks again
class Rectangle(object):
def __init__(self, length, width=None):
self.length = length
if width is None:
self.width = length
else:
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
class Square(Rectangle):
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)
a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Square(4)
print (c.area())
print (c.perimeter())
More information about the Python-list
mailing list