Object
1. What is an object? An instance that has properties and methods (actions that can be done on an object) can be called an object. For example, a ball, the property has the ball's color, the ball's size, the ball's weight, the method has the ball movement direction;
2, properties and methods. As described in 1, the attribute is well understood, is able to describe the object characteristics of some variables, the method can be understood as a number of code blocks, the code block can do some things to the object, and we usually say the function is somewhat similar;
3. What is a class? The description of the object (what is going to be done with this object) or the blueprint is called a class;
4. Create an instance of the class. The definition of a class simply writes a framework, what attributes it will have, what functions are to be implemented, all is empty talk, the class will play a real role only if the class is used to create a real object, and the object is called an instance of this class.
5. Special methods __init__ () and __str__ (). As soon as you create a new instance of the class, run the __init__ () method, which passes the values you want to the properties of the instance and completes the initialization. __STR__ () can print the contents of a class according to their own ideas,
such as: Def __str__ (self):
msg = "Hi,i ' m A" +self.size+ "" +self.color+ "ball!"
Return msg
6, polymorphic. The same method, different behavior. The method of the same function name, used in different classes, implements different functions, called polymorphism.
7, inheritance. Learn from the parent class. The parent class has properties and method subclasses as well, and subclasses can add their own properties and methods to improve the code reuse rate.
such as: Class Gameobject:
def __init__ (self,name):
Self.name = Name
def pickUp (Self,player):
Pass
Class Coin (Gameobject): #继承GameObject类
def __init__ (Self,value):
Gameobject.__init__ (self, "coin") #继承初始化方法并添加新的属性
Self.value = value
def spend (Self,buyer,seller): #添加新的方法
Pass
8, the keyword pass in Python can be used as a placeholder, 1:30 will not remember to write what content, the first placeholder this, so the function method module does not error
#Give it a try T1" "establish a class definition for bankaccount. It should have some attributes, including the account name (a string), the account number (a string or integer), and the balance (a floating-point number), plus some ways to show the balance, save money, and withdraw cash ." "classBankAccount:def __init__(self,account_name,account_number): Self.account_name=account_name Self.account_number=Account_number self.account_overage= 0.0defshowoverage (self):Print "The account balance is", Self.account_overagedefSave (Self,money): Self.account_overage= Self.account_overage + MoneyPrint "You save", MoneyPrint "The new overage is", Self.account_overagedefWithdrawal (Self,money):ifSelf.account_overage >=Money:self.account_overage= Self.account_overage- MoneyPrint "You withdrawal", MoneyPrint "The new overage is", Self.account_overageElse: Print "You tried to withdrawal", MoneyPrint "Your Balance is", Self.account_overagePrint "No enough funds"MyAccount= BankAccount ("Weimiaomiao", 123456)Print "Account Name", Myaccount.account_namePrint " Account Number", MyAccount.account_numbermyAccount.ShowOverage () Myaccount.save (100) Myaccount.withdrawal (20.5) Myaccount.withdrawal (50)#Give it a try T2" "create a class that earns interest, called Interestaccount. This should be a subclass of BankAccount (so the properties and methods of BankAccount are inherited). Internetaccount should also have a property that corresponds to the interest rate, and there is another way to increase interest. For simplicity, assume that the Addinternet () method is called once a year to calculate interest and update the balance" "classBankAccount:def __init__(self,account_name,account_number): Self.account_name=account_name Self.account_number=Account_number self.account_overage= 0.0defshowoverage (self):Print "The account balance is", Self.account_overagedefSave (Self,money): Self.account_overage= Self.account_overage + MoneyPrint "You save", MoneyPrint "The new overage is", Self.account_overagedefWithdrawal (Self,money):ifSelf.account_overage >=Money:self.account_overage= Self.account_overage- MoneyPrint "You withdrawal", MoneyPrint "The new overage is", Self.account_overageElse: Print "You tried to withdrawal", MoneyPrint "Your Balance is", Self.account_overagePrint "No enough funds" classInterestaccount (BankAccount):def __init__(self, interest_rate): BankAccount.__init__(Self,"Weimiaomiao", 123456)#inheriting the BankAccount classSelf.interest_rate =interest_ratedefaddinterest (self): interest= Self.account_overage *self.interest_ratePrint "Your Interest is", InterestPrint "Your Overall funds is", interest+self.account_overage MyAccount= Interestaccount (0.2) Myaccount.save (100) Myaccount.withdrawal (20.5) Myaccount.withdrawal (50) myaccount.addinterest ()
#父与子的编程之旅 # 14th Chapter