简体   繁体   English

C#类,子句,IClonable,IDisposable

[英]C# class, subclases, IClonable, IDisposable

I am working in C# some exercises and I don't understand the big picture. 我正在用C#进行一些练习,但我不了解全局。 If these should implement in Java I wodn't have problems. 如果这些应该用Java实现,我不会有问题。 But I am new in C# so I mixed up things and I don't know how te structure should look like. 但是我是C#的新手,所以我把事情混在一起了,不知道结构应该是什么样子。 I read some explanasions like: http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-P and http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx 我阅读了一些解释,例如: http : //www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-Pattern-Phttp://msdn.microsoft.com/zh-cn/library/ fs2xkftw.aspx

I have first class where I make LinkedList and itst type is node, so: LinkedList linkedlist = new LinkedList than I make some nodes in puti it in linkedlist by some methods also here is method print, which prints elements from linkedlist. 我有一个第一类,其中我使LinkedList和它的类型是节点,所以:LinkedListlinkedlist = new LinkedList比我通过某些方法在puti中使它在puti中的某些节点还这里是方法print,它打印来自linkedlist的元素。

My "object" class is Node, where I have constructors, properties and so getters and setters. 我的“对象”类是Node,其中有构造函数,属性以及getter和setter。 I have allso here virtula method print and I don't know what shoudl I put in this method, becuse this node class is just abstract class and here I must implement IDisposable methods: 我在这里打印了virtula方法,我不知道我应该在此方法中添加什么内容,因为该节点类只是抽象类,在这里我必须实现IDisposable方法:

 public void Dispose()  {}
 protected virtual void Dispose(bool disposing) {}

Here I don't undrsetend why should I use IDispose? 在这里,我不会放松,为什么我应该使用IDispose? In all examples I saw it has been used for access to some file and working with pictures. 在所有示例中,我都看到它已用于访问某些文件和处理图片。 In some examples I saw: 在某些示例中,我看到了:

 public interface IDisposable
 {
   void Dispose();
 }

and I don't know if I must put this in some new class, shuld I put it in the same class or even not implement it. 而且我不知道是否必须将其放在某个新类中,是否应该将其放在同一类中,甚至不实现它。

And than I make subclasses NodeLong and Nodestring which uses superclass node whre I put some new properties and setters and getters. 然后,我将使用超类节点的子类NodeLong和Nodestring制成子类,然后添加了一些新属性,setter和getter。 I have also used print method. 我也用过打印方法。 I also don't understand genercis. 我也不太理解。

Linearcollection class: 线性集合类:

public class LinearCollection{
     void Main(string[] args){
         LinkedList<Node> linkedlist = new LinkedList<Node>
          //calling methods for adding some elements, deleting, getting some elements
     }
     //implemented methods

     public void Print(){
        foreach( NodeElement item in linkedList){
              // what to do??
        }
     }

My Nodeelemts class: 我的Nodeelemts类:

 public abstract class Node:{

     private bool _disposed;
     //constructors
     //properties, set, get ex:
     private Object data;
     public Object _data{
        get{
            rethurn this._data;
        }
        set{
            this._data=value;
        }
     }

     public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing){
        if(!_disposed){
            if(disposing){
                //disposed managed resources
            }
            //
        }
        _disposed = true;
        //
    }

     virtual public void Print(){
        //what to implemetn?
     }
 }
 //stil the same file
 public class NodeString : Node{
    //prop
    public string typeOfElement{ get set ...}
   //new constructor where is involved new property
   public overrride void Print()
   {
        Console.WriteLine(_data.ToString());
        base.Print();
    }
 }

 public class NodeLong : Node{
    //prop
    public long typeOfElement{ get set ...}
   //new constructor where is involved new property
   public overrride void Print()
   {
        Console.WriteLine(_data.ToString());
        base.Print();
    }
 }

If I make NodeLang: IClonable than I cant make NodeLang:Node. 如果我使NodeLang:IClonable,则不能使NodeLang:Node。

My instructions says that I must use IClonable methods in both subclasses, I don't know how should I use it? 我的指令说我必须在两个子类中都使用IClonable方法,我不知道该如何使用它? Also use IDisposable in class Node, how? 在Node类中也使用IDisposable,怎么办? I hope I am understandible? 我希望我可以理解吗?

So the finale question is how it should look like my class, subclass, IDisposable and ICloneable? 因此,结题是它的外观如何,如我的类,子类,IDisposable和ICloneable?

There are times when it could make sense for a class which implements IDisposable to have a cloning method, in which case whatever code invokes the cloning method the object would be responsible for ensuring that Dispose will get called the new copy (either by calling Dispose itself, or handing off the responsibility to some other code). 有时对于实现IDisposable的类具有克隆方法可能是有意义的,在这种情况下,无论什么代码调用克隆方法,该对象都将负责确保Dispose被调用为新副本(通过调用Dispose本身) ,或将责任移交给其他代码)。 There are also occasions when it makes sense to have aa container class with a Dispose method that calls Dispose on each member stored therein, though in most such situations the purpose of the container class would center around such disposal and there would be no reason for it to support any kind of cloning. 在某些情况下,使用Dispose方法对存储在其中的每个成员调用Dispose容器类是有意义的,尽管在大多数情况下,容器类的目的将围绕这种处置,因此没有理由支持任何形式的克隆。

Unless the Node class will be exposed to the outside world, I don't see much purpose for having it implement IDisposable . 除非将Node类暴露给外界,否则我看不出让它实现IDisposable目的。 Even if the class which holds the linked list is a disposal aggregator (something which accepts objects whose useful lifetime will be shorter than its own and then cleans them up when its own Dispose method is called) and every node but the last will not only hold something that requires cleanup, but will also know of another node. 即使保存链接列表的类是处置聚合器(某种接受其使用寿命将比其自身短的对象,然后在调用其自己的Dispose方法时将其清除的对象),并且每个节点(但最后一个节点)不仅保持需要清理的东西,但也会知道另一个节点。 Having a node call Dispose on the next node would result in unnecessary recursion, causing a crash if there are too many nodes. 在下一个节点上进行节点调用Dispose将导致不必要的递归,如果节点过多,则会导致崩溃。 An alternative would be to have the Node class implement a Node DisposeAndReturnNext() method which would cause the Node to dispose of all resources it knows about and then return a reference to the next node. 一种替代方法是让Node类实现Node DisposeAndReturnNext()方法,该方法将使Node处置其知道的所有资源,然后返回对下一个节点的引用。 A caller could then use something like: 呼叫者然后可以使用类似:

while(nodeToDispose != null)
  nodeToDispose = nodeToDispose.DisposeAndReturnNext();

Here it would be clear that the DisposeAndReturnNext method would not be expected to dispose the next node (and its descendants), even though it would know that they need disposal. 在这里很明显,即使它知道需要处置,也不希望使用DisposeAndReturnNext方法来处置下一个节点(及其后代)。 While it would be possible for Node to define a Dispose method which includes a loop like the above, having the first list item's disposal handled a public Dispose method and having every other item's disposal handled by a loop like the above seems rather icky. 尽管Node可以定义一个Dispose方法,该方法包括一个类似于上述的循环,但让第一个列表项的处置处理一个公共的Dispose方法,而让其他所有项目的处置都由上述循环处理似乎很棘手。 Better to have one loop, in the enclosing class, which handles disposal for everyone. 最好在封闭类中有一个循环,该循环负责处理每个人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM