Python Inheritance relationship and DVD case

Source: Internet
Author: User

1. module. getrefcount--view the number of references, throw an exception if there is no reference, 2. Inheritance: Simplify the code. When a subclass is generated, the parent class inherits the property--①self. Property = Current Property ② Parent class. property = Current Property ③ parent class. The _init_ () method ④super (subclass, Self). _init_ () Method 3. The constructor method (which must have a return value) takes precedence over the initialization method class name. MSO is used to invoke the location of the parent class Def _new_ (cls,*,**): print () return Object._new_ (CLS) 4. Class method: @classmethod require parameters    For example, the constructor method static method: @staticmethod no parameters and classes. The object has no relation 5. Singleton mode: Create only one object construction method inside can be judged # Singleton mode class Sing:__instance=none def __new__ (CLS): If cls.__instance==none:cls.__instance=object.__new__ (CLS) return cls.__instance Else:return cls.__ins  Tance s=sing () ss=sing () print (ID (s), ID (ss))  6. Object list sort: Override _lt_ () _str_ (), and list output requires loop def _lt_ (Self,other): Return Self.brand.encode ("GBK") <other.brand.encode ("GBK") der _str_ (self): return Self.brand+str (Self.price) 7. Exception handling and write try: statement except exception type as Alias: statement else: No exception is executed finally: statement 8. Use else, general and break using Flag=falsefor I in range (1,11): If i%3= =0:flag=true print (i) Break else:print ("not divisible")  9.pass do nothing at all. Parse the file with the   iterator adornment pattern XML &NBSP;&NBSP;&NB Sp;dvd Management System case: (Divided into simple simulation and optimization) 1. SimpleAnalog (with errors)
Class DVD:
def __init__ (Self,num=none):
Self.num=num
List=[]
Print ("========dvd management system ========\n1. Query all dvd\n2. Add dvd\n3. Lend dvd\n4. Return dvd\n5. Exit")

Class Handler (DVD):
def __init__ (self):
Dvd.__init__ (self)
num = Int (input ("Please enter the number you want to select"))
List=[]
If NUM is 1:
Print (list)
Handler ()
If NUM is 2:
Value=input ("Please enter the name of the DVD")
List.append (value)
Print (list)
Nu=int (Input ("continue adding 0/1"))
If Nu is 0:
Value = input ("Please enter the name of the DVD")
List.append (value)
Print (list)
Handler ()

If NUM is 3:
Value=input ("Please enter the DVD you want to borrow")
List.remove (value)
Print (list)
Handler ()
If NUM is 4:
Value=input ("Please enter the name of the DVD to be returned")
List.append (value)
Print (list)
Handler ()
Else
Print ("Already exited the system! ")
NU=STR (Input ("re-enter System 0/1"))
If Nu is 0:
Handler ()
Else
Pass

S=handler ()
2. Optimization of the case (can continue to optimize, the current requirements are not clear, limited capacity):
Class DVD:
def __init__ (Self,num=none):
Self.num=num
List=[]
Print ("========dvd management system ========\n1. Query all dvd\n2. Add dvd\n3. Lend dvd\n4. Return dvd\n5. Exit")




Class Handler (DVD):
def __init__ (self):
Dvd.__init__ (self)
num = Int (input ("Please enter the number you want to select"))
List=[]
If NUM is 1:
Print (list)
Handler ()
If NUM is 2:
Value=input ("Please enter the name of the DVD")
List.append (value)
Print (list)
Nu=int (Input ("continue adding 0/1"))
If Nu is 0:
Value = input ("Please enter the name of the DVD")
List.append (value)
Print (list)
Handler ()

If NUM is 3:
Value=input ("Please enter the DVD you want to borrow")
List.remove (value)
Print (list)
Handler ()
If NUM is 4:
Value=input ("Please enter the name of the DVD to be returned")
List.append (value)
Print (list)
Handler ()
Else
Print ("Already exited the system! ")
NU=STR (Input ("re-enter System 0/1"))
If Nu is 0:
Handler ()
Else
Pass

S=handler ()

Python Inheritance relationships and DVD cases

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.