Example of day23 03 combination

Keywords: Python Programming Attribute

Example of day23 03 combination

I. use the combination method to write a circle and calculate its perimeter and area

 1 from math import pi  # Import from built-in functions pi
 2 # Define a circle class first
 3 class Circle:
 4     def __init__(self,r):  # Initialization and self The property of a circle is radius
 5         self.r = r  # assignment
 6 
 7     def area(self):  # The method related to circle, calculating the area of circle
 8         return self.r**2 * pi
 9     def perimeter(self):  # The method related to circle, calculating the area of circle
10         return 2*pi*self.r
11 
12 # Define another ring class
13 class Ring:
14     def __init__(self,outside_r,inside_r):  # Initialization and self The outer radius and inner radius of a torus
15         self.outside_c = Circle(outside_r)  # The outer radius of a ring is assigned by the radius of a circle class
16         self.inside_c = Circle(inside_r)  # The inner radius of a ring is also assigned by the radius of a circle
17     def area(self):
18         return self.outside_c.area() - self.inside_c.area()  # Directly referring to the calculated area in the circle class area Method, this involves the method of combination
19     def perimeter(self):
20         return self.outside_c.perimeter() + self.inside_c.perimeter()  # Directly referring to the calculation perimeter in the circle class perimeter Method, which involves the method of combination
21 ring = Ring(20,10)
22 print(ring.area())  # Calculate the area of the torus
23 print(ring.perimeter())  # Calculating the circumference of a torus

Operation result:

C:\Users\sku1-1\PycharmProjects\untitled\venv\Scripts\python.exe "C:/Users/sku1-1/PycharmProjects/untitled/day23  03 Combinatorial example.py"
942.4777960769379
188.49555921538757

Process finished with exit code 0

 

2. Create a teacher class. The teacher has a birthday, and the birthday is also a class, involving the combination method

class Teacher:
    def __init__(self,name,age,sex,birthday):
        self.name = name
        self.age = age
        self.sex = sex
        self.birthday = birthday

class Birthhday:
    def __init__(self,year,month,day):
        self.year = year
        self.month = month
        self.day = day

b = Birthhday(2000,5,1)  # instantiation
teacher = Teacher('Lily',18,'female',b)  # instantiation
print(teacher.name)
print(teacher.birthday.year)  # Here's the combination
print(teacher.birthday.day)  # Here's the combination

Operation result:

C:\Users\sku1-1\PycharmProjects\untitled\venv\Scripts\python.exe "C:/Users/sku1-1/PycharmProjects/untitled/day23  03 Combinatorial example.py"
//Lily
2000
1

Process finished with exit code 0

 

Three. Review

1. Object oriented programming

Idea: abstraction of roles, creation of classes, creation of roles (instantiation), manipulation of these instances

 

Keyword: class

Basic framework:

class name:
    Static property ='aaaa '
    def _init_(self):
        pass

2. Class name and object operation

Class name. Static attribute ---- stored in the class namespace

Object = class name () -- instantiation process: create a self object, execute initialization of the init method, and return the self object to the external

Object. Properties

Object. Method = = = class name. Method (object)

Objects can use static variables

Class cannot use the properties in an object

3. Combination: there are at least two classes, which are related to what they are (both of which refer to classes). View the properties of another class through one class

The object of one class is the property of another

Posted by bing_crosby on Fri, 08 Nov 2019 11:42:42 -0800