亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python OOP:类、对象、继承和构造函数与示例

Python 中的 OOP

Python 中的 OOP 是一种编程方法,专注于使用与其他通用编程语言相同的对象和类。对象可以是任何真实世界的实体。 Python 允许开发人员使用 OOP 方法开发应用程序,主要关注代码的可重用性。在 Python 中创建类和对象非常容易。

什么是类?

Python 中的类是数据和函数的逻辑分组。它提供了创建包含任意内容并因此易于访问的数据结构的自由。

例如,对于任何想要在线获取客户详细信息的银行员工都将转到 customer class ,其中将列出其所有属性,例如交易详细信息、取款和存款详细信息、未偿债务等。

在本教程中,我们将学习,

如何定义 Python 类

要定义类,您需要考虑以下几点

步骤 1) 在 Python 中,类由 “类” 定义 关键词

class myClass():

步骤 2) 在类内部,您可以定义属于此类的函数或方法

def method1 (self):
   print "Guru99"
def method2 (self,someString): 
   print "Software Testing:" + someString

步骤 3) 类中的所有内容都是缩进的,就像函数、循环、if 语句等中的代码一样。任何不缩进的东西都不在类中

注意 :关于在 Python 中使用“self”

第 4 步) 制作类的对象

c = myClass()

第 5 步) 调用类中的方法

c.method1()
    c.method2(" Testing is fun")

第 6 步) 这是完整的代码

# Example file for working with classes
class myClass():
  def method1(self):
      print("Guru99")
        
  def method2(self,someString):    
      print("Software Testing:" + someString)
  
      
def main():           
  # exercise the class methods
  c = myClass ()
  c.method1()
  c.method2(" Testing is fun")
  
if __name__== "__main__":
  main()

继承的工作原理

继承是面向对象编程中使用的一个特性。它指的是定义一个新类,对现有类进行较少或没有修改。新类称为派生类 并且从它继承的一个称为 base . Python支持继承;它还支持多重继承 .一个类可以从另一个称为子类或继承类的类继承属性和行为方法。

Python 继承语法

class DerivedClass(BaseClass):
    body_of_derived_class

步骤 1) 运行以下代码

# Example file for working with classes
class myClass():
  def method1(self):
      print("Guru99")
        
  
class childClass(myClass):
  #def method1(self):
        #myClass.method1(self);
        #print ("childClass Method1")
        
  def method2(self):
        print("childClass method2")     
         
def main():           
  # exercise the class methods
  c2 = childClass()
  c2.method1()
  #c2.method2()

if __name__== "__main__":
  main()

请注意,在 childClass 中,method1 没有定义,但它是从父 myClass 派生的。输出是“Guru99”。

步骤 2) 取消注释第 8 行和第 10 行。运行代码

现在,方法 1 在 childClass 中定义,输出“childClass Method1”正确显示。

步骤 3) 取消注释第 9 行。运行代码

您可以使用语法调用父类的方法

ParentClassName.MethodName(self)

在我们的例子中,我们调用 myClass.method1(self) 并且 Guru99 按预期打印

第 4 步 ) 取消注释第 19 行。运行代码。

调用子类的方法2,按预期打印“childClass method2”。

Python 构造函数

构造函数是将对象实例化为预定义值的类函数。

它以双下划线 (_) 开头。它是__init__()方法

在下面的示例中,我们使用构造函数获取用户的名称。

class User:
    name = ""

    def __init__(self, name):
        self.name = name

    def sayHello(self):
        print("Welcome to Guru99, " + self.name)

User1 = User("Alex")
User1.sayHello()

输出将是:

欢迎来到 Guru99,Alex

Python 2 示例

以上代码是 Python 3 示例,如果您想在 Python 2 中运行,请考虑以下代码。

# How to define Python classes
# Example file for working with classes
class myClass():
  def method1(self):
      print "Guru99"
        
  def method2(self,someString):    
      print "Software Testing:" + someString
      
   
      
def main():           
  # exercise the class methods
  c = myClass ()
  c.method1()
  c.method2(" Testing is fun")
  
if __name__== "__main__":
  main()


#How Inheritance works
# Example file for working with classes
class myClass():
  def method1(self):
      print "Guru99"
        
      
class childClass(myClass):
  #def method1(self):
        #myClass.method1(self);
        #print "childClass Method1" 
        
  def method2(self):
        print "childClass method2"     
         
def main():           
  # exercise the class methods
  c2 = childClass()
  c2.method1()
  #c2.method2()

if __name__== "__main__":
  main()

总结:

“类”是功能和数据的逻辑分组。 Python 类提供了面向对象编程的所有标准特性。


Python

  1. C# 类和对象
  2. Python 面向对象编程
  3. Python 对象和类
  4. Python 继承
  5. 使用 Raspberry Pi 和 Python 构建机器人
  6. 结构和类的区别:用 C++ 示例解释
  7. Python String strip() 函数与示例
  8. 集合中的 Python 计数器示例
  9. Python 中的 type() 和 isinstance() 示例
  10. Python 列表 index() 与示例
  11. Java - 对象和类
  12. Python - 面向对象