【发布时间】:2016-06-28 11:34:53
【问题描述】:
我有 2 个类包含有关用户的一些信息。一个保存数据,另一个保存控制信息。现在我需要将它们都作为参数传递给方法。但是它们是相互连接的,我不认为将 2 个类作为参数单独传递是可以的。我想知道我是否应该将它们放在Tuple 或将它们保持在一起的东西中,这样我就可以将它们作为 1 个参数传递给任何方法。以下是它们的外观:
数据类:
public class UsersProperties
{
public enum CUser
{
Player,
Bot1,
Bot2,
Bot3,
Bot4,
Bot5
}
public int RightCard { get; set; }
public string Name { get; set; }
public int? Chips { get; set; }
public int Type { get; set; }
public bool Turn { get; set; }
public bool FoldTurn { get; set; }
public int PreviousCall { get; set; }
public int LeftCard { get; set; }
public double Power { get; set; }
public int EnumCasted { get; set; }
}
控件类:
public class UserControls
{
public Point CardsLocation { get; set; }
public AnchorStyles CardsAnchor { get; set; }
public Panel Panel { get; } = new Panel();
public Point PanelLocation { get; set; }
public Size PanelSize { get; } = new Size((Settings.Width + 10) * 2, Settings.Height + 20);
public int IndentationPanelXy { get; } = 10;
public Label UsernameLabel { get; set; } = new Label();
public Point UsernameLabelLocation { get; set; }
public Size UsernameLabelSize { get; } = new Size(Settings.Width * 2, 20);
public TextBox ChipsTextBox { get; set; } = new TextBox();
public Label StatusLabel { get; set; } = new Label();
public UserControls(AnchorStyles style, Point cardsLocation, bool down)
{
CardsAnchor = style;
CardsLocation = cardsLocation;
UsernameLabelLocation = down ? new Point(CardsLocation.X, CardsLocation.Y - 20) : new Point(CardsLocation.X, CardsLocation.Y + Settings.Height);
PanelLocation = new Point(CardsLocation.X - IndentationPanelXy, CardsLocation.Y - IndentationPanelXy);
}
}
现在这是我初始化它们的方法:
private static Player Player = new Player(Properties.Settings.Default.StartingChips);
private UserControls PlayerControls = new UserControls(AnchorStyles.Bottom, new Point(560, 470),false);
这是我需要通过它们的方法:
private void SetPlayers(UsersProperties user, UserControls userControls, int turn, ref bool check, Image refreshbackImage)
{
if (user.Chips <= 0) return;
_foldedPlayers--;
if (turn < user.RightCard || turn > user.LeftCard) return;
if (Holder[user.RightCard].Tag != null)
{
Holder[user.LeftCard].Tag = _reserve[user.LeftCard];
}
Holder[user.RightCard].Tag = _reserve[user.RightCard];
if (!check)
{
_horizontal = userControls.CardsLocation.X;
_vertical = userControls.CardsLocation.Y;
}
check = true;
Holder[turn].Anchor = userControls.CardsAnchor;
Holder[turn].Image = refreshbackImage;
if (turn < Bot1.RightCard)
{
Holder[turn].Image = Deck[_i];
}
Holder[turn].Location = new Point(_horizontal, _vertical);
_horizontal += Holder[turn].Width;
Holder[turn].Visible = true;
Controls.Add(userControls.Panel);
userControls.Panel.Location = userControls.PanelLocation;
userControls.Panel.BackColor = Color.DarkBlue;
userControls.Panel.Size = userControls.PanelSize;
userControls.Panel.Visible = false;
if (_i != user.LeftCard) return;
check = false;
}
我想知道是否有更好的方法将 2 个类作为参数传递,我认为它们应该粘在一起。
【问题讨论】:
-
如果你想把它们放在一个容器类中,那就这样做吧。但是没有什么说你不应该只给两个对象作为参数。把它们放在一个元组中只会让它更复杂,不会给你任何东西
标签: c# class oop methods parameters