c#基础知识 构造方法

  <summary>
    /// 构造方法的主要作用是完成对象的初始化工作
    /// 构造方法的方法名与类名相同
    /// 构造方法没有返回类型,也不写void.
    ///
    /// 构造方法不能显示地直接调用,而是用new来调用
    /// person p = new person("tomme",20)
    ///
    /// </summary>
    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public string FistName { get; set; }
        public string LastName { get; set; }

        public int Salary { get; set; }
    }

方法的重载

public void sayhello(){
Console.WriteLine("hello word"+name);
}

public void sayhello(Person another){
Console.WriteLine("hello word"+anpther.name);
}

方法的签名:方法名及参数个数及类型构成(参数名不算)

this对象本身,常用于

(1)访问这个对象的字段与方法

(2)区分字段和局部变量

public Person(int age,string name){
this.age=age;
this,name=name;
}

(3)构造方法调用

写类:定义字段和方法

原文地址:https://www.cnblogs.com/zry2510/p/6196368.html