【问题标题】:Referencing variable in class from within the same class从同一类中引用类中的变量
【发布时间】:2018-12-17 21:53:09
【问题描述】:

我目前第一次需要将我的应用程序重写到库中。到目前为止我已经成功了,但是我需要以某种方式进行自动重复过程,这可以通过简单的 camShield.start() 来启动。

但我无法从任何地方引用启用。这里的想法是我将使用计时器启动线程,这将检查启用的变量。但要做到这一点,我需要另一个函数,比如 stop(),它将启用的变量设置为 false。

有没有更好的方法来实现这样的功能?
---编辑----
我需要编写函数 CamShield.start() 和 CamShield.stop(),它们将能够访问 CamShield.enabled 变量。

这是我正在尝试解决的部分代码(它是类库)

using SharpAdbClient;
using System;
using System.Diagnostics;
using System.Threading;

namespace BaReader
{
    public class Private
    {
        public class CamShield
        {
            internal bool enabled = true;

            public static void start()
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;                        
                    Timer camShieldTimer = new Timer(tap, null, 0, 20000);
                }).Start();
            }
        }

        internal static void tap(Object o)
        {
            AdbClient.Instance.ExecuteRemoteCommand("input tap 600 900", Device.lookup(), null);
            Debug.WriteLine("Tapped");
        }
    }
}

提前感谢您的任何想法。

【问题讨论】:

  • 无法理解您面临的问题,您能否更清楚地说明您正在尝试做什么,或者您面临什么错误?这将提高获得解决方案的机会。
  • internal 表示可以从库中访问该变量。如果您想从图书馆外访问,请将其公开。
  • 要注意的是,我不知道如何在同一个类中编写函数 .start() 和 .stop(),因为当我在 Private 类或 CamShield 中创建这些函数时,我无法从 CamShield.start() 和 CamShield.stop() 访问 CamShield.enabled
  • 不要使用静态方法。
  • 这对我有什么帮助?即使 start() 不是静态的,我也无法从其中访问启用的变量。

标签: c# class variables static-libraries


【解决方案1】:

您已将方法声明为静态,并将您的变量启用为非静态,因此您无法访问它,

    public class CamShield
    {
        internal bool enabled = false;
        public void start()
        {
            if(!enabled)
            {
                enabled = true;
                //your code to start
            }
        }

        public void stop()
        {
            if(enabled)
            {
                //your code to stop
                enabled = false;
            }
        }
    }

我相信您可以实例化 CamShield 类并从外部访问启动和停止方法。

【讨论】:

  • 好的,非常感谢,这样的声明有效!谢谢。
  • 很高兴它有帮助! :)
【解决方案2】:

为了停止线程,你需要使用Abort 杀死它。所附问题将为您提供足够的工具和知识来实现​​目标。

其次,由于您的范围,您无法访问已启用。再看一下代码:

    public class Private
    {
        public class CamShield
        {
            internal bool enabled = true;

            public static void start()
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;                        
                    Timer camShieldTimer = new Timer(tap, null, 0, 20000);
                }).Start();
            }
        }

        internal static void tap(Object o)
        {
            AdbClient.Instance.ExecuteRemoteCommand("input tap 600 900", Device.lookup(), null);
            Debug.WriteLine("Tapped");
        }
    }

您的internal bool enabled 在 CamShield 类的范围内,除非您初始化 CamShield 类,否则您的 tap 方法将无法使用。

为了使用你的internal bool enabled,你需要在你的私有类中声明它,然后在 Tap 中使用它:

    public class Private
    {
        internal bool enabled = true;

        public class CamShield
        {
            enabled = false;
            public static void start()
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;                        
                    Timer camShieldTimer = new Timer(tap, null, 0, 20000);
                }).Start();
            }
        }

        internal static void tap(Object o)
        {
            enabled = true;
            AdbClient.Instance.ExecuteRemoteCommand("input tap 600 900", Device.lookup(), null);
            Debug.WriteLine("Tapped");
        }
    }

【讨论】:

  • 您好,感谢您的回复。我不需要它在 tap() 函数中可用,但无论如何,即使我像你一样在 Private 下声明它,我也不能在 CamShield 中调用它,也不能在 tap() 中调用它。
  • 尝试将其声明为公开。
猜你喜欢
  • 1970-01-01
  • 2017-02-14
  • 2017-06-30
  • 2012-07-29
  • 2016-10-11
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多