C# C语言中的多种方法#

C# C语言中的多种方法#,c#,C#,我想有很多方法。然而,我不想一遍又一遍地写它。我想要方法bBIntersectsB1,bBIntersectsB2,…,bBIntersectsB9,bBIntersectsB10。在每一种方法中,唯一的变化是代替blueBallRect1,我想要blueBallRect2,…,blueBallRect9,blueBallRect10 public bool bBIntersectsB1(Rect barTopRect, Rect barBottomRect, Rect blueBallRect

我想有很多方法。然而,我不想一遍又一遍地写它。我想要方法bBIntersectsB1,bBIntersectsB2,…,bBIntersectsB9,bBIntersectsB10。在每一种方法中,唯一的变化是代替blueBallRect1,我想要blueBallRect2,…,blueBallRect9,blueBallRect10

public bool bBIntersectsB1(Rect barTopRect, Rect barBottomRect, Rect blueBallRect1)
{
    barTopRect.Intersect(blueBallRect1);
    barBottomRect.Intersect(blueBallRect1);

    if (barTopRect.IsEmpty && barBottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}

只需制作一个方法,如下所示:

public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
    topRect.Intersect(ballRect);
    bottomRect.Intersect(ballRect);

    if (topRect.IsEmpty && bottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();

listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;

foreach(BlueBallRect ball in listOfBlueBallRect)
{
    if(DoesIntersect(topRect, bottomRect, ball))
    {
        // Do something here, because they intersect
    }
    else
    {
        // Do something else here, because they do not intersect
    }
}
然后只需调用
DoesIntersect
,如下所示:

public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
    topRect.Intersect(ballRect);
    bottomRect.Intersect(ballRect);

    if (topRect.IsEmpty && bottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();

listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;

foreach(BlueBallRect ball in listOfBlueBallRect)
{
    if(DoesIntersect(topRect, bottomRect, ball))
    {
        // Do something here, because they intersect
    }
    else
    {
        // Do something else here, because they do not intersect
    }
}
只要替换
blueBallRectX
,就可以重复多次

您还可以在
BlueBallRect
对象列表中循环,并将每个对象传递给
DoesIntersect
方法,如下所示:

public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
    topRect.Intersect(ballRect);
    bottomRect.Intersect(ballRect);

    if (topRect.IsEmpty && bottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();

listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;

foreach(BlueBallRect ball in listOfBlueBallRect)
{
    if(DoesIntersect(topRect, bottomRect, ball))
    {
        // Do something here, because they intersect
    }
    else
    {
        // Do something else here, because they do not intersect
    }
}
List listOfBlueBallRect=new List();
listOfBlueBallRect=SomeMethodThatGetsListOfBlueBallRect;
foreach(BlueBallRect列表中的BlueBallRect球)
{
if(DoesIntersect(顶直、底直、球))
{
//在这里做点什么,因为它们相交
}
其他的
{
//在这里做点别的,因为它们不相交
}
}

只需制作一个方法,如下所示:

public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
    topRect.Intersect(ballRect);
    bottomRect.Intersect(ballRect);

    if (topRect.IsEmpty && bottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();

listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;

foreach(BlueBallRect ball in listOfBlueBallRect)
{
    if(DoesIntersect(topRect, bottomRect, ball))
    {
        // Do something here, because they intersect
    }
    else
    {
        // Do something else here, because they do not intersect
    }
}
然后只需调用
DoesIntersect
,如下所示:

public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
    topRect.Intersect(ballRect);
    bottomRect.Intersect(ballRect);

    if (topRect.IsEmpty && bottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();

listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;

foreach(BlueBallRect ball in listOfBlueBallRect)
{
    if(DoesIntersect(topRect, bottomRect, ball))
    {
        // Do something here, because they intersect
    }
    else
    {
        // Do something else here, because they do not intersect
    }
}
只要替换
blueBallRectX
,就可以重复多次

您还可以在
BlueBallRect
对象列表中循环,并将每个对象传递给
DoesIntersect
方法,如下所示:

public bool DoesIntersect(Rect topRect, Rect bottomRect, Rect ballRect)
{
    topRect.Intersect(ballRect);
    bottomRect.Intersect(ballRect);

    if (topRect.IsEmpty && bottomRect.IsEmpty)
    {
        return false;
    }
    else
    {
        return true;
    }
}
var doesBall1Intersect = DoesIntersect(topRect, bottomRect, blueBallRect1);
var doesBall2Intersect = DoesIntersect(topRect, bottomRect, blueBallRect2);
var doesBall3Intersect = DoesIntersect(topRect, bottomRect, blueBallRect3);
var doesBall4Intersect = DoesIntersect(topRect, bottomRect, blueBallRect4);
var doesBall5Intersect = DoesIntersect(topRect, bottomRect, blueBallRect5);
var doesBall6Intersect = DoesIntersect(topRect, bottomRect, blueBallRect6);
var doesBall7Intersect = DoesIntersect(topRect, bottomRect, blueBallRect7);
var doesBall8Intersect = DoesIntersect(topRect, bottomRect, blueBallRect8);
var doesBall9Intersect = DoesIntersect(topRect, bottomRect, blueBallRect9);
List<BlueBallRect> listOfBlueBallRect = new List<BlueBallRect>();

listOfBlueBallRect = SomeMethodThatGetsListOfBlueBallRect;

foreach(BlueBallRect ball in listOfBlueBallRect)
{
    if(DoesIntersect(topRect, bottomRect, ball))
    {
        // Do something here, because they intersect
    }
    else
    {
        // Do something else here, because they do not intersect
    }
}
List listOfBlueBallRect=new List();
listOfBlueBallRect=SomeMethodThatGetsListOfBlueBallRect;
foreach(BlueBallRect列表中的BlueBallRect球)
{
if(DoesIntersect(顶直、底直、球))
{
//在这里做点什么,因为它们相交
}
其他的
{
//在这里做点别的,因为它们不相交
}
}

请讲清楚一点。你似乎理解使用参数的概念,但你没有明确说明为什么在这种情况下不能使用参数。为什么不能使用一种方法?我不知道你在问什么。请提供更多信息。此方法用于碰撞检测。但是,我有blueBallRect(1-20)。每个blueBallRect包含不同的图像。我该怎么做呢?为什么你不能循环使用ball变量并调用这个方法呢?请更清楚一些。你似乎理解使用参数的概念,但你没有明确说明为什么在这种情况下不能使用参数。为什么不能使用一种方法?我不知道你在问什么。请提供更多信息。此方法用于碰撞检测。但是,我有blueBallRect(1-20)。每个blueBallRect包含不同的图像。我该怎么做呢?为什么你不能循环使用ball变量并调用这个方法呢?