开发者

What does it mean: Defining a class with a method that gets an object of the class type?

Suppose that we have a class called class1.

The class1 has a method called method1 and this method gets an object of type class1. like this:

public class class1
{
     //instance members
     // property methods

    public void method1(class1 obj)
    {
         //...........
    }
}

What does it mean: the method gets an object 开发者_高级运维of this class type? In what scenarios can this be used?


What does it mean: the method gets an object of this class type?

Yep. Nothing odd about that. Why do you ask?

This sort of thing happens all the time. A Set has a method Union which takes another Set. A Giraffe has a method Mate which takes another Giraffe. A Lobster has a method Eat which takes another Lobster. A sequence has a method Concatenate which takes another sequence. And so on.


Most obvious example I can think of:

public class Node
{
    private m_childNodes List<Node>;
    // ...
    public AppendChild(Node child)
    {
        m_childNodes.Add(child);
    }

}


It allows method1 to operate on an outside instance of class1.


It takes an object of type 'Class1'.

For example, you could do:

Class1 myClass = new Class1();
Class1 yourClass = new Class1();
myClass.method1(yourClass);

Each variable we declared of type Class1 is its own object, with its own functions and members.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜