2013-05-31 97 views
3

这是我的课谁开始Wireshark过程并返回文件的详细信息:返回我的对象​​是否正确?

public class Capinfos 
    { 
     private int _packets; 
     private string _duration; 

    private void getPackets(string file) 
    { 
     /// 
    } 

    private void getDuration(string file) 
    { 
     /// 
    } 

     public int packets 
     { 
      get { return _packets; } 
     } 

     public string duration 
     { 
      get { return _duration; } 
     } 

    public Capinfos getFileDetails(string file) 
     { 
      this.getNumberOfPackets(file); 
      this.getFileDuration(file); 
      return this; 
     } 
} 

主要

 Capinfos capinfos = new Capinfos(); 
     Capinfos cap = capinfos.getFileDetails(file); 

我的问题是关于getFileDetails功能,是它确定返回我的对象​​这样?

+0

为什么不构建新对象或将方法名称更改为“PopulatePackets”,“PopulateDuration”等? –

+0

您可能想要使用* Constructor Injection *,因为您实际上传递了一个参数,然后返回相同的参数并稍加修改。 – Greg

+0

我可以举个例子吗? – user2214609

回答

3

为什么引用的东西你已经离开。如前所述,您可以使用一个构造函数中,你需要 '建构' 类的信息传递:

public class Capinfos 
{ 
    private int _packets; 
    private string _duration; 

//constructor; 
    public Capinfos(string file) 
    { 
     this.getPackets(file); 
     this.getDuration(file); 

    } 
private int getPackets(string file) 
{ 
    /// 

} 

private string getDuration(string file) 
{ 
    /// 

} 

    public int packets 
    { 
     get { return _packets; } 
    } 

    public string duration 
    { 
     get { return _duration; } 
    } 

}

主营:

Capinfos capinfos = new Capinfos(file);

1

我实际上将该文件作为构造函数。您没有使用像StringBuilder这样的构建器模式,所以它很奇怪返回相同的引用。

+0

你可以把代码吗? – AAlferez

+0

我发布了所有相关的代码 – user2214609

+0

我在告诉@Daniel .... – AAlferez

2

你也可以实现getFilesDetails方法,静态工厂方法

public class Capinfos 
{ 
    private int _packets; 
    private string _duration; 

private void getPackets(string file) 
{ 
    /// 
} 

private void getDuration(string file) 
{ 
    /// 
} 

    public int packets 
    { 
     get { return _packets; } 
    } 

    public string duration 
    { 
     get { return _duration; } 
    } 

public static Capinfos GetFileDetails(string file) 
    { 
     var info = new Capinfos(file); //allowed, because it's the same class 
     info.getNumberOfPackets(file); 
     info.getFileDuration(file); 
     return info; 
    } 

}

使用

var cap = Capinfos.GetFileDetails(file); 
+0

什么好处会给我静态? – user2214609

+1

这只是一种利用'GetFileDetails'方法的方法,工厂模式主要用于如果您想将实例化的类的详细信息保留到工厂方法的情况。假设你有GifFile&JpgFile都从Capinfos继承。通过使用工厂方法,Capinfos类可以根据传入的文件决定创建哪种类型,并且在任何情况下调用代码都会获得Capinfos对象。 – Jason

0

让我提出一个)为您的数据创建一个只读struc,b)结合文件中的读取功能

public struct CapInfo 
{ 
    public readonly int Packets; 
    public readonly string Duration; 

    CapInfo(int packets, string duration) 
    { 
     this.Packets=packets; 
     this.Duration=duration; 
    } 

    public static CapInfo ReadFileDetails(string file) 
    { 
     using(var fs=System.IO.File.Open(file, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 
     {     
      int packets = // from the file 
      string duration = // from the file 

      return new CapInfo(packets, duration); 
     } 

    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var info=CapInfo.ReadFileDetails(file); 
    } 
} 
相关问题