本文介绍了对象之间的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//-------------------Class 01
public partial class Form1 : Form
{
PackCards_1 packCards_1 = new PackCards_1();
private void card1_MouseClick(object sender, MouseEventArgs e)
        {
            card13 = packCards_1.Card01; //card13 is of type Card
            //I want to transfer the properties from one Card to another Card
            //but nothing is happening
            //where do I fail?
        }
}
//-------------------Class 02
 public class PackCards_1
    {
        public Card Card01 = new Card();
        private void InitializeComponent()
        {
            Card01.point = 1;
            Card01.atack = 1;
            Card01.shield = 1;
            Card01.life = 1;
            Card01.UpdateCard();
        }
    }



我尝试过的事情:

我尝试过的这段代码.我不撒谎.



What I have tried:

this code I tried. I dont lie.

推荐答案


card13 = new Card();
card13.point = packCards_1.Card01.point;
card13.atack = packCards_1.Card01.atack;
// do the same for the rest of the properties


public void Clone(Card item)
{
    // either set the fields one at a time, or use reflection to get the PropertyInfo[] 
    // for all of the properties in the Card object, and loop through that array, 
    // calling the _set method for each property. (I would probably choose the 
    // reflection approach if it was me, since classes are subject to change frequently 
    // during development.) 
}



您可能还想创建一个接受Card对象作为参数的构造函数重载,然后如果使用现有的Card对象创建新的Card实例,则可以从那里调用Clone方法.



You might also want to create a constructor overload that accepts a Card object as a parameter, and then you can call the Clone method from there if you''re creating new Card instances with existing Card objects.


这篇关于对象之间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:42