訪問對象的屬性和行爲
namespace ConsoleApp1
{
class Program
{
int i = 47;
public void call()
{
Console.WriteLine("調用call()方法");
for(i = 0; i < 3; i++)
{
Console.Write(i + " ");
if (i == 2)
{
Console.WriteLine();
}
}
}
//public Program()//定義構造函數
//{
//}
static void Main()
{
Program t1 = new Program();
Program t2 = new Program();
t2.i = 60;
Console.WriteLine("第一個實例對象調用變量i的結果:" + t1.i++);
t1.call(); //使用第一個對象調用類成員方法
Console.WriteLine("第二個實例對象調用變量i的結果:" + t2.i);
t2.call();
}
}
}
對象的引用
類名 對象引用名稱
Book book;
通常一個引用不需要有一個對象相關聯。
引用與對象相關聯:
Book book = new book();