The path to Python-object oriented (Basic)

Source: Internet
Author: User

Object-Oriented Programming: classes, Objects

Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".

Introduction to Object-oriented technology
    • Class: A collection of objects that describe the same properties and methods, which defines the properties and methods that are common to each object in the collection.
    • Object: An instance of a class
creating classes and Objects
1 classPerson :2        3     def __init__(self,name,age):4Self.name =name5Self.age = Age6 7     defTalk (self,msg):8         Print("%s-talk:%s"%(self.name,self.msg))9obj = Person ("Alex", "23")
    • Class is the keyword representation, followed by the class name "person"
    • Create Object obj = Person (), class name parentheses (arguments, parameters to pass the constructor method into)
    • What's a self?

Self:python automatically passes the parameters that represent the object of your instance. The corresponding properties and methods of the object can be called through self.

    • Construction method: "__init__ (Self,name,age)"; As long as the object is created, the method is automatically executed and then I can access it directly with the object: Obj.name &obj.age
1P_obj1 = Person ("Alex"," at")2P_obj2 = Person ("LCY"," -")3 Print(P_obj1.name,p_obj1.age)#Alex4 Print(P_obj2.name,p_obj2.age)#LCY
Object-oriented three major features: encapsulation, inheritance, polymorphic encapsulation

The path to Python-object oriented (Basic)

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.