Object-oriented super simple exercises

  1. Define a rectangle class with attributes: length and width. Methods: find perimeter and area

    class recs:
        def __init__(self,long,wide):
            self.longs=long
            self.wides=wide
    
        def peri(self):
            return (self.longs+self.wides)*2
    
        def areas(self):
            return self.longs*self.wides
    
    t1 = recs(10,20)
    print(t1.areas())
    print(t1.peri())
    
  2. Define a two-dimensional point class with attributes: x-coordinate and y-coordinate. Method: find the distance from the current point to another point

    class recs:
        def __init__(self,x=0,y=0):
            self.x=x
            self.y=y
    
        def peri(self,self1):
            return ((self.x-self1.x)**2+(self.y-self1.y)**2)**0.5
    
    
    t1 = recs(10,20)
    t2 = recs()
    print(t2.peri(t1))
    
  3. Define a circle class with attributes: radius and center. Methods: find the circumference and area of ​​the circle, determine whether the current circle is circumscribed to another circle

    class Circle:
        x = 3.14
        def __init__(self,r=0,cen=0):
            self.r = r
            self.c = cen
    
    
        def peri(self):
            return 2*self.x*self.r
    
    
        def areas(self):
            return  self.x*self.r**2
    
    
        def cutting(self,self1):
            return self.c+self1.c==self.r+self1.r
    
    
    r=Circle(5)
    c = Circle(7,12)
    print(r.peri())
    print(r.areas())
    print(r.cutting(c))
    
  4. Define a line segment class with attributes: start point and end point, and method: get the length of the line segment

    class Lines:
        def __init__(self,a,b):
            self.a=a
            self.b=b
    
        def lengths(self):
            return self.b-self.a
    
    
    f1=Lines(15,84)
    print(f1.lengths())
    
  5. Define a dog and a human:

    Dog has attributes: name, gender and breed. Method of possession: call

    Human possessing attributes: name, age, dog possessing method: walking the dog

Guess you like

Origin blog.csdn.net/SaharaLater/article/details/111936604