Python类和对象的定义与实际应用案例分析

时间:2022-10-14 07:54:41

本文实例讲述了python类和对象的定义与实际应用。分享给大家供大家参考,具体如下:

1.dvd管理系统

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding:utf-8 -*-
#! python3
class dvd:
  def __init__(self,name,price,state):
    self.name=name;
    self.price=price
    self.state=state
  def __str__(self):
    stat='已借出';
    if self.state==1:
      stat='未借出'
    return '名称:《%s》 单价:%d 状态:%s'%(self.name,self.price,stat)
class dvdmanager:
  #存储 多个dvds 每一个元素 都是一个dvd
  dvds=[]
  def init(self):
    self.dvds.append(dvd('光辉岁月',10,0));
    self.dvds.append(dvd('绅士', 5, 1));
    self.dvds.append(dvd('海阔天空', 11, 1));
  def menu(self):
    self.init()
    while(true):
      print('1.查询所有dvd')
      print('2.增加dvd')
      print('3.借出dvd')
      print('4.归还dvd')
      print('5.退出')
      choose=int(input('请选择:'));
      if choose==1:
        self.showalldvd();
      elif choose==2:
        self.insertdvd();
      elif choose==3:
        self.jiechudvd();
      elif choose==4:
        self.rollbackdvd();
      elif choose==5:
        print('5.谢谢使用')
        break;
  def showalldvd(self):
    for d in self.dvds:
      print(str(d))
  def jiechudvd(self):
    name = input('请输入dvd的名称:')
    ret = self.checkdvd(name);
    if ret != none:
      if ret.state==0:
        print('dvd 已经借出去了')
        #return;
      else:
        days=int(input('请输入借出天数:'))
        ret.state = 0;
        print('借出 %d 天,应还利息%d '%(days,ret.price*days))
    else:
      print('dvd 不存在!')
  def rollbackdvd(self):
    name = input('请输入归还dvd的名称:')
    ret = self.checkdvd(name);
    if ret == none:
      print('dvd 不存在!')
    else:
      if ret.state == 1:
        print('dvd 未借出!')
      else:
        days= int(input('请输入借出去了多少天?'))
        y_money=ret.price * days
        print('借出 %d 天,应还利息%d ' % (days, y_money))
        money=0;
        while true:
          money= int(input('请输入金额:'))
          #带学生完成..
          if money<y_money:
            print('不够!')
          else:
            break;
        if money>=y_money:
          print('找零:%d'%(money-y_money));
        ret.state=1;
        print('归还成功!')
  def insertdvd(self):
    name = input('请输入dvd的名称:')
    ret= self.checkdvd(name);
    if ret != none:
      print('dvd 已经存在!')
    else:
      price=int(input('请输入价格:'))
      new_dvd=dvd(name,price,1);
      self.dvds.append(new_dvd)
      print('存放成功!')
  def checkdvd(self,name):
    for d in self.dvds:
      if d.name==name:
        return d;#返回当前对象-->内存地址
    else:
      return none;
manager= dvdmanager();
manager.menu();

本机测试运行结果:

Python类和对象的定义与实际应用案例分析

2.给宠物看病

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding:utf-8 -*-
#! python3
class pet:
  def kanbing(self):
    print('给宠物看病。。')
class car(pet):
  def kanbing(self):
    print('给猫咪看病 吃药,吃小黄鱼..')
class dog(pet):
  def kanbing(self):
    print('给狗狗看病 输液 吃骨头....')
class bird(pet):
  def kanbing(self):
    print('看小鸟看病 拔罐 吃虫子... ')
class pig(pet):
  def kanbing(self):
    print('给小猪看病 针灸 吃猪粮... ')
class shouvet:
  def jiuzhen(self,pet):
    print('兽医出诊..给宠物看病...')
    pet.kanbing();
class person:
  def kanbing(self):
    print('给人类看病 望闻问切 多喝水..')
dingdang=car();
kala=dog();
huangli=bird();
peiqi=pig()
bianque=person()
syi=shouvet();
syi.jiuzhen(dingdang);
syi.jiuzhen(kala);
syi.jiuzhen(huangli);
syi.jiuzhen(peiqi);
syi.jiuzhen(bianque)

本机测试运行结果:

Python类和对象的定义与实际应用案例分析

3.计算机

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding:utf-8 -*-
#! python3
class operation(object):
  '''
  四则运算的父类,接收用户输入的数值
  '''
  def __init__(self, number1=0, number2=0):
    self.num1 = number1
    self.num2 = number2
  def getresult(self):
    pass
  pass
#加法运算类
class operationadd(operation):
  def getresult(self):
    return self.num1 + self.num2
#减法运算类
class operationsub(operation):
  def getresult(self):
    return self.num1 - self.num2
#乘法运算类
class operationmul(operation):
  def getresult(self):
    return self.num1 * self.num2
#除法运算类
class operationdiv(operation):
  def getresult(self):
    if self.num2 == 0:
      return '除数不能为0 '
    return 1.0*self.num1 / self.num2
#其他操作符类
class operationundef(operation):
  def getresult(self):
    return '操作符错误'
#简单工厂类
class operationfactory(object):
  def choose_oper(self,ch):
    if ch == '+':
      return operationadd()
    elif ch == '-':
      return operationsub()
    elif ch == '*':
      return operationmul()
    elif ch == '/':
      return operationdiv()
    else:
      return operationundef()
if __name__ == "__main__":
  ch = ''
  while not ch == 'q':
    num1 = input('请输入第一个数值: ')
    oper = str(input('请输入一个四则运算符: '))
    num2 = input('请输入第二个数值: ')
    # operation(num1,num2)
    of = operationfactory()
    oper_obj = of.choose_oper(oper)
    oper_obj.num1 = num1
    oper_obj.num2 = num2
    print('运算结果为: ',oper_obj.getresult())

希望本文所述对大家python程序设计有所帮助。

原文链接:https://blog.csdn.net/mid_python/article/details/79232225