Python Object-oriented

Source: Internet
Author: User

1. Basic

Class Student (object):    name = ' Student ' # default    def __init__ (self, Name, score):        Self.score = score # publicself . __name = name # private     def set_name (name):      self.__name = name          def get_name ():      return Self.__name

  

2. Inheritance

Class Animal (object):    def run (self):        print (' Animal is running ... ')        class Dog (Animal):    def run (self) :        print (' Dog is running ... ') class Cat (Animal):    def run:        print (' Cat is running ... ')  def run_ Twice (animal):    animal.run ()    Animal.run ()

To run the example:

>>> Run_twice (Animal ()) Animal is running ... Animal is running ... When we pass in the instance of Dog, Run_twice () prints out:>>> Run_twice (dog ()) Dog is running ... Dog is running ... When we pass in an instance of Cat, Run_twice () prints out:>>> Run_twice (Cat ()) Cat is running ... Cat is running ...

The advantage of polymorphism is that the person who passes the argument is the method that calls it. Have an inheritance relationship with each other.

Object is an inherited parent class for all classes.

3. Judging the inheritance relationship

Dog = Dog () print (Isinstance (Dog, Animal)) # Animal

True

4. Dynamic binding function

Class Student (object):    passdef set_age (self, Age): # defines a function as an instance method     Self.age = Agestudent.set_score = Set_score # gives Class added functions = Student () s.set_score (100) # can be called

Add a method to only one instance

From types Import Methodtypeclass Student (object):    passdef set_age (self, Age): # defines a function as an instance method     Self.age = ages = St Udent () s.set_score= Methodtype (Set_score, s) # Bind a method to an instance S.set_score (100) # can be called S2 = Student () s2.set_score (100) # cannot be called

5. Enumeration

from enum Import enummonth = enum (' Month ',      (' Jan ', ' Feb ', ' Mar ', ' April ', ' may ', ' June ', ' Jul ', ' April ', ' Sep ', ' Oct ', ' Nov ') ' Dec ')) for name, member in Month.__members__.items ():    print (name, ' = = ', member, ', ', Member.value)

Execution effect

(' Jan ', ' = = ', <month.jan:1>, ', ', 1) ...

Other examples of enumerations

From enum import enum, Unique@uniqueclass Weekday (enum):    Sun = 0 # Sun's value is set to 0    Mon = 1    Tue = 2    Wed = 3< C4/>thu = 4    Fri = 5    Sat = 6

Execution results

>>> day1 = weekday.mon>>> print (day1) weekday.mon>>> print (weekday.tue) weekday.tue> >> print (weekday[' Tue ')) weekday.tue>>> print (Weekday.Tue.value) 2>>> print (Day1 = = Weekday.mon) true>>> print (Day1 = = Weekday.tue) false>>> print (Weekday (1)) weekday.mon>>> Print (Day1 = = Weekday (1)) true>>> Weekday (7) Traceback (most recent call last):  ... Valueerror:7 is not a valid weekday>>> for name, member in Weekday.__members__.items (): ...     Print (name, ' = = ', member) ... Sun = Weekday.sunmon = Weekday.montue = weekday.tuewed = Weekday.wedthu = Weekday.thufri = Weekday . Frisat = Weekday.sat

  

Resources

Liaoche's Python tutorial

https://www.liaoxuefeng.com/

Python Object-oriented

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.