1、构造方法
初始化对象太繁琐了,那就使用构造函数:
1)构造函数没有返回值,连void也不能够写;
2)构造函数名必须与类名相同;
3)创建对象的时候会执行构造函数;
4)类中会存在默认的构造函数,它是无参的;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
public class Student
{
//构造函数
public Student(string name,char gender,int age,int math,int chinese,int english)
{
this.NAME = name;
this.Gender = gender;
this.Age = age;
this.Math = math;
this.Chinese = chinese;
this.English = english;
}
//名字
private string _name;
public string NAME
{
get { return _name; }
set { _name = value; }
}
//年龄
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 120)
{
value = 0;
}
_age = value;
}
}
//性别
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _gender = value; }
}
//语文
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
//数学
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
//英语
private int _english;
public int English
{
get { return _english; }
set { _english = value; }
}
//方法
public void PrintInfo()
{
Console.WriteLine("我叫{0},今年{1}岁,是一名{2}生!", this.NAME, this.Age, this.Gender);
}
public void PrintScore()
{
Console.WriteLine("我的英语成绩:{0},语文成绩:{1},数学成绩{2},总分:{3},平均分:{4}",this.English,this.Chinese,this.Math,
this.English+this.Chinese+this.Math,(this.English+this.Chinese+this.Math)*1.0/3);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
//创建对象
Person Tim = new Person();
Student student1 = new Student("王强", '男', 23, 90, 90, 90);
Tim.Name = "Tim";
Tim.Gender = '女';
Tim.Age = 5;
Tim.PrintInformation();
student1.PrintInfo();
student1.PrintScore();
Console.ReadKey();
}
}
}
2、new关键字的作用
1)在内存中开辟一块空间
2)在开辟的空间中创建对象
3)调用对象的构造函数初始化对象
3、构造函数的重载
不想要对每个属性赋值时,用到函数的重载:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
public class Student
{
//构造函数
public Student(string name,char gender,int age,int math,int chinese,int english)
{
this.NAME = name;
this.Gender = gender;
this.Age = age;
this.Math = math;
this.Chinese = chinese;
this.English = english;
}
//构造函数重载
public Student(string name, char gender, int age)
{
this.NAME = name;
this.Gender = gender;
this.Age = age;
}
//名字
private string _name;
public string NAME
{
get { return _name; }
set { _name = value; }
}
//年龄
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 120)
{
value = 0;
}
_age = value;
}
}
//性别
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _gender = value; }
}
//语文
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
//数学
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
//英语
private int _english;
public int English
{
get { return _english; }
set { _english = value; }
}
//方法
public void PrintInfo()
{
Console.WriteLine("我叫{0},今年{1}岁,是一名{2}生!", this.NAME, this.Age, this.Gender);
}
public void PrintScore()
{
Console.WriteLine("我的英语成绩:{0},语文成绩:{1},数学成绩{2},总分:{3},平均分:{4}",this.English,this.Chinese,this.Math,
this.English+this.Chinese+this.Math,(this.English+this.Chinese+this.Math)*1.0/3);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
//创建对象
Person Tim = new Person();
Student student1 = new Student("王强", '男', 23, 90, 90, 90);
Student student2 = new Student("包蕾", '女', 22);
Tim.Name = "Tim";
Tim.Gender = '女';
Tim.Age = 5;
Tim.PrintInformation();
student1.PrintInfo();
student1.PrintScore();
student2.PrintInfo();
Console.ReadKey();
}
}
}
4、this关键字
不想在重载函数的属性赋值里重新敲写代码,this代表当前类的对象,在类当中显示了当前类的构造函数,使用方法:this。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
public class Student
{
//构造函数
public Student(string name,char gender,int age,int math,int chinese,int english)
{
this.NAME = name;
this.Gender = gender;
this.Age = age;
this.Math = math;
this.Chinese = chinese;
this.English = english;
}
//构造函数重载
public Student(string name, char gender, int age):this(name,gender,age,0,0,0)
{
//this.NAME = name;
//this.Gender = gender;
//this.Age = age;
}
//构造函数重载
public Student(string name, int math,int chinese,int english):this(name,'男',0,math,chinese,english)
{
//this.NAME = name;
//this.Math = math;
//this.Chinese = chinese;
//this.English = english;
}
//名字
private string _name;
public string NAME
{
get { return _name; }
set { _name = value; }
}
//年龄
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 120)
{
value = 0;
}
_age = value;
}
}
//性别
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _gender = value; }
}
//语文
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
//数学
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
//英语
private int _english;
public int English
{
get { return _english; }
set { _english = value; }
}
//方法
public void PrintInfo()
{
Console.WriteLine("我叫{0},今年{1}岁,是一名{2}生!", this.NAME, this.Age, this.Gender);
}
public void PrintScore()
{
Console.WriteLine("我的英语成绩:{0},语文成绩:{1},数学成绩{2},总分:{3},平均分:{4}",this.English,this.Chinese,this.Math,
this.English+this.Chinese+this.Math,(this.English+this.Chinese+this.Math)*1.0/3);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChuangzhiConsel
{
class Program
{
static void Main(string[] args)
{
//创建对象
Person Tim = new Person();
Student student1 = new Student("王强", '男', 23, 90, 90, 90);
Student student2 = new Student("包蕾", '女', 22);
Student student3 = new Student("小明", 100, 100, 100);
Tim.Name = "Tim";
Tim.Gender = '女';
Tim.Age = 5;
Tim.PrintInformation();
student1.PrintInfo();
student1.PrintScore();
student2.PrintInfo();
student2.PrintScore();
student3.PrintInfo();
student3.PrintScore();
Console.ReadKey();
}
}
}
5、析构函数
程序结束时才会被执行,帮助我们马上释放资源,格式:
//析构函数
~Student()//类名
{
}
6、示例Ticket实现构造
//类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ticket
{
class Ticket
{
//通过构造函数,赋值
public Ticket(double distance)
{
distance= distance < 0 ? 0 : distance;
this._distance = distance;
}
//字段
private double _distance;
public double Distance
{
get { return _distance; }
}
private double _price;
public double Price
{
get
{
if (this._distance <= 100)
{
this._price = this._distance * 2.0;
}
else if (this._distance > 100 && this._distance <= 200)
{
this._price = this._distance * 2.0 * 0.95;
}
else if (this._distance > 200 && this._distance <= 300)
{
this._price = this._distance * 2.0 * 0.9;
}
else
{
this._price = this._distance * 2.0 * 0.8;
}
return _price;
}
}
//方法
public void ShowTicket()
{
Console.WriteLine("你行驶了{0}公里,票价是{1}", this.Distance, this.Price);
}
}
}
//主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ticket
{
class Program
{
static void Main(string[] args)
{
string input;
do
{
Console.WriteLine("请输入行驶距离:\n>>>");
input = Console.ReadLine();
if (input != "q")
{
Ticket myTiket = new Ticket(ChangetoDouble(input));
myTiket.ShowTicket();
}
} while (input != "q");
}
public static double ChangetoDouble(string s)
{
while (true)
{
try
{
double data = Convert.ToDouble(s);
return data;
}
catch
{
Console.WriteLine("输入的不是数字,请重新输入!!!");
s = Console.ReadLine();
}
}
}
}
}