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.
精彩评论