如何在C#中从另一个方法调用对象

4

有谁能帮助一个初学者学习C#语言?我试图调用在同一类但不同方法中创建的对象。这两个方法都是从另一个类中调用的。下面是简化的代码,我省略了方法执行的其他代码。一个错误提示在第二个方法中“listener”对象无法识别。非常感谢你提供的任何帮助。

// this 1st class calls methods of a 2nd class
public class Lru_operation
{
    // create an object of the 2nd class
    public Lru_Listen LruListen = new Lru_Listen();

    // this method calls two methods from other class   
    public void LruOperation()
    {   
        LruListen.ListenForAag();          // first method call

        LruListen.LruListenAccReq();       // second method call 
    }
}

// this is the 2nd class 
public class Lru_Listen
{
    // 1st method creates an object from a different class (HttpListener)
    public void ListenForAag()
    {
        HttpListener listener = new HttpListener(); 
    }

    // 2nd method calls 1st method's object to perform 
    // a method task from a different class
    public void LruListenAccReq()
    {
        HttpListenerContext context = listener.Getcontext();        
    }
}  
4个回答

4
为了在两种不同的方法中调用此内容,这两种方法都需要访问该值。由于它们在同一类型上,共享listener值的最简单方式是将其设置为字段。
public class Lru_Listen {
  HttpListener listener;

  public void ListenForAag() {
    listener = new HttpListener(); 
  }

  public void LruListenAccReq() {
    HttpListenerContext context = listener.Getcontext();        
  }
}  

2
问题纯粹在于 Lru_Listen 类中 - 您声明的变量是局部的 ListenForAag 成员。如果将其改为类级别变量(字段),您将不会遇到此问题:
// Make an instance variable:
HttpListener listener;

// 1st method creates an object from a different class (HttpListener)
public void ListenForAag()
{
    // Set the instance variable
    listener = new HttpListener(); 
}

// 2nd method calls 1st method's object to perform 
// a method task from a different class
public void LruListenAccReq()
{
    HttpListenerContext context = listener.Getcontext();        
}

请注意,在这种情况下,最好在构造函数中设置而不是在方法中进行设置:
// this 1st class calls methods of a 2nd class
public class Lru_operation
{
    // create an object of the 2nd class
    // Note that this can be private, since it's only used in this class
    private Lru_Listen lruListen = new Lru_Listen();

    // this method calls two methods from other class   
    public void LruOperation()
    {   
        // No longer required
        // lruListen.ListenForAag();          // first method call

        lruListen.LruListenAccReq();       // second method call 
    }
}

// this is the 2nd class 
public class Lru_Listen
{
    HttpListener listener;

    // use the constructor
    public Lru_Listen()
    {
        listener = new HttpListener(); 
    }

    public void LruListenAccReq()
    {
        HttpListenerContext context = listener.Getcontext();        
    }
}  

这样可以确保即使类的使用者(可能是你)忘记显式调用 ListenForArg,监听器仍然会正确地设置。


0

你需要了解作用域。基本上,除了在ListenForAag函数的作用域中,你的listener不存在。假设你需要在函数中实例化listener。但是,使用构造函数可能更好。

public class Lru_Listen
{
 HttpListener listener;
// 1st method creates an object from a different class (HttpListener)
public void ListenForAag()
{
    listener = new HttpListener(); 
}

// 2nd method calls 1st method's object to perform 
// a method task from a different class
public void LruListenAccReq()
{
    HttpListenerContext context = listener.Getcontext();        
}
}  

0

你可以在第二个方法中返回监听器并接收它。

public HttpListener ListenForAag()
{
    listener = new HttpListener(); 
    return listener;
}

public void LruListenAccReq(HttpListener listener)
{
    HttpListenerContext context = listener.Getcontext();        
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接