3 回答

TA貢獻1812條經驗 獲得超5個贊
簡短的回答:你不能讓一個對象在另一個類中可見。
但是你可以讓Andrewobject 作為參數傳入類或Output方法。
這是一個傳遞Andrew對象被構造參數的示例,然后你可以調用這個類Output方法來顯示Andrew.PersonAge。
public class TestOutput
{
private Age andrew;
public TestOutput(Age _andrew)
{
andrew = _andrew;
}
public void Output()
{
Console.WriteLine(andrew.PersonAge);
Console.ReadLine();
}
}
Age Andrew = new Age();
Andrew.PersonAge = "30";
TestOutput output = new TestOutput(Andrew);
output.Output();

TA貢獻1895條經驗 獲得超7個贊
你最好的選擇可能是傳遞Andrew給任何需要它的人。或者,您可以創建一個公開它的屬性。你的TestOutput類甚至不會編譯,所以讓我們添加一個接受Age對象的方法:
class TestOutput
{
public static void Output(Age age)
{
Console.WriteLine(age.PersonAge);
Console.ReadLine();
}
}
然后你只需要從Main()以下位置調用它:
static void Main(string[] args)
{
Age Andrew = new Age();
Andrew.PersonAge = "30";
TestOutput.Output(Andrew);
}
- 3 回答
- 0 關注
- 239 瀏覽
添加回答
舉報