Python class inheritance and refactoring

Source: Internet
Author: User

Python class inheritance and refactoring 0 objects
    • An instance of a data structure defined by a class.
    • The object consists of two data members (class variables and instance variables) and methods.
#!/usr/bin/python# -*- coding: UTF-8 -*- class Employee:   ‘所有员工的基类‘   empCount = 0    def __init__(self, name, salary):      self.name = name      self.salary = salary      Employee.empCount += 1      def displayCount(self):     print "Total Employee %d" % Employee.empCount    def displayEmployee(self):      print "Name : ", self.name,  ", Salary: ", self.salary      
    • The Empcount variable is a class variable whose value is shared among all instances of the class. You can use Employee.empcount access in internal classes or outside classes.
    • The first method, the __init__ () method, is a special method called the constructor or initialization method of a class that is called when an instance of the class is created.
    • Self represents an instance of a class, and self is necessary when defining a method of a class, although you do not have to pass in the appropriate arguments when calling
Self represents an instance of a class, not a class
    • There is only one special difference between a method of a class and a normal function--they must have an extra first parameter name, according to the Convention its name is self.
class Test:    def prt(self):        print(self)        print(self.__class__) t = Test()t.prt()

The result of the above instance execution is:

<__main__.Test instance at 0x10d066878>__main__.Test
    • As can be seen from the execution results, self represents an instance of the class, representing the address of the current object, while Self.class points to the class.
    • Self is not a python keyword, and we can do it normally if we change it to Runoob:

    • You can add, remove, and modify the properties of the class.

Inheritance of Classes
    • Some of the characteristics of inheritance in Python:
    1. The construction of the base class in inheritance (Theinit() method) is not automatically called, it needs to be called specifically in the construction of its derived class.
    2. When calling a method of a base class, you need to prefix the class name of the base class with the Self argument variable. The difference is that you do not need to take the self argument when calling a normal function in a class
    3. Python always looks for a method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look up one by one in the base class. (Find the method that was called in this class before you can find it in the base class).
1 Method overrides
    • If the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.

Python class inheritance and refactoring

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.