简体   繁体   中英

How to modify the attribute of super class using subclass in python?

I have a class Rectangle

class Rectangle:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        
    def set_width(self, width):
        self.width = width
        
    def set_height(self, height):
        self.height = height
        
    def get_area(self):
        return self.width * self.height
    
    def get_perimeter(self):
        return self.width*2 + self.height*2
    
    def get_diagonal(self):
        return (self.width ** 2 + self.height ** 2) ** .5
    
    def get_picture(self):
        picture = str()
        if self.width >= 50 | self.height >= 50:
            return "Too big for picture."
        
        for height in range(self.height):
            picture += "*"*self.width+"\n"
            
        return picture
    
    def __repr__(self):
        return f"Rectangle(width={self.width}, height={self.height})"

And a subclass of it Square

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)
        self.length = length
    
    def set_side(self, length):
        super().__init__(length, length)
        self.length = length
    
    def set_width(self, length):
        super().__init__(length, length)
        self.length = length        
        
    def set_height(self, length):
        super().__init__(length, length)
        self.length = length
        
    def __repr__(self):
        return f"Square(side={self.length})"

Here I called the super(). init () every time I want to change values.

What I want is instead of defining set_height and set_width explicitly in Square class too, is there a way I can set both height and width to length by calling set side and the str repr should also show the new set side? Also, I feel like there is a better way to do this then what I did.

    class Rectangle:
        def __init__(self, width, height):
            self.width = width
            self.height = height
            
        def set_width(self, width):
            self.width = width
            
        def set_height(self, height):
            self.height = height
            
        def get_area(self):
            return self.width * self.height
        
        def get_perimeter(self):
            return self.width*2 + self.height*2
        
        def get_diagonal(self):
            return (self.width ** 2 + self.height ** 2) ** .5
        
        def get_picture(self):
            picture = str()
            if self.width >= 50 | self.height >= 50:
                return "Too big for picture."
            
            for height in range(self.height):
                picture += "*"*self.width+"\n"
                
            return picture
        
        def __repr__(self):
            return f"Rectangle(width={self.width}, height={self.height})"
    
    
    
    class Square(Rectangle):
        def __init__(self, length):
            super().__init__(length, length)
            self.length = length
        
        def set_side(self, length):
            super().__init__(length, length)
            self.length = length
        
        def set_width(self, length):
            # super().__init__(length, length)
            # self.length = length     
            self.set_side(length)   
            
        def set_height(self, length):
            # super().__init__(length, length)
            # self.length = length
            self.set_side(length)   
    
            
        def __repr__(self):
            return f"Square(side={self.length})"
    
    
    
    # r = Rectangle(20, 20)
    s = Square(20)
    
    # print(r)
    print(s)
    s.set_width(60)
    print(s)

So, it's funny I'm working on the same project with freecodecamp at this moment. However, I have went a different route than this. You don't have to add all of those methods into the subclass like that. You can define the side in the Square class. I'll share what I have done so far. Now this is nowhere near completed yet. I like getting the functionality down then simplifying and making it cleaner. I'm working on the get_amount_inside() method, and I'm searching for how to pull an attribute from a subclass to use in the superclass.

class Rectangle:

    def __init__(self, width=None, height=None):
        self.height = height
        self.width = width
        self.area = None
        self.perimeter = None
        self.diagonal = None
        self.side = None

    def set_attributes(self, new_width=None, new_height=None):
        self.side = new_width or new_height
        self.width = new_width
        self.height = new_height
        return self.width, self.height

     def set_width(self, new_width):
        self.width = new_width
        return self.width

    def set_height(self, new_height):
        self.height = new_height
        return self.height

    def get_area(self):
        if self.side is not None:
            self.area = (self.side ** 2)
            return self.area
        else:
            self.area = (self.width * self.height)
            return self.area

    def get_perimeter(self):
        if self.side is not None:
            self.perimeter = (self.side * 4)
            self.perimeter = ((2 * self.height) + (2 * self.width))
            return self.perimeter
        else:
            self.perimeter = ((2 * self.height) + (2 * self.width))
            return self.perimeter

    def get_diagonal(self):
        if self.side is not None:
            self.diagonal = ((self.side ** 2) * 2) ** 0.5
            return self.diagonal
        else:
            self.diagonal = (self.height ** 2 + self.width ** 2)**0.5
            return self.diagonal

    def __repr__(self):
        self.get_area()
        self.get_perimeter()
        self.get_diagonal()
        rectangle = ["Rectangle" + "(width=" + str(self.width), ", height=" + str(self.height) + ")"]
        return "".join(rectangle)


class Square(Rectangle):

    def __init__(self, side):
        self.side = side

    def set_side(self, new_side):
        self.side = new_side
        return self.side

    def __repr__(self):
        square = ["Square" + "(side=" + str(self.side) + ")"]
        return "".join(square)

The questions I'm having for this project is completely different than yours. I completely forgot about the f"" formatting method, so I am happy I clicked on this link. -ZichKoding is my name on Repl.it

this is a similar example, you can solve problem

class Rectangle:

def init (self,length,width): self.length = length self.width = width

class Square(Rectangle):

def init (self, length, width): super(). init (length, width)

def area(self): return self.length*self.width

class Cube(Rectangle):

def init (self,length, width,height): super(). init (length, width)
self.height = height

def volume(self): return self.length self.width self.height

square = Square(3,3) cube = Cube(3,3,3)

print(square.area()) print(cube.volume())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM