静态变量和静态方法是各种编程语言中广泛使用的编程概念,如C++、PHP、Java等。这些变量和方法属于类和对象。在本节中,我们将学习如何在Python中创建静态变量静态方法

Python中的静态变量

当我们在类内部但在方法外部声明一个变量时,它被称为静态或类变量。它可以直接从类中调用,但不能通过类的实例调用。然而,静态变量与其他成员变量有很大不同,并且不会与Python程序中的相同变量名称发生冲突。

让我们考虑一个程序来演示在Python中使用静态变量的用法。

Static.py

class Employee: # create Employee class name  
    dept = 'Information technology'  # define class variable  
    def __init__(self, name, id):  
        self.name = name       # instance variable  
        self.id = id             # instance variable  
  
# Define the objects of Employee class  
emp1 = Employee('John', 'E101')          
emp2 = Employee('Marcus', 'E105')  
  
print (emp1.dept)   
print (emp2.dept)   
print (emp1.name)   
print (emp2.name)   
print (emp1.id)    
print (emp2.id)   
  
# Access class variable using the class name  
print (Employee.dept) # print the department  
  
# change the department of particular instance  
emp1.dept = 'Networking'  
print (emp1.dept)   
print (emp2.dept)   
  
# change the department for all instances of the class  
Employee.dept = 'Database Administration'  
print (emp1.dept)   
print (emp2.dept)  

输出:

Information technology
Information technology
John
Marcus
E101
E105
Information technology
Networking
Information technology
Networking
Database Administration

在上面的示例中,dept 是一个类变量,在类方法之外定义在类定义内部。而 nameid 是定义在方法内部的实例变量。

使用相同类对象访问静态变量

我们可以使用相同的类对象和点运算符直接访问Python中的静态变量。

让我们考虑一个程序,使用相同的类对象在Python中访问静态变量。

staticVar.py

class Car:  
    # define the class variable or static variable of class Car  
    num = 7  
    msg = 'This is a good Car.'  
  
# create the object of the class  
obj = Car()  
  
# Access a static variable num using the class name with a dot operator.  
print ("Lucky No.", Car.num)  
print (Car.msg)  

输出:

Lucky No. 7
This is a good Car

静态方法

Python有一种属于类的静态方法。它就像一个绑定到类而不是类对象的静态变量一样。可以在不创建类的对象的情况下调用静态方法。这意味着我们可以直接使用类名的引用调用静态方法。此外,静态方法受限于类,因此不能更改对象的状态。

静态方法的特点

以下是静态方法的特点:

  1. Python中的静态方法与类相关。
  2. 可以通过引用类名直接从类中调用它。
  3. 不能访问Python程序中的类属性。
  4. 仅绑定到类。因此,它不能修改对象的状态。
  5. 用于将实用方法分离给类。
  6. 只能在类内部定义,而不能定义在类的对象中。
  7. 所有类的对象仅共享静态方法的一个副本。

有两种方式可以在Python中定义静态方法:

  1. 使用staticmethod()方法
  2. 使用@staticmethod装饰器

使用staticmethod()方法

staticmethod()是Python中的一个内置函数,用于将给定的函数返回为静态方法。

语法:

staticmethod (function)

staticmethod()接受一个参数。传递的参数是需要转换为静态方法的函数。

让我们考虑一个程序,使用staticmethod()在Python中创建一个函数作为静态方法。

staticMethod.py

class Marks:  
    def Math_num(a, b): # define the static Math_num() function  
        return a + b  
  
    def Sci_num(a, b): # define the static Sci_num() function  
        return a +b  
  
    def Eng_num(a, b):  # define the static Eng_num() function  
        return a +b  
# create Math_num as static method  
Marks.Math_num = staticmethod(Marks.Math_num)  
  
# print the total marks in Maths  
print (" Total Marks in Maths" , Marks.Math_num(64, 28))   
  
# create Sci_num as static method  
Marks.Sci_num = staticmethod(Marks.Sci_num)  
  
# print the total marks in Science  
print (" Total Marks in Science" , Marks.Sci_num(70, 25))   
  
# create Eng_num as static method  
Marks.Eng_num = staticmethod(Marks.Eng_num)  
  
# print the total marks in English  
print (" Total Marks in English" , Marks.Eng_num(65, 30))  

输出:

Total Marks in Maths 92
 Total Marks in Science 95
 Total Marks in English 95

在上面的程序中,我们使用了staticmethod()函数在类外部将Math_num方法、Sci_num方法和Eng_num方法定义为静态方法。之后,我们可以使用类名Marks直接调用静态方法。

使用@staticmethod装饰器

@staticmethod是一个内置装饰器,用于在类内部定义静态方法。它不接受任何参数作为类实例的引用或调用静态方法的类本身。

语法:

class Abc:  
@staticmethod  
def function_name (arg1, arg2, ?):  
# Statement to be executed  
Returns: a static method for function function_name 

注意:@staticmethod是一种在类中定义方法为静态方法的现代方法,大多数程序员在Python编程中使用这种方法。

让我们创建一个程序,使用@staticmethod装饰器在Python中定义静态方法。

staticFun.py

class Marks:  
    @staticmethod  
    def Math_num(a, b): # define the static Math_num() function  
        return a + b  
  
    @staticmethod  
    def Sci_num(a, b): # define the static Sci_num() function  
        return a +b  
  
    @staticmethod  
    def Eng_num(a, b):  # define the static Eng_num() function  
        return a +b  
  
# print the total marks in Maths  
print (" Total Marks in Maths" , Marks.Math_num(64, 28))   
  
# print the total marks in Science  
print (" Total Marks in Science" , Marks.Sci_num(70, 25))  
  
# print the total marks in English  
print (" Total Marks in English" , Marks.Eng_num(65, 30)) 

输出:

Total Marks in Maths 92
 Total Marks in Science 95
 Total Marks in English 95

使用相同类对象访问静态方法

考虑一个程序,使用@staticmethod在Python中使用类的相同对象访问类的静态方法。

Test.py

class Test:  
    # define a static method using the @staticmethod decorator in Python.  
    @staticmethod  
    def beg():  
        print ("Welcome to the World!! ")  
  
# create an  object of the class Test  
obj = Test()  
# call the static method   
obj.beg()  

输出:

Welcome to the World!!

使用静态方法返回值

让我们编写一个程序,使用@staticmethod在Python中返回值。

Static_method.py

class Person:   
    @staticmethod  
    def Age (age):  
        if (age <= 18): # check whether the Person is eligible to vote or not.  
            print ("The person is not eligible to vote.")  
        else:  
            print ("The person is eligible to vote.")  
  
Person.Age(17) 

输出:

The person is not eligible to vote.

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程