8. Object-oriented class

Source: Internet
Author: User
The object concept is the same as that in other languages. A basic class
  1. #!/usr/bin/pythonclass person:        def hi(self,name):                print ‘Hello,%s‘%namep1= person()p1.hi(‘lk‘)

[[email protected] python_scripts]# python 12class.py Hello,lk

Class initialization: When the variable needs to be used in multiple functions of the class, if it is necessary to initialize the function in the class, the function parameters must be added when the object is created.Def _ init _ (self, name, salary): -- defines self. name1 = Name
P1 = person ('lk ', 20000) -- you can use print p1.name1 to call the class itself, that is, the created P1. Therefore, you can use P1 to directly call the variable.
  1. [[email protected] python_scripts]# cat 12class.py #!/usr/bin/pythonclass person:        def __init__(self,name,salary):                self.name=name                self.salary=salary        def hi(self):                print ‘Hello,%s‘% self.name        def hi2(self):                print ‘Yes,%s have %s every month!‘%(self.name,self.salary)p1= person(‘lk‘,20000)p1.hi()p1.hi2()

[[email protected] python_scripts]# python 12class.py Hello,lkYes,lk have 20000 every month!

Exercises: Create a dog class (with names and gender). It can be called, eaten, and seen as the opposite sex.
  1. [[Email protected] python_scripts] # Cat 13dogclass. py #! /Usr/bin/pythonclass dog: def _ init _ (self, name1, sex1): Self. name = name1 self. sex = sex1 def bark (self, default = 'low'): ifdefault = 'low': Print 'low .............. 'else: print 'hige ............. 'def eat (self, default = 'like'): ifdefault = 'like': Print 'Good food, I like it! 'Else: print' Im full ~ ~ ~ ~ ~ ~ ~ 'Def tail (self, default): If self. Sex! = Default: Print 'Oh! Shark ~ ~ I like you ~ ~ 'Else: Print 'hello, you are my friend! 'D1 = dog ('www ', 'male') # create a public dog D2 = dog ('mmm', 'female') # create a bitch d1.bark () # dog name: d1.eat () # Dinner d1.tail ('male') # D1 sees a public dog d1.tail (d2.sex) # D1 sees D2

  1. [[email protected] python_scripts]# python 13dogclass.py low..............good food,i like it!hello ,you are my friend!oh! shark~ ~ i like you~ ~

In actual use, no object is created in the class program file, and the object is created when the class is called:
  1. >>>import dogclass>>> d1=dogclass.dog(‘www‘,‘male‘)>>> d2=dogclass.dog(‘mmm‘,‘female‘)>>> d1.bark()low..............>>> d1.eat()  good food,i like it!>>> d1.tail(‘male‘)hello ,you are my friend!>>> d1.tail(d2.sex) oh! shark~~~~>>> d1.name ‘www‘

Class inheritance:
  1. Classperson: # parent class def _ init _ (self, type, sex, age, name): Self. race = Type self. sex = sex self. age = Age self. name = Name def talk (self, MSG = 0): Self. MSG = MSG if self. MSG! = 0: Print self. name, 'saying: ', self. msgclass person_info (person): # subclass, which represents the inherited def _ init _ (self, type, sex, age, name, nation, work, salary ): person. _ init _ (self, type, sex, age, name) # Call the initialization method self of the parent class. country = Nation self. job = work self. salary = salary def tell (self, MSG): Return ''' % s' personal information: name: % s age: % s nation: % s work: % s ''' % (self. name, self. name, self. age, self. country, self. job)

B1 = person_info ('yellow', 'male', '18', 'lk ', 'China', 'it', '000000') b2 = person_info ('yellow ', 'male', '18', 'Tom ', 'China', 'it', '000000') G1 = person_info ('yellow', 'famale', '18 ', 'jack', 'China', 'it', '000000') print '% s is looking for boyfriend: % s' % (g1.name, b1.tell ('aaa ')) Expected result:Jack is looking for boyfriend: LK's personal information: name: LK age: 18 nation: China work: it

8. Object-oriented class

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.