0114 面向对象 习题

此篇博客展示了Python编程的基础应用,包括time库的使用,实现计算器功能,创建Person和WuMingFen类,以及Account模拟账户类。通过实例演示了类的构造方法、重载以及方法的实现,如加减乘除、累加、阶乘、判断素数等。还创建了Customer类,与Account类结合展示了账户操作,如存款、取款,并处理余额不足的情况。
摘要由CSDN通过智能技术生成

time库引用

import time
print(time.asctime())
print(time.clock())
print("开始睡觉")
time.sleep(5) #休眠
print("睡醒了")
print(time.strftime("%Y-%m-%d %H:%M:%S"))
print(time.ctime(3600))
print(time.get_clock_info(""))
print(time.gmtime(3600))
time.mktime()
# 04.方法的参数及返回值
# 定义一个计算器;它的功能有
# 		加,减,乘,除,累加,阶乘
# 		求平方,求次方,判断一个数是否为素数;返回值-boolean,参数-int,并写测试类来测试这个方法
class Calculator():
    def __init__(self):
        pass
    def jia(self,*args):#args不定长参数,可接收无数个数
        return sum(args)
    def jian(self,a,b):
        return a-b
    def cheng(self,*args):
        s = 1
        for a in args:
            s *= a
        return s
    def chu(self,a,b):
        return a/b
    def lei_jia(self,n):#累加只需要一个数
        sum = 0
        for i in range(1,n+1):
            sum += i
        return sum
    def lei_cheng(self,m):
        sum = 0
        for i in range(1,m+1):
            sum *= i
        return sum
    def ping_fang(self,n):
        return n*n
    def ci_fang(self,n):
        return n**3
    def su_shu(self,n) ->bool:#返回bool类型
        flag = False
        for i in range(2,n//2+1):
            if n%i == 0:
                flag = True
                break
        return not flag#flag默认是False,是素数应该输出True
com = Calculator()
c = com.jia(1,2,3,4,5)
print(c)

# 编写python程序,用于显示人的姓名和年龄。
# 定义一个人类(Person),该类中应该有两个私有属性,姓名(name)和年龄(age)。定义构造方法,用来初始化数据成员。再定义显示(display)方法,将姓名和年龄打印出来。
# 在main方法中创建人类的实例,然后将信息显示。
class Person():
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def display(self):
        print(self.name,self.age)
p = Person('老刘',60)
p.display()

# 07.构造方法与重载
# 为“无名的粉”写一个类:class WuMingFen 要求:
# 1.有三个属性:面码:String: theMa 粉的份量(两):int: quantity
# 是否带汤:boolean: likeSoup
# 2.写一个构造方法,以便于简化初始化过程,如:
# f1 = WuMingFen("牛肉",3,true);
# 3.重载构造方法,使得初始化过程可以多样化:
# f2 = WuMingFen("牛肉",2);
# 4.如何使得下列语句构造出来的粉对象是酸辣面码、2两、带汤的?
# f3 = WuMingFen();
# 5.写一个普通方法:check(),用于查看粉是否符合要求。即:将对象的三个属性打印在控制台上。
class WuMingFen():
    def __init__(self,theMa,quantity,likeSoup):
        self.theMa = theMa
        self.quantity = quantity
        self.likeSoup = likeSoup
    def __init__(self,theMa,quantity):
        self.theMa = theMa
        self.quantity = quantity
        self.likeSoup = True
    def __init__(self):
        self.theMa = '酸辣面吗'
        self.quantity = 2
        self.likeSoup = True
    def check(self):
        print(self.theMa,self.quantity,self.likeSoup)

# 写一个名为Account的类模拟账户。该类的属性和方法如下图所示。该类包括的属性:账号id,余额balance,年利率annualInterestRate;包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。
# Account
# Int: id
# float: balance
# float:annualInterestRate
# init (int :id, float :balance, float: annualInterestRate )
# int getId()
# float getBalance()
# float getAnnualInterestRate()
# void setId( int: id)
# void setBalance(float :balance)
# void setAnnualInterestRate(float: annualInterestRate)
# void withdraw (float :amount)
# void deposit (float: amount)
# 
# 提示:在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
class Account():
    def __init__(self,id,balance,annualInterestRate):
        self.id = id
        self.balance = balance
        self.annualInterestRate = annualInterestRate

    def getId(self):
        return self.id
    def getBalance(self):
        return self.balance
    def getAnnualInterestRate(self):
        return self.annualInterestRate

    def setId(self,id):
        self.id = id
    def setgetBalance(self,balance):
        self.balance = balance
    def setAnnualInterestRate(self,annualInterestRate):
        self.annualInterestRate = annualInterestRate

    def withdraw(self,amount):
        if self.balance >= amount:
            self.balance -= amount
        else:
            print("余额不足")
    def deposit(self,amount):
        self.balance += amount

    def __str__(self):
        return "\n".join((str(self.id),str(self.balance),str(self.annualInterestRate)))

class Customer():
    def __init__(self,firstName,lastName):
        self.firstName =firstName
        self.lastName = lastName
        self.account = None

    def getFirstName(self):
        return self.firstName
    def getLastName(self):
        return self.lastName
    def getAccount(self):
        return self.account
    def setAccount(self,account):
        self.account = account
    def __str__(self):
        return "\n".join((self.firstName,self.lastName))
customer = Customer("Jane","Smith")
account = Account(1000,2000,0.0123)
customer.setAccount(account)
customer.getAccount().deposit(100)
customer.getAccount().withdraw(960)
customer.getAccount().withdraw(2000)
print(customer)
print(customer.getAccount())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值