python 面向对象编程_Python面向对象的编程–类和对象

python 面向对象编程

In this tutorial we will discuss about Python Object Oriented Programming (OOP), class, object and constructor with some program examples.

在本教程中,我们将通过一些程序示例来讨论Python面向对象编程(OOP),类,对象和构造函数。

Python is one of the few programming languages which supports both Structured Programming feature as well as Object Oriented Programming approach at the same time. So, in a single program we can have an OOP based section along with a Structured section.

Python是同时支持结构化编程功能和面向对象编程方法的少数编程语言之一。 因此,在一个程序中,我们可以具有基于OOP的部分以及结构化的部分。

Python面向对象编程 (Python Object Oriented Programming)

Object-oriented programming (OOP) is a programming paradigm based on the concept of Classes and Objects. You can read more about OOPs features by following below link.

面向对象编程(OOP)是基于类和对象的概念的编程范例。 您可以通过以下链接阅读有关OOPs功能的更多信息。

https://en.wikipedia.org/wiki/Object-oriented_programming

https://zh.wikipedia.org/wiki/面向对象的编程

Object is actually data structure that contain both data and functions or methods, which helps to manipulate or work with that data.

对象实际上是既包含数据又包含函数或方法的数据结构,这有助于操纵或使用该数据。

Class is a blueprint on which object creation is based. Class is the base for objects. It is important for a class to be defined before creation of an object.

类是对象创建所基于的蓝图。 类是对象的基础。 在创建对象之前定义一个类很重要。

Objects are created or instantiated from class. One class can have mlutiple objects with different attributes. A program example is given below that shows how to define class and then create its objects in Python.

对象是从类创建或实例化的。 一类可以具有具有不同属性的多对象。 下面给出一个程序示例,该示例演示如何定义类,然后在Python中创建其对象。

Python类和对象示例 (Python Class and Object Example)

class BaseClass(object):
	def write(self):
		print "This is an Instance of BaseClass"
# main
BaseObject = BaseClass()
BaseObject.write()

Output

输出量

Python Object Oriented Programming 1

The first line declares the class name with a parameter ‘object’ that indicates that the class is based on an object datatype.

第一行使用参数“对象”声明类名称,该参数表明该类基于对象数据类型。

The next line is the declaration of a method (function) which the object can use later after instantiation. It has one parameter named as self.

下一行是对象实例化后可以使用的方法(函数)的声明。 它有一个名为self的参数。

As a matter of fact, every method must have a first special parameter as self that provides a way for a method to refer to the object itself.

实际上,每个方法都必须具有第一个特殊参数作为self,这为方法引用对象本身提供了一种方法。

This is followed by a set of statements in the function declaration.

函数声明中紧随其后的是一组语句。

Now, the main function comes into picture. Actually, we need not explicitly declare any main function in Python code. Python automatically understands the scope of main if indentations are proper.

现在,主要功能浮现出来。 实际上,我们不需要在Python代码中显式声明任何主要函数。 如果缩进正确,Python会自动理解main的范围。

We have declared a variable in main function that is assigned the class name. Actually this statement creates an object of that particular class.

我们在主函数中声明了一个分配了类名的变量。 实际上,此语句创建该特定类的对象。

Now, we can access the method within the class and this is done by the second statement of the main function.

现在,我们可以访问类中的方法,这是通过main函数的第二条语句完成的。

调用方法 (Calling a Method)

Following syntax is used to access the class methods using objects.

以下语法用于使用对象访问类方法。

Syntax

句法

Object_Name.Method_Name()

Python构造函数 ( Python Constructor)

A constructor is a special method that is automatically called as soon as you create an object. It is actually used to directly initialize the attributes (variables) of the class at the time of object creation. An example is given below that shows how constructor can be defined.

构造函数是一种特殊的方法,创建对象后会立即自动调用它。 实际上,它用于在创建对象时直接初始化类的属性(变量)。 下面给出一个示例,说明如何定义构造函数。

  Example

class BaseClass(object):
	#constructor
	def __init__(self):
		print "A New Object has been Created"
	#method	
	def write(self):
		print "This is an Instance of BaseClass"
# main
BaseObject1 = BaseClass()
BaseObject2 = BaseClass()
BaseObject1.write()
BaseObject1.write()

Output

输出量

Python Object Oriented Programming 2

Here, __init__() method is used to define constructor.

在这里,__init __()方法用于定义构造函数。

In the above program, we have created two objects that are linked with the same class.

在上面的程序中,我们创建了两个与同一类链接的对象。

  Lets take another example to understand the concept of class and object in Python.

让我们再举一个例子来理解Python中类和对象的概念。

class Doctor:
  def __init__(self, docname, docsalary):
      self.docname = docname
      self.docsalary = docsalary
 
  def display(self):
      print "Doctor Name : ", self.docname,  ", Salary: ", self.docsalary
 
Doctor1 = Doctor("Dr. Neeraj", 120000)
Doctor2 = Doctor("Dr. Tushar", 80000)
Doctor1.display()
Doctor2.display()

Output

输出量

Python Object Oriented Programming 3

If you found anything incorrect or have any doubts regarding above Python object oriented programming tutorial then mention it by commenting below.

如果您发现任何不正确的地方或对上面的面向对象的Python编程教程有疑问,请在下面的注释中提及。

翻译自: https://www.thecrazyprogrammer.com/2015/08/python-object-oriented-programming-class-and-object.html

python 面向对象编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值