Python Learning Path-day seventh introduction to-python object-oriented programming

Source: Internet
Author: User

Object-Oriented Programming

In Python, classes and objects are also important components.

And in Python, the basic data types, such as int are encapsulated class, have their own method, it should be similar to the integer in Java

Classes include fields and methods: A field is a variable in a class or object, and a method is a function declared inside a class.

Self Keyword:

Equivalent to this in Java, here is an example:

#!/usr/bin/python# Filename: method.pyclass Person:    def sayHi(self):        print ‘Hello, how are you?‘p = Person()p.sayHi()# This short example can also be written as Person().sayHi()

You must be wondering how Python assigns to self and why you don't need to assign a value to it. Give an example to make this clear. Suppose you have a class called MyClass and an instance of this class MyObject. When you call this object's method Myobject.method (Arg1, arg2), this is automatically converted from Python to Myclass.method (MyObject, Arg1, arg2)-that's how self works. This also means that if you have a method that does not require parameters, you still have to define a self parameter for this method.

The use and description of the init method (equivalent to a constructor function):

A program to be run as soon as the class is instantiated, used as an initialization of an object

#!/usr/bin/python# Filename: class_init.pyclass Person:    def __init__(self, name):        self.name = name    def sayHi(self):        print ‘Hello, my name is‘, self.namep = Person(‘qjx‘)p.sayHi()# This short example can also be written as Person(‘qjx‘).sayHi()
$ python class_init.pyHello, my name is qjx

The use and description of the del method (equivalent to a destructor):

He is used to run after the object is released a program, to release resources and other operations, generally after the program is gone automatically, if you want to release manually, the Call Del statement will be displayed

In general, the scope of variables and methods

    • __privatevar (two underscore prefixes) Python will use it as a private variable

    • There is also a convention, that is, the usual people's naming habits, if a variable only want to use in a class or object, it should be prefixed with a single underscore.

    • Other names will be public and can be used by other classes/objects

inheritance (The Schoolmember class is known as a base class or superclass.) And the teacher and student classes are referred to as export classes or subclasses)

#!/usr/bin/python# Filename:inherit.pyclass Schoolmember: "represents any school member."  def __init__ (self, Name, age): Self.name = name Self.age = Age print ' (Initialized schoolmember:%s) '        % Self.name def tell (self): "Tell my details."    print ' Name: '%s ' Age: '%s ' '% (Self.name, self.age), Class Teacher (Schoolmember): ' Represents a Teacher. ' def __init__ (self, name, age, salary): schoolmember.__init__ (self, name, age) Self.salary = Salary PR int ' (Initialized Teacher:%s) '% Self.name def tell (self): Schoolmember.tell (self) print ' Salary: '%d '    % Self.salaryclass Student (schoolmember): ' Represents a Student. '  def __init__ (self, name, age, marks): schoolmember.__init__ (self, name, age) Self.marks = marks Print ' (Initialized Student:%s) '% Self.name def tell (self): Schoolmember.tell (self) print ' Marks: '%d ' '% SE Lf.markst = Teacher (' Mrs. ShriVidya ', Max, 30000) s = Student (' Swaroop ', ', ') Print # prints a blank linemembers = [T, s]for member in Members:membe R.tell () # Works for both Teachers and Students
$ python inherit.py(Initialized SchoolMember: Mrs. Shrividya)(Initialized Teacher: Mrs. Shrividya)(Initialized SchoolMember: Swaroop)(Initialized Student: Swaroop)Name:"Mrs. Shrividya" Age:"40" Salary: "30000"Name:"Swaroop" Age:"22" Marks: "75"

Python does not automatically invoke the constructor of the base class in a subclass, and you have to call it yourself specifically.

Python can inherit multiple

Python Learning Path-day seventh introduction to-python object-oriented programming

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.