class Student:
def __init__(self, name, age, sc_list):
self.name = name
self.age = age
self.sc_list = sc_list
def get_name(self):
return self.name
def get_age(self):
return self.age
def get_course(self):
return max(self.sc_list)
zm = Student('zhangming', 20, [69, 88, 100])
print(zm.get_name(), zm.get_age(), zm.get_course())
import math
class Circle:
def __init__(self, center, radius, color):
self.center = center
self.radius = radius
self.color = color
def compute_perimeter(self):
return round(2 * math.pi * self.radius, 2)
def compute_area(self):
return round(math.pi * self.radius**2, 2)
circle = Circle((1, -1), 2, "blue")
print("圆的属性有:圆心位置为{},半径为{},颜色为{}".format(circle.center, circle.radius, circle.color))
print("此圆的周长为{},面积{}".format(circle.compute_perimeter(), circle.compute_area()))
class Student:
def __init__(self, name, age, sex, sc_list):
self.name = name
self.age = age
self.sex = sex
self.sc_list = sc_list
def compute_total(self):
total = 0
for i in range(len(self.sc_list)):
total += self.sc_list[i]
return total
def compute_average(self):
return round(self.compute_total()/len(self.sc_list), 2)
s1 = Student('lisi', 20, 'male', [77, 88, 99])
print("姓名:{},年龄:{},性别:{},成绩:英语{} 数学{} 语文{}"
.format(s1.name, s1.age, s1.sex, s1.sc_list[0], s1.sc_list[1], s1.sc_list[2]))
print("该学生的总分为{},平均分为{}".format(s1.compute_total(), s1.compute_average()))
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def personinfo(self):
print("姓名:{},年龄:{},性别:{}".format(self.name, self.age, self.sex))
class Student(Person):
def __init__(self, name, age, sex, Group, college):
super().__init__(name, age, sex)
self.Group = Group
self.college = college
def personinfo(self):
print("姓名:{} 年龄:{} 性别:{} 班级:{} 学院:{}".format(self.name, self.age, self.sex, self.Group, self.college))
s1 = Person('liuer', 20, 'male')
s1.personinfo()
s2 = Student('liuer', 20, 'male', '21-1', 'software college')
s2.personinfo()
class Vehicle:
def __init__(self, speed, size):
self.speed = speed
self.size = size
def printinfo(self):
print("此交通工具的速度为{}km/h,体积为{}m^3".format(self.speed, self.size))
def setSpeed(self, sp):
self.speed = sp
print("Vehicle speed {}km/h".format(self.speed))
def move(self):
print("Igniting...")
self.setSpeed(10)
print("Set vehicle speed 10. Move forward.")
def stop(self):
self.setSpeed(0)
print("Vehicle stop!")
def speedUp(self, num):
if self.speed + num >= 120:
self.setSpeed(120)
print("Speed reaches upper limit!")
else:
self.setSpeed(self.speed+num)
def speedDown(self, num):
if self.speed - num < 0:
self.stop()
else:
self.setSpeed(self.speed-num)
car = Vehicle(0, 8)
car.printinfo()
car.move()
car.setSpeed(50)
car.speedUp(20)
car.speedUp(60)
car.speedDown(20)
car.stop()
本周题型针对的是类的使用,对于python的面向对象编程我还是有较大的不足。我目前只能运用最基本的构造和继承类的方法,而对于更深层次——灵活使用类方法和静态方法、析构函数等,还是有所欠缺,一般在解题的时候也都不会往那些方面去想。