Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的未填充参数和静态方法_Python_Windows_Class_Object - Fatal编程技术网

Python中的未填充参数和静态方法

Python中的未填充参数和静态方法,python,windows,class,object,Python,Windows,Class,Object,这是一个与windows操作相关的程序。我制作了这个程序的不同版本。所以我试着用类编写一个版本。我是一个初学者,认为这将帮助我理解这个概念,但我得到了一些错误,如 Method 'shutdown_windows' may be 'static' 有很多这样的错误,我想了解一下它们以及如何解决它们 这是我的代码: import os import datetime import time class WindowsOperations(object): def __init__(s

这是一个与windows操作相关的程序。我制作了这个程序的不同版本。所以我试着用类编写一个版本。我是一个初学者,认为这将帮助我理解这个概念,但我得到了一些错误,如

Method 'shutdown_windows' may be 'static'
有很多这样的错误,我想了解一下它们以及如何解决它们

这是我的代码:

import os
import datetime
import time


class WindowsOperations(object):
    def __init__(self, shutdown, restart, open_app, close_app):
        self.shutdown = shutdown
        self.restart = restart
        self.open_app = open_app
        self.close_app = close_app

    def shutdown_windows(self):
        time.sleep(remaining_time)
        while True:
            if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:  #
                os.system("shutdown /s /t 1")
                break

    def restart_windows(self):
        time.sleep(remaining_time)
        while True:
            if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:
                os.system("shutdown /r /t 1")
                break

    def open_application(self):
        app_name_open = input("Enter the application name you want to open: ").lower() + ".exe"
        time.sleep(remaining_time)
        while True:
            if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:
                os.startfile(app_name_open)
                break

    def close_application(self):
        app_name_close = input("Enter the application name you want to close: ").lower() + ".exe"
        time.sleep(remaining_time)
        while True:
            if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:
                os.system("TASKKILL /F /IM " + app_name_close)
                break


choice = int(input("Enter your choice \n1.Shutdown Windows \n2.Restart Windows "
                   "\n3.Open an application \n4.Close an application \n:"))
time_hour = int(input("Enter hour: "))
time_minutes = int(input("Enter minutes: "))
time_now = datetime.datetime.now()
remaining_time = ((time_minutes - time_now.minute) * 60 + (time_hour - time_now.hour) * 3600) % 86400
print(remaining_time)
my_windows_operation = WindowsOperations('shutdown', 'restart', 'open_app', 'close_app')
if choice == 1:
    my_windows_operation.shutdown_windows()
elif choice == 2:
    my_windows_operation.restart_windows()
elif choice == 3:
    my_windows_operation.open_application()
elif choice == 4:
    my_windows_operation.close_application()
else:
    print("Please enter a valid input")
如有必要,请修改代码,并给我一些提示。第二个“参数‘shutdown’unfilled”是一个错误。它的出现是因为您定义了
WindowsOperations
Initializer以获取4个参数:

def __init__(self, shutdown, restart, open_app, close_app):
但在实际创建要传递的对象时:

my_windows_operation = WindowsOperations()
你肯定需要解决这个问题


第一个“方法‘shutdown_windows’可能是‘static’”更像是一个建议。它的出现是因为方法
shutdown\u windows
没有使用
self
——也就是说,它不需要是类的一部分来完成它的工作。

代码没有使用特殊方法中存在的属性(init)。因此,我编写了一个在特殊方法中使用这些属性的方法。这样,您就可以了解如何使用它。我在代码末尾调用该方法,用户可以在其中选择选项。我在方法的括号中设置需要执行的操作,在这种情况下,该方法称为WindowsOperations(“”)。WindowsOperations(“”)中提到的字符串将存储在self.select_操作中。它将根据用户选择的选项进行更改。希望您可以通过可视化此代码来制作此程序的不同版本

import os
import datetime
import time  
class WindowsOperations(object):
    def __init__(self, select_operation):
        self.select_operation=select_operation
    def all_operations(self):
        if self.select_operation=='shutdown':
            time.sleep(remaining_time)
            while True:
                if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:  #
                    os.system("shutdown /s /t 1")
                    break
        if self.select_operation=='restart':
            time.sleep(remaining_time)
            while True:
                if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:
                    os.system("shutdown /r /t 1")
                    break
        if self.select_operation=='open_app':
            app_name_open = input("Enter the application name you want to open: ").lower() + ".exe"
            time.sleep(remaining_time)
            while True:
                if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:
                    os.startfile(app_name_open)
                    break
        if self.select_operation=='close_app':
            app_name_close = input("Enter the application name you want to close: ").lower() + ".exe"
            time.sleep(remaining_time)
            while True:
                if time_hour == datetime.datetime.now().hour and time_minutes == datetime.datetime.now().minute:
                    os.system("TASKKILL /F /IM " + app_name_close)
                    break




choice = int(input("Enter your choice \n1.Shutdown Windows \n2.Restart Windows "
                   "\n3.Open an application \n4.Close an application \n:"))
time_hour = int(input("Enter hour: "))
time_minutes = int(input("Enter minutes: "))
time_now = datetime.datetime.now()
remaining_time = ((time_minutes - time_now.minute) * 60 + (time_hour - time_now.hour) * 3600) % 86400
print(remaining_time)
if choice == 1:
   WindowsOperations('shutdown').all_operations()
elif choice == 2:
    WindowsOperations('restart').all_operations()
elif choice == 3:
    WindowsOperations('open_app').all_operations()
elif choice == 4:
    WindowsOperations('close_app').all_operations()
else:
    print("Please enter a valid input")

那么我能做些什么来防止这种情况呢?我是否应该完全不使用类而只使用函数定义它们?对于“静态”建议,如果您愿意,您可以忽略它。代码仍然有效。这只是你的短绒太迂腐了。所以如果我这样写,代码会正常工作吗?my_windows_operation=WindowsOperations('shutdown'、'restart'、'open_app'、'close_app')@ravuripraneth是否传递所需参数?是否将签名更改为不需要参数?所有这些属性的目的是什么,你似乎根本没有使用它们。@juanpa.arrivillaga你有什么建议?我是一个初学者,我刚刚开始学习课程。所以给我一些明确的答案是很好的。我这样做了,但我仍然得到建议方法“关机窗口”可能是static@RavuriPraneeth是的,您的所有方法都不使用任何内部状态,您的属性在对象中不起任何作用,因此您的所有方法都不需要内部状态,也就是说,它们可以是“静态的”。通常,您只需将其作为常规函数编写。同样,您为什么决定添加这些属性?你的意图是什么?我想他是想在面向对象编程中实现那些常规函数。好吧,我完全不同意这个编辑。一个简单地填充了一堆staticmethods的类不应该是一个类。@Ravuripraneth我已经修改了代码及其工作原理。如果这对你仍然没有帮助,请告诉我。你不明白的是什么?您已将构造函数定义为接受多个参数,但没有传递任何构造函数。如果我做了类似于
def foo(a,b):…
当我做
foo()
时,你期望会发生什么?这是同样的想法。如果您不打算使用任何参数,请不要这样定义函数是的,我已经更改了代码。