Python类与对象----动态添加类对象属性和类方法

#定义一个学生类
class Student:
    '''
    学生类,计算学生的分数情况
    '''
    #定义类属性
    name="Li Min";
    age=23;
    gener='男';
    yuwen=90;
    shuxue=100;
    yingyu=98;
    sum=0;
    avg=0;

    #定义类方法
    def __init__(self):
        print("hello,调用构造函数");
    def __init__(self,name,age,gener,yuwen,shuxue,yingyu):
        self.name=name;
        self.age=age;
        self.gener=gener;
        self.yuwen=yuwen;
        self.shuxue=shuxue;
        self.yingyu=yingyu;
    def print_info(self):
        print("name",self.name,"age",self.age,"gener",self.gener);
        print(self.yuwen,self.shuxue,self.yingyu);
    def cal_sum(self):
        self.sum=self.yuwen+self.shuxue+self.yingyu;
        print("总分为:",self.sum);
    def cal_avg(self):
        self.avg=(self.yuwen+self.shuxue+self.yingyu)/3;
        print("Avg=",self.avg)
    def judge(self):
        if self.avg<60:
            print("成绩不合格")
        elif 60

运行结果如下:

name Li Bailing age 25 gener 女
89 90 96
总分为: 275
Avg= 91.66666666666667
成绩优秀
18900
---print_content---- hello world, you need study Python!
---print_content---- hello,world,Learning Python is very siginificant for us!
---print_content---- hello world!,nihaoma?

Process finished with exit code 0

 

你可能感兴趣的:(Python)