实例化:
【方法一】常规方法(一一赋值)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Student Addy = new Student();
Addy.SNO = "10001";
Addy.SName = "Addy";
Addy.Gender = "女";
Addy.Birthday = Convert.ToDateTime("2001-2-18");
Addy.MObile = "19254622331";
Addy.Email = "PU@163.com";
Addy.HomeAddress = "翻斗大街翻斗花园二号楼1001室。";
Addy.SayHello();
Console.ReadKey();
}
}
class Student
{
//属性
//学号
public string SNO { get; set; }
//姓名
public string SName { get; set; }
//性别
public string Gender{ get; set; }
//出生日期
public DateTime Birthday { get; set; }
//手机号码
public string MObile { get; set; }
//邮箱地址
public string Email { get; set; }
//家庭住址
public string HomeAddress { get; set; }
//方法
public void SayHello()
{
Console.WriteLine("大家好,我的学号是:{0}叫做{1},是一个{2}生,于{3}出生,手机号为{4},邮箱地址为{5},家住{6}",
SNO, SName, Gender, Birthday, MObile, Email, HomeAddress);
}
}
}
【方法二】对象初始化器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Student Steven = new Student()
{
SNO = "10001",
SName = "Steven ",
Gender = "女",
Birthday = Convert.ToDateTime("2001-2-18"),
MObile = "19254622331",
Email = "LixUn@163.com",
HomeAddress = "翻斗大街翻斗花园二号楼1001室",
};
Steven.SayHello();
Console.ReadKey();
}
}
class Student
{
//属性
//学号
public string SNO { get; set; }
//姓名
public string SName { get; set; }
//性别
public string Gender{ get; set; }
//出生日期
public DateTime Birthday { get; set; }
//手机号码
public string MObile { get; set; }
//邮箱地址
public string Email { get; set; }
//家庭住址
public string HomeAddress { get; set; }
//方法
public void SayHello()
{
Console.WriteLine("大家好,我的学号是:{0}叫做{1},是一个{2}生,于{3}出生,手机号为{4},邮箱地址为{5},家住{6}",
SNO, SName, Gender, Birthday, MObile, Email, HomeAddress);
}
}
}
【方法三】通过构建方法来进行初始化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Student Steven = new Student("99","FUPU","男",Convert.ToDateTime("2021-05-15"),"18965428945","FUPU@168.com","翻斗大街翻斗花园2号楼1001室");
Steven.SayHello();
Console.ReadKey();
}
}
class Student
{
//属性
//学号
public string SNO { get; set; }
//姓名
public string SName { get; set; }
//性别
public string Gender{ get; set; }
//出生日期
public DateTime Birthday { get; set; }
//手机号码
public string MObile { get; set; }
//邮箱地址
public string Email { get; set; }
//家庭住址
public string HomeAddress { get; set; }
//方法
public void SayHello()
{
Console.WriteLine("大家好,我的学号是:{0}叫做{1},是一个{2}生,于{3}出生,手机号为{4},邮箱地址为{5},家住{6}",
SNO, SName, Gender, Birthday, MObile, Email, HomeAddress);
}
//构建方法
public Student(string sno, string sname, string gender,DateTime birthday,string mobile,string email,string homeaddress)
{
SNO = sno;
SName = sname;
Gender = gender;
Birthday = birthday;
MObile = mobile;
Email = email;
HomeAddress = homeaddress;
}
}
}
构建方法:在实例化类的时候,用来给类的成员初始化
关于构造方法: 是一种特殊的方法 主要用来在创建对象时初始化对象 即为对象成员变量赋值初始值,总与new运算符一起使用在创建对象的语句中。 构造函数的命名必须和类名完全相同 构造函数的功能主要用于在类的对象创建时定义初始化的状态。它没有返回值,也不能用void来修饰。这就保证了它不仅什么也不用自动返回,而且根本不能有任何选择 一个类可以有多个构造函数 可根据其参数个数的不同或参数类型的不同来区分它们 即构造函数的重载 构造函数不能被直接调用,必须通过new运算符在创建对象时才会自动调用
如果在默认的类中,没有构造方法,系统会自动添加一个没有参数的构造方法 如果用户手工添加了构造方法,系统默认的无参数构造方法会自动删除
访问修饰符 方法名称(参数列表)
{
方法具体代码
}
public Person()
{
}