在Person类的基础上实现派生类Student与向量加减乘除三个维度的实现。

请在Person类的基础上实现派生类Student,在学生类中增加新的SetInfo和ShowInfo方法,实现学生信息的输入和显示。
向量加法(add)、减法(sub)、乘法(mul,multiply n)、除法(div,divide by n)三个维度的实现。(类实现)
运算结果如图

img

img


class Person(object):
    def __init__(self):
        self.__name = '1'     # 姓名
        self.__age = '18'      # 年龄
        self.__sex = '男'
        self.__major='计算机'
class Student( Person):

    def SetInfo(self, name, age, sex,major):
        self.__name = name     # 姓名
        self.__age = age       # 年龄
        self.__sex = sex
        self.__major=major
    def ShowInfo(self):
        print(self.__name+" "+self.__age+" "+self.__sex+" "+self.__major)

stu = Student()
n=input("please input the name of student:")
a=input("please input the age of student:")
s=input("please input the sex of student:")
m=input("please input the major of student:")
stu.SetInfo(n,a,s,m)
stu.ShowInfo()

第二个

l1=[]
l2=[]
l1=input('the first vect:').split(',')
l2=input('the second vect:').split(',')
n=input('the n:')
print('the result is:',end="")
for i in range(len(l1)):
    print(str(int(l1[i])+int(l2[i])),end=" ")
print()
print('the result is:',end="")
for i in range(len(l1)):
    print(str(int(l1[i])-int(l2[i])),end=" ")
print()
print('the result is:',end="")
for i in range(len(l1)):
    print(str(int(l1[i])*int(n)),end=" ")
print()
print('the result is:',end="")
for i in range(len(l1)):
    print(str(int(l1[i])/int(n)),end=" ")

class Person():
    def __init__(self, name, sex = '男'):
        self.name = name
        self.sex = sex
        
class Student(Person):
    def __init__(self, name, sex = '男'):
        super(Student, self).__init__(name, sex)
    
    def SetInfo(self, age):
        self.age = age
    
    def ShowInfo(self):
        print(self.name, self.sex, self.age)

class vect:
    def __init__(self, *v):
        self.v = v
        
    def __add__(self, vt):
        if len(self.v) != len(vt.v):
            return "error"
        else:
            return vect(*[vt.v[i]+value for i,value in enumerate(self.v)])
    def __str__(self):
        return ','.join(map(str, self.v))
    def __repr__(self):
        return ','.join(map(str, self.v))


s1 = Student('小明', '女')
s1.SetInfo(34)
s1.ShowInfo()

v1 = vect(1, 2, 3)
print('the first vect:', v1)
v2 = vect(2, 3, 4)
print('the second vect:', v2)
v3 = vect(4, 5, 6)
print('the third vect:', v3)

print('the sum is:', v1 + v2 + v3)
其它的差,积,商仿照即可
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632