1. 巢狀類別的傳遞
public class roll //第一層類別
{
public string str1 { get; set; }
public int a { get; set; }
public string getstring()
{
return str1 + a.ToString();
}
public class roll2 //第二層類別
{
public string str2 { get; set; }
public int b { get; set; }
public string getstring2(roll AA) // 把類別roll當傳遞參數
{
return AA.str1 + AA.a.ToString(); //引用時要以 " 物件.屬性" 來使用
}
}
}
class Program
{
static void Main(string[] args)
{
roll ma = new roll{str1 = "AAA",a=123 }; //設定roll的str1和a值
roll.roll2 ma2 = new roll.roll2 { str2 = "BBB", b = 456 };
Console.WriteLine(ma2.getstring2(ma)); //傳入物件roll
Console.ReadKey();
}
}
2. 一般類別的傳遞
public class roll
{
public string str1 { get; set; }
public int a { get; set; }
public string getstring()
{
return str1 + a.ToString();
}
}
public class roll2
{
public string str2 { get; set; }
public int b { get; set; }
public string getstring2(roll AA)
{
return AA.str1 + AA.a.ToString();
}
}
class Program
{
static void Main(string[] args)
{
roll ma = new roll{str1 = "AAA",a=123 };
roll2 ma2 = new roll2 ();
Console.WriteLine(ma2.getstring2(ma));
Console.ReadKey();
}
}
留言列表