The object-oriented primary knowledge of Python advanced

Source: Internet
Author: User

Object oriented 

Unlike the process-oriented programming idea, object-oriented is a programming idea that abstracts programs into objects, which can better deal with some complex problems in the real world.

Use the following example to get an initial look at object-oriented programming.

classPerson:#Class + classes Name: Define a classPlace ='Earth'  #Class Property (static property)    #The __init__ initialization method, which creates a self object by default to receive the properties of the object. There can be no initialization method    def __init__(self, name, sex, age, nationality):#Self must have, other arguments are defined with the properties of the objectSelf.name = Name#Instantiating PropertiesSelf.sex =Sex Self.age=Age self.nationality=NationalitydefWalk (self):#a method is a function (dynamic property) that defines what this class of objects can do. Self must have        Print('%s Walking'%self.name) Zxc= Person ('Zhang San','male', 24,'China')#object = Class name (instantiated property)-instantiation: Creates a specific objectPrint(Zxc.name)#Zhang San viewing the properties of an objectPrint(Person.place)#Earth View properties of a classPrint(ZXC.__dict__)#View all properties of an object is a dictionaryPrint(person.__dict__)#View all properties of a class is a dictionaryZxc.name ='John Doe'        #Modifying PropertiesZxc.walk ()#John Doe Walk using method equivalent to Person.walk (ZXC)

A simple summary of the above example

"""     class: A collection instantiation used to describe a class of objects with the same properties and methods:    object = class name ()    object: 1, you can view property 2, modify property 3, call Method     class: 1, Instantiate 2, Invoke method, But to self-pass argument 3, view and modify the class property "" "

The following is a look at the namespace of classes and objects, first defining a class, and comparing two objects in an instantiation.

classdemo:class_nature='Static Properties'Class_nature1= ['Static Properties']    def __init__(self, object_nature): Self.nature=object_naturedefdynamic_nature (self):Print('Dynamic Properties') d= Demo ('Instantiating Properties') D1= Demo ('instantiating a property 1')

First study the following classes

# Class can only invoke class properties and dynamic properties that are names in the namespace of the class Print (demo.class_nature) # print (demo.nature)  # Error   class cannot access names in the object namespace

look at the object .

# object can access names in the class namespace Print (d.class_nature)  # static Property d.dynamic_nature ()  #  dynamic Property

you can see that the object can access the name in the namespace of the class, and then see if the class properties can be modified by the object.

#when a property is a immutable data type, the object cannot modify the name in the class namespace, and all modifications are equivalent to creating a new property within the object itselfDemo.class_nature ='Class Property Modification'd.class_nature='Class D Property modification'd1.class_nature='D1 Class Property Modification'Print(demo.class_nature)#Class Property ModificationPrint(d.class_nature)#Class D Property modificationPrint(d1.class_nature)#D1 Class Property Modification

looking at variable data types

#when a property is a mutable data type, the object can modify the elements inside the class property, and the modification will be used for all objects. But the class property itself cannot be modifiedPrint(Demo.class_nature1)#[' static property ']D.class_nature1[0] ='d Modification'D.class_nature1= ['Modify']Print(D.class_nature1)#[' Modify ']Print(Demo.class_nature1)#[' d modify ']Print(D.class_nature1)#[' Modify ']Print(D1.class_nature1)#[' d modify ']

use the combination of classes: an object's property value is an object of another class

Let's take a look at the following example

classTeacher:def __init__(Self,name, age, Birthday): Self.name=name Self.age=Age Self.birthday= Birthday#receives an object of the birthday class so that the birthday class can be manipulated by the object of the teacher classclassBirthday:def __init__(self, year, month, day): Self.year=Year Self.month=month Self.day=Daybirth= Birthday (1992, 12, 23) Teacher= Teacher ('Miss Zhang', 26, birth)Print(teacher.birthday.year)#1992Print(Teacher.birthday.month)# APrint(Teacher.birthday.day)# at

The object-oriented primary knowledge of Python advanced

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.