-
Create a car class Auto, including the number of tires, car color, body weight, speed and other attributes, and create instances through different construction methods. At least the car must be able to accelerate, decelerate and stop. Then define a car class CarAuto inherit Auto and add air conditioning, CD attributes, and re-implement methods to cover acceleration and deceleration methods
class Auto: def __init__(self, tyre, color, weight, speed): self.tyre = tyre self.color = color self.weight = weight self.speed = speed def __repr__(self): return f'{str(self.__dict__)[1:-1]}' def speed_up(self, up): self.speed += up return self.speed def speed_down(self, down): self.speed -= down return self.speed def stop(self): self.speed = 0 return self.speed class CarAuto(Auto): def __init__(self, tyre, color, weight, speed, air_con, cd): super().__init__(tyre, color, weight, speed) self.air_con = air_con self.cd = cd def speed_up(self): self.speed += 10 return self.speed def speed_down(self): self.speed -= 10 return self.speed auto1 = Auto(4, 'white', 1500, 80) auto2 = Auto(8, 'black', 2500, 120) car1 = CarAuto(4, 'red', 1400, 60, 'off', '我们是共产主义接班人') car1.stop() print(car1)
-
Create a Person class and add a class field to count the number of objects of the Person class
class Person: count = 0 def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex Person.count += 1 def __repr__(self): return f'{str(self.__dict__)[1:-1]}' print(Person.count) p1 = Person('李', '18', 'man') p2 = Person('张', '18', 'woman') print(Person.count)
-
Create an animal class with attributes: gender, age, color, type,
When requesting to print objects of this class, print them in the form of'/XXX objects: gender-? age-? color-? type-?/'
class Animal: def __init__(self): self.gender = 'male' self.age = 1 self.color = 'yellow' self.breed = 'cat' def __repr__(self): return f'/{Animal.__name__}的对象: 性别-{self.gender} 年龄-{self.age} 颜色-{self.color} 类型-{self.breed}/' cat1 = Animal() cat1.age = 3 print(cat1)
-
Write a circle class with attributes radius, area and perimeter; when requesting area and perimeter, you can get the corresponding value according to the value of radius. But when assigning values to area and perimeter, the program crashes directly and prompts that the attribute cannot be assigned
from math import pi class AssignError(Exception): def __str__(self): return '该属性不能赋值' class Circle: def __init__(self, radius): self.radius = radius @property def perimeter(self): return 2*pi*self.radius @perimeter.setter def perimeter(self, value): raise AssignError @property def area(self): return pi*self.radius**2 @area.setter def area(self, value): raise AssignError c1 = Circle(10) print(c1.area, c1.perimeter) c1.radius = 100 print(c1.area, c1.perimeter) c1.area = 1000
-
Write a poker class, which requires the function of dealing cards and shuffling cards (specific attributes and other functions are played according to the actual situation)
-
(Try) Write a class whose functions are: 1. Parse the content of the specified lyric file 2. Display the lyric prompt by time: The content of the lyric file is generally stored in the following format. The front of the lyrics corresponds to the time, and the corresponding lyrics can be displayed at the corresponding time point
[00:00.20]蓝莲花 [00:00.80]没有什么能够阻挡 [00:06.53]你对自由地向往 [00:11.59]天马行空的生涯 [00:16.53]你的心了无牵挂 [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月 [02:16.51][01:55.46][00:26.83]也曾感到彷徨 [02:21.81][02:00.60][00:32.30]当你低头地瞬间 [02:26.79][02:05.72][00:37.16]才发觉脚下的路 [02:32.17][00:42.69]心中那自由地世界 [02:37.20][00:47.58]如此的清澈高远 [02:42.32][00:52.72]盛开着永不凋零 [02:47.83][00:57.47]蓝莲花
day19- inherit memory management and copy operations
Guess you like
Origin blog.csdn.net/xdhmanan/article/details/109283021
Recommended
Ranking