Object-oriented Python

Source: Internet
Author: User

First, object-oriented program design ideas OOD

1. Find the object involved in the program

2, find the class: Inductive objects common characteristics and skills (public properties), summed up the object unique characteristics and skills (Def __init__ (self))

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------

Object-Oriented Programming OOP

1. Define the class first

2. Instantiate an Object

3. Interaction between objects

Three: Code

classStudens:country='China'School='old boy'    def __init__(Self,cla, teacher,name,age): Self.cla=CLA Self.teacher=Teacher Self.name=name Self.age= AgedefSeach_teacher (self):Print("teacher%s for %s"%(self.name,self.teacher))defStudy (self):Print("%s is learning%s"%(SELF.NAME,SELF.CLA)) S1=studens ('python','Egon','Zhanggen',' -') S1.seach_teacher () S1.study ()classTecher:company='Oldboy'Job='Teacher'Hobby='woman'    def __init__(Self,name,age,teach): Self.name=name Self.age=Age Self.teach=TeachdefTeaching (self):Print('%s Teaching%s'%(Self.name,self.teach))defseach (self):Print('He works at:%s, currently working:%s Hobby:%s'%(self.company,self.job,self.hobby)) T1=techer ('Alex', 35,'python') t1.teaching () T1.seach () T2=techer ('Egon', 45,'Linux') t2.teaching () T2.seach () Zhanggen teacher Egon Zhanggen is learning Pythonalex teaching Python He works in: Oldboy, current work: Teacher Hobby: Womanegon Teaching Linux He works in: Oldboy, currently working: Teacher Hobby: Woman

Iv. Interaction between objects (use methods (function attributes) between objects to call each other to change the properties (variable attributes) of each other

classCh:#definition of Chu Han class    def __init__(self,country,name,arms,attack,mount,life): Self.country=country#NationalSelf.name=name#nameSelf.arms=arms#Take the WeaponSelf.attack=attack#Attack ValueSelf.mount=mount#MountsSelf.life=life#Health Value    defatt (self,enemy): Enemy.life-=Self.attackPrint("%s killed%s%s and%s is left! "%(self.name,enemy.name,self.attack,enemy.name,enemy.life))ifenemy.life<=0:Print("%s killed%s under%s! "%(Self.name,enemy.name,self.mount)) L=ch ('Han','Liu Bang','Little Sword', 300,'Khan Blood BMW', 1000)#instantiation of Liu BangX=ch ('Chu','Xiang Yu','Knives', 300,'Red Rabbit Horse', 1000)#instantiation of Xiang YuL.att (x)#Liu Bang used the attacking method to tell the object of Xiang Yu as a parameter, so that the properties of Liu Bang's object changed; ===== Liu Bang attacks the interaction between Xiang Yu objectsL.att (x) L.att (x) L.att (x) X.att (L)#Xiang Yu used the attacking method to pass the Liu Bang object as a parameter, so that the properties of Liu Bang's object changed; ===== Xiang Yu attacking the interaction between Liu Bang objectsX.att (L) X.att (L) X.att (L) execution results Liu Bang killed Xiang Yu300 Ma Xiangyu and 700 left.The horses! Liu Bang kills Xiang Yu300 Ma Xiangyu and 400 left.The horses! Liu Bang kills Xiang Yu300 Ma Xiangyu and 100 left.The horses! Liu Bang kills Xiang Yu300 people Ma Xiangyu still left-200The horses! Liu Bang killed Xiang Yu under the blood of the BMW! Xiang Yu kills Liu Bang300 Ma Liu Bong and 700 left.The horses! Xiang Yu kills Liu Bang300 Ma Liu Bong and 400 left.The horses! Xiang Yu kills Liu Bang300 Ma Liu Bong and 100 left.The horses! Xiang Yu kills Liu Bang300 people Ma Liu Bong still left-200The horses! Xiang Yu cut Liu Bang to death under the Red Rabbit Horse! 

Object-oriented Python

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.