当前位置:网站首页>[Basic 6] - encapsulation and inheritance of classes, objects and classes
[Basic 6] - encapsulation and inheritance of classes, objects and classes
2022-07-24 05:09:00 【terrific51】
Case study : Bank card
Write a bank card :
attribute : Name of Bank , Card number , password , full name , balance
Method : Sign in , deposit
class Card():
def __init__(self,cnum,cpwd,cname,cbalance):
self.bankName = "python Bank "
self.cnum = cnum
self.cpwd = cpwd
self.cname = cname
self.cbalance = cbalance
def login(self):
num = input(" Please enter the card number :")
pwd = input(" Please input a password :")
if num==self.cnum and pwd==self.cpwd:
print(" Verify success !")
return "ok"
else:
print(" Validation failed !")
return "no"
def deposit(self):
r=self.login()# Call other methods in the question part of the class , Use self As object name
if r=="ok":
money=float(input(" Please enter the deposit amount :"))
self.cbalance+=money
print(" Deposit success ! Deposit in ",money," element ! balance ",self.cbalance," element !")
c1=Card("1001","123"," Zhang San ",1000)
c2=Card("1002","123"," Li Si ",5000)
c2.deposit()
Please enter the card number :1002
Please input a password :123
Verify success !
Please enter the deposit amount :1500
Deposit success ! Deposit in 1500.0 element ! balance 6500.0 element !
One 、 Class encapsulation
Privatize properties or attributes in a class , Privatized properties and methods can only be called inside a class
take Above Private property self.cbalance = cbalance Change it to self.__cbalance = cbalance, The running result is :
AttributeError: ‘Card’ object has no attribute ‘__cbalance’
thus it can be seen , Yes, it is encapsulation , Become private attribute , Cannot directly access
- Encapsulation can restrict access to properties or methods
class Card():
def __init__(self,cnum,cpwd,cname,cbalance):
self.bankName = "python Bank "
self.cnum = cnum
self.cpwd = cpwd
self.cname = cname
self.__cbalance = cbalance
def login(self):
num = input(" Please enter the card number :")
pwd = input(" Please input a password :")
if num==self.cnum and pwd==self.cpwd:
print(" Verify success !")
return "ok"
else:
print(" Validation failed !")
return "no"
def showBalance(self):
# Restrict access to properties or methods
r = self.login()
if r == "ok":
print(" balance :",self.__cbalance," element !")
def deposit(self):
r=self.login()# Call other methods inside the class , Use self As object name
if r=="ok":
money=float(input(" Please enter the deposit amount :"))
self.cbalance+=money
print(" Deposit success ! Deposit in ",money," element ! balance ",self.cbalance," element !")
c1=Card("1001","123"," Zhang San ",1000)
c2=Card("1002","123"," Li Si ",5000)
c2.showBalance()
Please enter the card number :1002
Please input a password :123
Verify success !
balance : 5000 element !
- Private method
take Above Private methoddef login(self):Change it todef __login(self):, Can only be called inside a class , It can improve the security of classes
Two 、 Class inheritance
One class inherits another class , Automatically own the properties and methods of this class
Parent class – Subclass
- Parent class : Define common properties and methods in multiple classes .
- stay Python in , A parent class can have more than one subclass ;
A subclass can have more than one parent class( special )( Multiple inheritance ) - object: The ultimate parent of all classes
# Parent class : Define common properties and methods in multiple classes
# stay Python in , A parent class can have more than one subclass ; A subclass can have more than one parent class ( special )( Multiple inheritance )
#object: The ultimate parent of all classes
class Animal():
def __init__(self,color,age):
print("Animal The initialization method is called !")
self.color=color
self.age=age
def eat(self):
print(" Animals are eating !")
def run(self):
print(" Animals are running !")
class Cat(Animal):# Subclass ( Parent class )
pass
class Dog(Animal):
pass
class Bird(Animal):
def fly(self):
print(" Birds are flying !")
#------------------------------
c1=Cat(" orange ",2)
c1.eat()
b1=Bird(" orange ",2)
b1.fly()
Animal The initialization method is called !
Animals are eating !
Animal The initialization method is called !
Birds are flying !
边栏推荐
- Middle aged crisis, workplace dad who dare not leave, how to face life's hesitation
- MS simulated written test
- postgresql:在Docker中运行PostgreSQL + pgAdmin 4
- The x-fkgom supporting the GOM engine key.lic is authorized to start
- Want to know how a C program is compiled—— Show you the compilation of the program
- Chapter 0 Introduction to encog
- Transpose of array sparse matrix
- Do you want to have a robot that can make cartoon avatars in three steps?
- 13. Write a program, in which a user-defined function is used to judge whether an integer is a prime number. The main function inputs a number and outputs whether it is a prime number.
- Several common sorts
猜你喜欢

pso和mfpso

mapreduce概念
![Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]](/img/1e/95078ad64a17686463547e8ba87cb1.png)
Rlib learning - [4] - algorithmconfig detailed introduction [pytoch version]

Chapter III encog workbench

Heavy! The 2022 China open source development blue book was officially released

Context encoders: feature learning by painting paper notes
![Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]](/img/c1/320e0349e2edda0eb420ed018aa831.png)
Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]

Drools 开发决策表

Redis enhancements

greatest common divisor
随机推荐
pso和mfpso
How to set up an internal wiki for your enterprise?
Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]
How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
What is the sandbox technology in the data anti disclosure scheme?
网NN计算能主机系统资e提供的NTCP
Activation functions and the 10 most commonly used activation functions
Chapter VI more supervision training
Mrs +apache Zeppelin makes data analysis more convenient
C. Recover an RBS (parenthesis sequence, thinking)
How is it that desktop icons can't be dragged? Introduction to the solution to the phenomenon that desktop file icons can't be dragged
Transpose of array sparse matrix
熊市抄底指南
472-82 (22, 165, 39, sword finger offer II 078, 48. Rotate image)
Update C language notes
Learning pyramid context encoder network for high quality image painting paper notes
MapReduce介绍
Chapter V communication training
Globally and locally consistent image completion paper notes
What if IPv4 has no internet access? Solutions to IPv4 without internet access rights (detailed explanation of pictures and texts)