Python Object-Oriented programming example parsing

Source: Internet
Author: User

1. Classes and functions

Examples of object-oriented programming:

#!/usr/bin/env python#-*-coding:utf-8-*-classPerson (object):#Adding "__" to the front of the property and variable allows the variable or method to be converted to a private variable, and access to the private variable can be added to the method access    def __init__(self,name,age): Self.__name=name self.__age= Age#define a function to get user information    defPrint_info (self):Print "%s:%s"% (self.__name, self.__age)    #Add "get_name" and "Get_age" methods to get Properties    defget_name (self):returnSelf.__name        defget_age (self):returnSelf.__age            #Add a function to set a name    defSet_name (self,name): Self.__name=name#Add a function that sets the age    defset_age (self,age):if(0< Age <=200): Self.__age= AgeElse:            RaiseValueError ("Bad age!")

It is important to note that in Python, the variable name __xxx__ is similar, which starts with a double underscore, and ends with a double underscore, which is a special variable that can be accessed directly, not a private variable, so it cannot be __name__ used , __score__ A variable name like this.

There are times when you see an instance variable name that starts with an underscore, such _name as an instance variable that can be accessed externally, but, as you see in the rules, when you look at such a variable, it means, "although I can be accessed, but please treat me as a private variable, Do not feel free to access ".

is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. cannot be accessed __name directly because the Python interpreter __name has _Student__name changed the variable so that it can still be accessed by _Student__name __name variables:

>>> bart._person__name'wyl'

However, it is strongly recommended that you do not do this because different versions of the Python interpreter may __name change to different variable names.

All in all, Python itself has no mechanism to stop you from doing bad things, all on your own.

2. Inheritance and polymorphism:

python_class_test1.py File:#!/usr/bin/env python#-*-coding:utf-8-*-#The following functions are written to test the inheritance and polymorphism of classes in Python#Write a animal class, which has a run methodclassAnimal (object):defRun (self):Print "Animal is running ..."#write a cat and dog class inheriting from the animal classclassCat (Animal):defRun (self):Print "Cat is running ..."    defEat (self):Print "Cat is eating ..."classDog (Animal):defRun (self):Print "Dog is running ..."    defEat (self):Print "Dog is eating ..."test2.py File: fromPython_class_test1ImportAnimal,cat,dogdog=Dog () Cat=Cat () Dog.run () dog.eat () cat.eat () Cat.run ()#a = list ()#d = Dog ()#C = Cat ()#determining whether a variable is a type can be judged by isinstance ():#print "What is the type of dog?"#isinstance (D,dog)#print "Judging what type of cat"#isinstance (C.cat)#isinstance (a,list)

Inheritance can take all the functions of the parent class directly, so that you do not have to re-zero, subclasses only need to add their own unique methods, but also the parent class does not fit the method overrides overrides;

With inheritance, you can have polymorphism. When invoking the class instance method, as far as possible the variable as the parent class type, so that all subclass types can be properly received;

The old way of defining a Python class allows for not inheriting from the object class, but this programming is heavily deprecated. At any time, if no suitable class can inherit, it inherits from the object class.

Python Object-Oriented programming example parsing

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.