14.1Python Object-oriented foreplay

Source: Internet
Author: User
Tags chop ming

Python Object-oriented foreplay

For Python, it can be done entirely using functional programming or object-oriented programming, and Java can only be object-oriented.

Object-oriented:
is just an alias

def foo (name,age,gender,content):
Print (name,age,gender,content)
Foo (Xiao Ming, 10 years old, male, go up the hill to chop Wood)
Foo (Xiao Ming, 10 years old, male, driving to Tohoku)

Class Bar:
def foo (self,name,age,gender,content):
Print (name,age,gender,content)

Obj=bar ()

Obj.foo (Xiao Ming, 10 years old, male, go up the hill to chop Wood)
Obj.foo (Xiao Ming, 10 years old, male, driving to northeast)

Object-oriented process:
A. Definition:
Function:
def function name (parameter)

Object-oriented:
Class Name:
def method Name (Self,args)
After the class has a method, the first parameter must write self
Two. Implementation
Function:
Function name (parameter)
Object-oriented:
O= class name () Create a man-in-the-middle O, called an object or instance
O. Method Name ()

class first_class():    def info(self):        print("ok!")        return Trueo=first_class()o.info()print(o.info())

Three. What does self mean
S1= "123" is equivalent to S1=str ("123") str itself is a class
Self equals the current execution object

class sec_class():    def info(self,args):        print(self,args)obj1=sec_class()obj2=sec_class()print(obj1)# <__main__.sec_class object at 0x000000AF402EAAC8>print(obj1.info("haha"))# <__main__.sec_class object at 0x000000AF402EAAC8> haha# Noneprint(obj2)# <__main__.sec_class object at 0x00000065B76BAA20>print(obj2.info("wawa"))# <__main__.sec_class object at 0x00000065B76BAA20> wawa# None

Objects inside the store, can enter the method, could!

class third_class():    def info(self,args):        print(self,self.name,args)obj3=third_class()obj3.name="jiaxin"print(obj3.info("kaka3"))# <__main__.third_class object at 0x0000004EB372AC88> jiaxin kaka3

14.1Python Object-oriented foreplay

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.