Introduction to Python (vii) class

Source: Internet
Author: User

Class
is an object-oriented vector
definition of the class:

class A (object):         pass         全局变量         函数1。。。         def....(self):         函数2.。。。         def....():

There are many functions in the class, the first parameter of the function is self
Variables can be directly defined directly within the class
When a class calls a function or variable internally, it must use self .
Self represents the individual after the class instantiation
Instantiate the class with the first letter lowercase to represent the class, and then instantiate the

Constructors for classes

class A (object):        def __init__(self,name):                self.name=name

Is the function that the class executes first when it is instantiated.

Inherited:

class A(object):    def __init__(self,name):        self.name=name    def student(self):        print ("please tall my name")class B (A):    def grand(self):        print("i will be back")a=A("huang")        

b invokes the attribute in a, which teaches inheritance
Rewrite:

class A(object):    def __init__(self,name):        self.name=name    def student(self):        print ("please tall my name")class B (A):def __init__(self):        print("this is test")def grand(self):        print("i will be back")def student(self):        print("my name is tom")B.student()

A has student,b also has the student, if has the init constructor in B, overrides the function, calls the Class B's student, does not go to the parent class to look for

To give a simple example:

class 人(object):    def __init__(self,name):        self.name=name        有两条腿        有年龄            有头发class 男人(人):      有妻子:      有胡子:class女人(人):        有丈夫:class和尚(人):        def __init__(self):        没有头发

That's basically what it means,
A man has two legs, an age, a wife, a beard, and a male heir to the hair.
A woman has two legs, an age, a husband, a woman with hair, a man's property,
Monk has two legs, age, no hair, hair this attribute and the parent class (person) conflict, then need to rewrite, need to have a constructor

Introduction to Python (vii) class

Related Article

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.