Python (vii) Advanced section: Object-oriented

Source: Internet
Author: User

I. Definition of Class

# Object Oriented # meaningful object-oriented code # class =  Object-oriented #  Class, Object # instantiation #  class The most basic role: encapsulation
class Student ():     "'              = 0 def print_file (self): print ('       Name'+self.name)        print('name'+ str (self.age)) Student# instantiation student.print_file ()

Two, the difference between the function and the method

Methods and functions: Design-level functions: a kind of appellation of program running and past style

Third, class and object.

Classes are behaviors and characteristics that describe a class

object is a concrete description object

Class can generate many different objects like a template

Four, the constructor function

#class, ObjectclassStudent (): Name="' Age=0#Behavior and Characteristics    def __init__(self):#constructor Function        Print('Student')    defPrint_file (self):Print('name'+self.name)Print('name'+str (self.age)) s=Student () s.__init__() Student#The constructor is called when the instantiation is instantiatedStudent#The constructor is called separately

The constructor cannot return a value other than none

V. Distinguishing between module variables and variables in a class

Six, class variables and instance variables

Class variables: Variables related to classes

Instance variables: variables associated with an object

classStudent (): Name="'  #class VariablesAge =0#Behavior and Characteristics    #class variables, instance variables    def __init__(self,name,age):#constructor Function        #Initialize the properties of an objectSelf.name=name#Defining instance VariablesSelf.age= Age#print (' student ')    defPrint_file (self):Print('name'+self.name)Print('name'+str (self.age)) s=student (' Daming', 20)Print(S.name)#to print an instance variable, you need to save the instance variable inside the class
Print (Student.name) #打印类变量
Daming
‘‘

Vii. class and object variable lookup order

Viii. Self and example method

Ix. accessing instance variables and class variables in an instance method

X. Types of methods

Xi. static methods

12. Visibility of Members: public and private

13. Nothing is inaccessible

14, inheritance.

XV, subclass method Call Parent class method: Super keyword

Python (vii) Advanced section: 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.