Getting started with Python--20--classes, objects

Source: Internet
Author: User

Oo=object oriented object-oriented

Python is an object-oriented programming language

Features of OO:

1, Package: Throw a bunch of things together, into a class

2, Inheritance: If a class inside

3, polymorphic: Different classes have the same name of the function, if you call separately, you can ... It looks like there's no egg.

1, the self of Python

You just use it like this, in the Def parameter of class:

Class B:

def setname (self,name):

Self.name=name

def kick (self):

Print (' My name is%s, damn it, who kicked me .... '%self.name ')

You just use it, and when you write Def, the first argument is self.

Call it this way: b=b (' potatoes ')

B=kick () #会有输出

2. Python's __init__ (self)

Class B:

def __init__ (self,name):

Self.name=name

def kick (self):

Print (' My name is%s, damn it, who kicked me .... '%self.name ')

Call it this way: b=b (' potatoes ')

B=kick () #会有输出

__init__ replaced the SetName function, which will be discussed in detail __init__

3. Python's private variables

Class Person:

__name= ' Zai zhe li '

You are now output: P=person ()

Print (P.__name) #是会报错的

Want to output like this: P=person ()

Print (P._person__name) #_类名__变量名称

Python is object-oriented programming, so he has a class, there are objects, not like a single dog, even the object is not

Define a class with class, and the first letter of the class name must be capitalized:

Class CC:

def setxy (self,x,y):

Self.x=x

Self.y=y

def printxy (self):

Print (SELF.X,SELF.Y)

Then: DD=CC () defines an object of the CC class, also the class is a template, DD is the finished product, a template can produce a lot of finished products.

Use del cc to delete this class, after the egg is deleted, the object can still be used

can use

Dd.setxy (4,5), the X and y assignments in the DD object,

dd.__dict__ Viewing function assignment conditions

Getting started with Python--20--classes, objects

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.