7.方法的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//如果要弹窗。加命名空间
using System.Windows.Forms;//记得在引用处引用System.Windows.Forms

namespace _6方法的定义和使用
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
public DateTime Birthday { get; set; }
public string PhoneNumber { get; set; }

    /// <summary>
    /// 获取学员信息。有返回值没有参数的方法
    /// </summary>
    /// <returns></returns>
    public string GetStudent()
    {
        string info = string.Format("姓名是:{0},学号为:{1}。", StudentName, StudentId);
        return info;
    }

    /// <summary>
    /// 获取学员的信息。有返回值,有参数的方法。
    /// </summary>
    /// <param name="stuName"></param>
    /// <param name="stuId"></param>
    /// <returns></returns>
    public string GetStudent(string stuName, int stuId)
    {
        this.StudentName = stuName;
        this.StudentId = stuId;
        string info = string.Format("姓名是:{0},学号为:{1}。", StudentName, StudentId);
        return info;

    }

    /// <summary>
    /// 没有返回值没有参数的方法
    /// </summary>
    public static void GetStudent1()//这个方法不能设为和上面两个同名的GetStudent方法
    {
        //  string info = string.Format("姓名为:{0},学号为:{1}。", StudentName, StudentId);//注意这行会报错
        string info = string.Format("学员类尽量不要用静态方法。要new对象好些。");
        Console.WriteLine(info);
        MessageBox.Show(info);
    }

}

//调用方法
class Program
{
    static void Main(string[] args)
    {
        Student objStudent = new Student();
        string info = objStudent.GetStudent("小梁", 1000);


        //静态方法不要New
        Student.GetStudent1();
        Console.Read();
    }
}

}

posted on 2020-12-29 18:03  cq752522131  阅读(64)  评论(0)    收藏  举报