简体   繁体   English

如何使用python中的子类修改super class的属性?

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

I have a class Rectangle我有一个 class 矩形

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以及它的一个子类 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().这里我调用了 super()。 init () every time I want to change values. init () 每次我都想改变值。

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?我想要的不是在 Square class 中显式定义 set_height 和 set_width,有没有一种方法可以通过调用 set side 将高度和宽度设置为长度,str repr 也应该显示新的 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.所以,有趣的是,我现在正在和 freecodecamp 做同一个项目。 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.您可以在 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.我正在研究 get_amount_inside() 方法,并且正在寻找如何从子类中提取属性以在超类中使用。

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.我完全忘记了 f"" 格式化方法,所以我很高兴我点击了这个链接。 -ZichKoding is my name on Repl.it -ZichKoding 是我在 Repl.it 上的名字

this is a similar example, you can solve problem这是一个类似的例子,你可以解决问题

class Rectangle: class 矩形:

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

class Square(Rectangle): class 方形(矩形):

def init (self, length, width): super(). def init (self, length, width): super(). init (length, width)初始化(长度,宽度)

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

class Cube(Rectangle): class 立方体(矩形):

def init (self,length, width,height): super(). def init (self,length,width,height): super(). init (length, width)初始化(长度,宽度)
self.height = height self.height = 身高

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

square = Square(3,3) cube = Cube(3,3,3)正方形 = 正方形 (3,3) 立方体 = 立方体 (3,3,3)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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