python类和对象_Python类和对象

python类和对象

Python is an object-oriented programming language. Python Classes and Objects are the core building blocks of Python programming language.

Python是一种面向对象的编程语言。 Python类和对象是Python编程语言的核心构建块。

Python类 (Python Class)

By this time, all of you should have already learned about Python Data Types. If you remember, basic data types in python refer to only one kind of data at a time.

到这个时候,你们所有人都应该已经了解了Python数据类型 。 如果您还记得的话,python中的基本数据类型一次仅引用一种数据。

How would it be if you could declare a data type which itself contains more than one data types and can work with them with the help of any function? Python class gives you that opportunity.

如果您可以声明一种数据类型,该数据类型本身包含多个数据类型,并且可以在任何函数的帮助下使用它们,那会怎么样? Python类为您提供了机会。

Python class is the blueprint on which instances of the class are created.

Python类是在其上创建类实例的蓝图。

简单的Python类声明 (Simple Python Class Declaration)

Here’s the very basic structure of python class definition.

这是python类定义的基本结构。

class ClassName:  
    # list of python class variables  
    # python class constructor  
    # python class method definitions

Now, let’s work with real examples.

现在,让我们使用实际示例。

#definition of the class starts here  
class Person:  
    #initializing the variables  
    name = ""  
    age = 0  
      
    #defining constructor  
    def __init__(self, personName, personAge):  
        self.name = personName  
        self.age = personAge  
  
    #defining class methods  
    def showName(self):  
        print(self.name)  
  
    def showAge(self):  
        print(self.age)  
          
    #end of the class definition  
  
# Create an object of the class  
person1 = Person("John", 23)  
#Create another object of the same class  
person2 = Person("Anne", 102)  
#call member methods of the objects  
person1.showAge()  
person2.showName()

This example is pretty much self-explanatory. As we know, the lines starting with “#” are python comments. The comments explain the next executable steps. This code produces the following output.

这个例子几乎是不言而喻的。 众所周知,以“#”开头的行是python注释 。 这些注释说明了接下来的可执行步骤。 此代码产生以下输出。

Python类定义 (Python Class Definition)

class Person:

This line marks the beginning of class definition for class ‘Person’.

这行标志着“ Person”类的类定义的开始。

Python类变量 (Python Class Variables)

#initializing the variables  
    name = ""  
    age = 0

‘name’ and ‘age’ are two member variables of the class ‘Person’. Every time we declare an object of this class, it will contain these two variables as its member. This part is optional as they can be initialized by the constructor.

“名称”和“年龄”是“人物”类的两个成员变量。 每次我们声明此类的对象时,它将包含这两个变量作为其成员。 这部分是可选的,因为它们可以由构造函数初始化。

Python类构造函数 (Python Class Constructor)

#defining constructor  
    def __init__(self, personName, personAge):  
        self.name = personName  
        self.age = personAge

Python class constructor is the first piece of code to be executed when you create a new object of a class.

创建类的新对象时,Python类构造函数是要执行的第一段代码。

Primarily, the constructor can be used to put values in the member variables. You may also print messages in the constructor to be confirmed whether the object has been created.

首先,可以使用构造函数将值放入成员变量中。 您也可以在构造函数中打印消息,以确认是否已创建对象。

We shall learn a greater role of constructor once we get to know about python inheritance. The constructor method starts with def __init__. Afterward, the first parameter must be ‘self’, as it passes a reference to the instance of the class itself. You can also add additional parameters like the way it is shown in the example. ‘personName’ and ‘personAge’ are two parameters to sent when a new object is to be created.

一旦了解python继承,我们将学习更多的构造函数角色。 构造函数方法以def __init__开头。 之后,第一个参数必须是“ self”,因为它传递了对类本身实例的引用。 您还可以添加其他参数,例如示例中的显示方式。 “ personName”和“ personAge”是要创建新对象时要发送的两个参数。

Python类方法 (Python Class Methods)

#defining python class methods  
    def showName(self):  
        print(self.name)

Methods are declared in the following way:

方法通过以下方式声明:

def method_name(self, parameter 1, parameter 2, …….)
    statements……..
    return value (if required)

In the pre-stated example, we’ve seen that the method showName() prints value of ‘name’ of that object. We shall discuss a lot more about python methods some other day.

在上述示例中,我们已经看到showName()方法打印出该对象的“名称”值。 前几天,我们将讨论更多关于python方法的信息。

Python类对象 (Python Class Object)

# Create an object of the class  
person1 = Person("Richard", 23)  
#Create another object of the same class  
person2 = Person("Anne", 30)  

#call member methods of the objects  
person1.showAge()
person2.showName()

The way objects are created in python is quite simple. At first, you put the name of the new object which is followed by the assignment operator and the name of the class with parameters (as defined in the constructor).

在python中创建对象的方式非常简单。 首先,放置新对象的名称,其后是赋值运算符,并输入带有参数的类的名称(在构造函数中定义)。

Remember, the number and type of parameters should be compatible with the parameters received in the constructor function.

请记住,参数的数量和类型应与构造函数中接收的参数兼容。

When the object has been created, member methods can be called and member attributes can be accessed (provided they are accessible).

创建对象后,可以调用成员方法并可以访问成员属性(前提是它们是可访问的)。

#print the name of person1 by directly accessing the ‘name’ attribute
print(person1.name)

That’s all for the basics of python class. As we are going to learn about python object-oriented features like inheritance, polymorphism in the subsequent tutorials, we shall learn more about python class and its features. Till then, happy coding and good bye! Feel free to comment if you have any query.

这就是python类的基础知识。 在后续教程中,我们将学习诸如继承,多态性之类的面向对象的python特性时,我们将学习有关python类及其功能的更多信息。 到那时,祝您编程愉快,再见! 如有任何疑问,请随时发表评论。

Reference: Python.org Documentation

参考: Python.org文档

翻译自: https://www.journaldev.com/14628/python-classes-objects

python类和对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值