赞
踩
目录
4、创建一个学生类,有属性 id,name,age,sex,私有属性addr,方法get_addr(),set_addr()返回以及设置学生的属性addr,方法msg()显示学生的所有信息。
- class Person:
- __slots__ = ('name', '__weight', '__height', '__sex')
-
- def __init__(self, name, weight, height, sex):
- self.name = name
- self.__weight = weight
- self.__height = height
- self.__sex = sex
-
- def getName(self):
- return self.name
-
- def setName(self, name):
- self.name = name
-
- def bmi(self):
- bim = int(self.__weight) / (int(self.__height) * int(self.__height))
- return bim
-
-
- person = Person('王', 90, 100, '男')
- person.name = "李想"
- print(f'{person.name},{person.bmi()}kg/m')
- class Employee:
- def __init__(self, name, age):
- self.name = name
- self.age = age
- self.ability = 100 - age
-
- def DoWork(self):
- self.ability -= 5
-
- def __str__(self):
- print("姓名:{},年龄:{},能力值:{}".format(self.name, self.age, self.ability))
-
-
- employee = Employee("wang", 9)
- employee.DoWork()
- employee.__str__()
- class Number:
- def __init__(self,n1,n2):
- self.__d1=n1
- self.__d2=n2
-
- @property
- def d1(self):
- return self.__d1
- @d1.setter
- def d1(self,n1):
- self.__d1=n1
- @property
- def d2(self):
- return self.__d2
- @d2.setter
- def d2(self,n2):
- self.__d2=n2
-
- def addition(self):
- return float(self.__d1+self.__d2)
- def subtration(self):
- return float(self.__d1-self.__d2)
- def multiplication(self):
- return float(self.__d1*self.__d2)
- def division(self):
- return float(self.__d1/self.__d2)
-
-
- n1=Number.d1
- n2=Number.d2
- n1=8
- n2=2
- number=Number(n1,n2)
- print('除法的结果为:{}'.format(number.division()))
- print('减法的结果为:{}'.format(number.subtration()))
- print('乘法的结果为:{}'.format(number.multiplication()))
- print('加法的结果为:{}'.format(number.addition()))
- class Student:
- def __init__(self,id,name,age,sex,addr):
- self.id=id
- self.name=name
- self.age=age
- self.sex=sex
- self.__addr=addr
-
- def get_addr(self):
- return self.__addr
- def set_addr(self,addr):
- self.__addr=addr
- def msg(self):
- return ("学号:{},姓名:{},年龄:{},性别:{},地址:{}".format(self.id,self.name,self.age,self.sex,self.__addr))
-
-
- student=Student(101648,'王莽',18,"男","重庆")
- student.set_addr("北京")
- print(student.msg())
- class Computer:
- __slots__ = ("brand","clr","mem")
- def __init__(self,brand,clr,mem):
- self.brand=brand
- self.clr=clr
- self.mem=mem
-
- def play_game(self):
- return "打游戏"
- def write_code(self):
- return "写代码"
- def watch_tv(self):
- return "看电视"
- def __str__(self):
- return ("电脑品牌为:{},颜色为:{},内存大小为:{}".format(self.brand,self.clr,self.mem))
-
-
- computer=Computer("外星人", "黑色", "1TB")
- print(computer.__str__())
- print(computer.play_game())
- print(computer.watch_tv())
- print(computer.write_code())
检查作业申请...
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。