Object-oriented Python basics

Source: Internet
Author: User

1.Python only class One says that there is no interface to say that it supports multiple class inheritance, subclasses inherit the parent class, and can override public and protected methods, variables in the parent class.

2.Python variables, functions are public by default, single underline variables, function representatives are protected, double underscore variables, function representatives are private.

he.py

#Coding=utf-8#defining the Grandfather classclassGrandfather:__uid1=1#Defining private Variables_uid2=2#Defining protection VariablesUid=3#Define public variables (you can access without creating class instances)    #Defining Constructors    def __init__(self):Pass    #Defining public methods    defgetUid1 (self):returnSelf.__uid1    #Defining protection Methods    def_getuid2 (self):returnSelf._uid2#defining the Father class, inheriting the grandfather classclassFather (grandfather):#Defining Constructors    def __init__(self): Grandfather.__init__(self)#overriding protection methods in grandfather    def_getuid2 (self): Self._uid2=4returnSelf._uid2

Calling code:

Python 3.6.1 (V3.6.1:69C0DB5, Mar, 17:54:52) [MSC v.1900 + bit (Intel)] on Win32
Type "Copyright", "credits" or "license ()" For more information.
>>> from He Import *
>>> F=father ()
>>> print (F.getuid2 ())
4
>>> from He Import *
>>> F=father ()
>>> print (F.getuid2 ())
4
>>> print (F.GETUID1 ())
1
>>>

Object-oriented Python basics

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.