練習自定義函式
'''
實現一個Rectangle類別,
該類別具有width和height兩個屬性。在初始化方法中,
接受一個寬度和一個高度,並將其分別設置為對象的屬性。
'''
class Rectangle:
def __init__(self,width,height):
self.width=width
self.height=height
def arrey(self):
return self.width*self.height
def lenght(self):
return (self.width+self.height)*2
four=Rectangle(3,4)
print(four.arrey())
print(four.lenght())
-------------終端機
12
14
'''
實現一個BankAccount類別,
該類別具有account_number和balance兩個屬性。
在初始化方法中,接受一個帳號和一個初始餘額,
並將其分別設置為對象的屬性。
'''
class BankAccount:
def __init__(self,account_number,balance=1000):
'''開戶時預設存入1千元'''
self.account_number=account_number
self.balance=balance
def save(self,money):
'存款'
if isinstance(money,int):
self.balance += money
return self.balance
else:
raise ValueError('只接受整數數字')
def carry_money(self,money):
'提款'
if isinstance(money,int):
self.balance -= money
return self.balance
else:
raise ValueError('只接受整數數字')
def show(self):
'查看餘額'
return print(f'帳戶{self.account_number}, 餘額為{self.balance}')
a=BankAccount('Qoo',1000)
a.show()
a.save(7777)
a.show()
a.carry_money(2111)
a.show()
---------------------終端機
帳戶Qoo, 餘額為1000
帳戶Qoo, 餘額為8777
帳戶Qoo, 餘額為6666
'''
實現一個Book類別,
該類別具有title和author兩個屬性。
在初始化方法中,
接受一個標題和一個作者,並將其分別設置為對象的屬性。
'''
class Book:
def __init__(self,title,author):
self.title=title
self.author=author
def __str__(self):
return f'書名:{self.title}, 作者:{self.author}'
Jump=Book('one_piece','Jp')
print(Jump)
書名:one_piece, 作者:Jp
'''
實現一個Car類別,
該類別具有make和model兩個屬性。
在初始化方法中,接受一個製造商和一個型號,
並將其分別設置為對象的屬性。
'''
class Car:
def __init__(self,make,model):
self.make=make
self.model=model
def __str__(self) -> str:
return f'製造廠:{self.make}, 型號:{self.model}'
first_car=Car('mazday','cx-5')
print(first_car)
製造廠:mazday, 型號:cx-5
'''
實現一個Employee類別,
該類別具有name和salary兩個屬性。
在初始化方法中,
接受一個名字和一個薪水,並將其分別設置為對象的屬性。
'''
class Employee:
def __init__(self,name,salary):
self.name=name
self.salary=salary
def show(self):
return f'{self.name}的薪水是{self.salary}'
def __str__(self):
return f'員工:{self.name}, 薪水:{self.salary}'
Kun=Employee('Kun',150000)
print(Kun.show())
print(Kun)
----------------------終端機
Kun的薪水是150000
員工:Kun, 薪水:150000
'''
實現一個Circle類別,該類別具有radius屬性。
在初始化方法中,接受一個半徑並將其設置為對象的屬性。
'''
class Circle:
def __init__(self,radius):
self.radius=radius
def area(self):
return print(f'圓面積: {self.radius**2*3.14}')
def lenght(self):
return print(f'圓周長: {2*self.radius*3.14}')
def __str__(self):
return f'圓半徑:{self.radius}'
c1=Circle(2)
c1.area()
c1.lenght()
print(c1)
------------------------終端機
圓面積: 12.56
圓周長: 12.56
圓半徑:2
'''
實現一個Student類別,該類別具有name和grade兩個屬性。
在初始化方法中,
接受一個名字和一個年級,並將其分別設置為對象的
'''
class Student:
def __init__(self,name,grade):
self.name=name
self.grade=grade
def show(self):
return print(f'該學員是{self.name}, 目前就讀{self.grade}')
def up_grade(self):
'升學'
self.grade +=1
def down_grade(self):
'降級'
self.grade -=1
water=Student('Water',3)
water.show()
water.up_grade()
water.show()
water.down_grade()
water.show()
-
-------------------------終端機
該學員是Water, 目前就讀3
該學員是Water, 目前就讀4
該學員是Water, 目前就讀3
PS C:\Users\K\OneDrive\桌面\leetcode>

0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁