17 summary and operation (object-oriented continuation and json)

Posted by scristaldi on Tue, 08 Mar 2022 13:10:39 +0100

1, Summary

  1. attribute

    1. Class attribute (field): variables directly defined in a class are class attributes, which are defined by 'class.' Mode of use

    2. Object attribute: take 'self Attribute name = value 'is defined in__ init__ Method

    3. Default values for object properties:

    4. Self: point to whoever calls it, so you can directly use self as an object in the object

    5. Selection of method:
      1) Object method: if an object (or object attribute) is required to implement the function function, the object method is used
      2) Class method: when the object (or object attribute) is not required to realize the function
      3) Static methods: neither classes nor objects are required

  2. inherit

    1. Inheritance: let the subclass have the properties and methods of the parent class (subclass: inheritor; parent: inheritee, also known as superclass)

    2. Syntax: class name (parent class list):
      Class description document
      Content of class
      (if the parent class is not written when defining a class, the name class inherits py's base class object by default)

    3. Add content to subclass:
      1) Add class properties and methods: define new properties and methods directly in subclasses
      2) Add object properties:

    4. super(). Init (parent parameter): add super to the subclass to obtain the object attributes in the parent class

    5. Multiple inheritance can inherit all methods and class properties of all parent classes, and can only inherit the object properties of the first parent class

  3. json

    1. json: is a general data format, which is mainly used for effective data communication between different languages
    2. Requirement: a json has only one data; The only data must be of the type supported by json (number, string, Boolean, null, array, dictionary)
      (numbers: write directly and support scientific counting)
      String: only double quotation marks can be used, and escape characters are supported
      Boolean value: only true and false, both in lowercase
      Null value: null
      Array: a list equivalent to py, [element 1, element 2,...]
      Dictionary: the dictionary equivalent to py, but the key can only be a string)
    3. Conversion between py and json data: (after importing the module)
      1)json to python:
      json python
      Numeric int, float
      String str
      Boolean lowercase to uppercase
      Null value None
      Array list
      Dictionary dict
      2)python to json:
      Basically, on the contrary, pay attention to case and single and double quotation marks. One more tuple can also be converted into an array of json
    4. json module:
    5. json.loads(json format string) - convert the JSON data corresponding to the JSON format string into the corresponding python data. There is no JSON error in the brackets
    6. json.dumps(python data) - converts python data to JSON format strings

2, Homework

  1. Define a dog and a human:

    Dog ownership attributes: name, sex and breed ownership method: call

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

    class Dog:
        def __init__(self, name, sex, breed):
            self.name = name
            self.sex = sex
            self.breed = breed
        def call(self):
            print(f'{self.name}It's a{self.sex}of{self.breed}´╝îIt's calling')
    
    class Human:
        def __init__(self, name1, age, dog):
            self.name1 = name1
            self.age = age
            self.dog = dog
        def play(self):
            print(f'{self.name1}this year{self.age}Years old, he has one{self.dog},He is walking the dog')
            
    a = Dog('chinese rhubarb', 'male', 'Golden hair')
    a.call()
    b = Human('jack', 18, '')
    b.play()
    
  2. Define a rectangle class with attributes: length and width. Methods: calculate perimeter and area

    class Rectangle:
        def __init__(self, long, wide):
            self.long = long
            self.wide = wide
        def area(self):
            return self.long * self.wide
        def perimeter(self):
            return (self.long + self.wide) * 2
    
    result1 = Rectangle(5, 2)
    result2 = Rectangle(5, 4)
    print(result1.area())
    print(result2.perimeter())
    
  3. 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 Point:
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def distance(self, o):
            return ((self.x - o.x) ** 2 + (self.y - o.y) ** 2) ** 0.5
    
    p1 = Point(0, 9)   # x,y
    p2 = Point(0, 5)
    print(p1.distance(p2))
    
  4. Define a circle class with attributes: radius and center. Methods: find the circumference and area of the circle, and judge whether the current circle and another circle are circumscribed

    class Circle:
        Pi = 3.14
        def __init__(self, r, x, y):
            self.r = r
            self.x = x
            self.y = y
        def area(self):
            return Circle.Pi * self.r ** 2
        def perimeter(self):
            return Circle.Pi * self.r * 2
        def exterior_contact(self, b):
            if ((self.x - b.x) ** 2 + (self.y - b.y) ** 2) ** 0.5 == self.r + b.r:
                return "Circumscribe two circles"
            else:
                return 'Two circles are not circumscribed'
    
    yuan1 = Circle(7, 4, 7)
    yuan2 = Circle(5, 0, -5)
    
    print(yuan1.exterior_contact(yuan2))
    
    print(yuan1.area())
    print(yuan1.perimeter())
    print(yuan2.area())
    print(yuan2.perimeter())
    
  5. Define a segment class, with attributes: start point and end point, and method: obtain the length of the segment

    class Segment:
        def __init__(self, start, end):
            self.start = start
            self.end = end
        def length(self):
            return abs(self.end - self.start)
    
    a1 = Segment(-2, -9)
    print(a1.length())
    

Topics: Python