Python 3.x Learning note 9 (Object-oriented)

Source: Internet
Author: User

1. Object-oriented
Object-Oriented is a method of understanding and abstraction of the real world, which is the product of the development of computer programming technology to a certain stage.


2. Classes (Class):
A class is an abstraction, a blueprint, a prototype for a class of objects that have the same properties. The properties of these objects are defined in the class (variables (data)), common methods

3. Objects (object):
An object is an instantiated instance of a class, a class must be instantiated before it can be called in the program, a class can instantiate multiple objects, each object can also have different properties, like human refers to everyone, each person refers to the specific object, people and people before there is a common, there are different

4. Features

Packaging
The assignment of data in a class, internal invocation is transparent to external users, which makes the class A capsule or container in which the data and methods of the class are contained.

Inherited
A class can derive subclasses, properties, methods defined in the parent class, automatic quilt class inheritance

Polymorphic
Polymorphism is an important feature of object-oriented, simple point: "An interface, a variety of implementations", refers to a base class derived from a different subclass, and each subclass inherits the same method name, but also the parent class method to do a different implementation, this is the same thing shows a variety of forms.


5. Constructors
Do some initialization work for classes at instantiation

Instance variable: SELF.A = a
Instance variable (static property), scoped to the instance itself

Example

1 classPerson :2n =123#类变量3 4 def __init__ (self,name,ability):5 #构造函数6 7Self.name =name #实例化变量 (static property), scoped to instantiate itself8Self.ability =Ability9 Ten def Eat (self): # class Method Function (dynamic property) OnePrint'I'll eat something! ') A  - def run (self): -Print'I'll runing!') the  - def sleep (self): -Print'I'll sleep!') -  + def doing (self): -Print'I am%s'%self.ability) +  AR1 = Person ('Zhang San','Dancing') at  - R1.sleep () - R1.run () - r1.eat () - r1.doing () -Print (R1.N)

Results

I'll sleep! ! !   I am dancing1234

Python 3.x Learning note 9 (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.