using System;
namespace lesson3_member method
{
Declaration of #region member method
//basic concept
//Member method (function) to express object behavior
//1. Declared in the class statement block
//2. is used to describe the behavior of the object
//3. The rules are the same as the function declaration rules
//4. Affected by access modifier rules
//5. Return value parameters are not limited
//6. The number of methods is not limited
//Notice
//1. Do not add static keyword to member method
//2. The member method must instantiate the object and use it through the object is equivalent to the object performing a certain behavior
//3. Member methods are affected by access modifiers
class Person
{
/// <summary>
/// The act of speaking
/// </summary>
/// <param name="str">The content of the speech</param>
public void Speak(string str)
{
Console.WriteLine("{0}说{1}", name, str);
}
/// <summary>
/// Determine whether it is an adult
/// </summary>
/// <returns></returns>
public bool IsAdult()
{
return age >= 18;
}
/// <summary>
/// Add friend method
/// </summary>
/// <param name="p">Incoming new friends</param>
public void AddFriend(Person p)
{
if (friends == null)
{
friends = new Person[] { p };
}
else
{
//Expansion
Person[] newFriends = new Person [friends.Length + 1 ];
for (int i = 0; i < friends.Length; i++)
{
newFriends[i] = friends[i];
}
//Put the newly added friend to the last one
newFriends[newFriends.Length - 1] = p;
//address redirection
friends = newFriends;
}
}
public string name;
public int age;
//friends
public Person[] friends;
}
#endregion
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Member method");
Use of #region member methods
//2. The member method must instantiate the object and use it through the object is equivalent to the object performing a certain behavior
Person p = new Person();
p.name = "Yu Shuang";
p.age = 22;
p.Speak("I love you");
if ( p . IsAdult ())
{
p.Speak("I want to fall in love");
}
Person p2 = new Person();
p2.name = "Brother Volcano";
p2.age = 22;
p.AddFriend(p2);
for (int i = 0; i < p.friends.Length; i++)
{
Console.WriteLine(p.friends[i].name);
}
#endregion
}
}
//Summarize
//Member method
// describe the behavior
//declare in the class
//Any amount
//The return value and parameters are determined according to the requirements
}