成员方法(函数)用来表现对象行为
申明在类语句块中,规则和函数申明规则相同
成员方法不用加static
关键字
成员方法必须实例化对象,再通过对象来使用,相当于该对象执行了某个行为
class Person
{
public string name;
public int age;
public Person friends
public void AddFriends(Person p)
{
if(friends==null)
{
friends = new Person[]{p};
}
else
{
//数组增加
Person[] newFriends= new Person[frineds.Length+1];
for (int i =0;i<friends.Length;i++)
{
newFriends[i]=friends[i];
}
newFriends[newFriends.Length-1]=p;
friends=newFriends;
}
}
public void Speak(string str)
{
Console.WriteLine("{0}说{1}",name,str);
}
public bool isAdult()
{
return age >= 18;
}
}
//实例化对象
Person p = new Person();
p.name = "robot";
p.age = 1 ;
p.Speak("bot")
标签:方法,成员,Person,new,friends,public
From: https://www.cnblogs.com/cannedmint/p/18617992