标签:style blog http io ar os 使用 sp strong
又到了总结知识的时间了,今天在云和学院学习了析构函数,命名空间及字符串的处理,现在就为大家总结下来。
理论:
析构函数
class Student
{//给前面写的学生类添加构造方法.使在实例化学生类的时候可以通过构造方法对姓名性别年龄语数英等属性赋值,也可以只对姓名和性别赋值.年龄默认为18岁,语数英成绩默认为0分.
//张三 男 18 三科成绩为:90 95 80
//小兰 女 16 三科成绩为:95 85 100
string name;
string gender;
int age=18;
int chinese;
int math;
int english;
public Student(string n,int a,string g,int c,int m,int e )
{
name = n;
age = a;
gender = g;
chinese = c;
math = m;
english = e;
}
public Student(string n, string g, int c, int m, int e)
{
name = n;
gender = g;
chinese = c;
math = m;
english = e;
}
public void Stt()
{
Console.WriteLine("大家好,我叫{0},是{1}同学,今年{2}岁,我的三科成绩为:{3},{4},{5}", name, gender, age, chinese, math, english);
}
}
在Main函数里写
static void Main(string[] args)
{
Student stu1 = new Student("张三",18,"男",90,95,80);
Student stu2 = new Student("小兰",16,"女 ",95, 85,100);
Student stu3 = new Student("小兰", "女 ", 95, 85, 100);
stu1.Stt();
stu2.Stt();
stu3.Stt();
Console.ReadKey();
}
运行结果是:

有一个方法,可以显示这张票的信息.90公里90块钱
class Ticket
{
#region 构造函数
public Ticket(int distance)
{
if(distance<0)
{
distance = 0;
}
this.distance = distance;
}
#endregion
int distance;
public int Distance
{
get {
return distance;
}
}
double price=500;
public double Price
{
get {
if(distance<=100 && distance>=0)
{
return distance * 1.0;
}
else if(distance>=101 && distance<=200)
{
return distance * 0.95;
}
else if(distance>=201 && distance<=300)
{
return distance * 0.9;
}
else
{
return distance*0.8;
}
}
}
public void Show()
{
Console.WriteLine("{0}公里需要{1}钱",distance,price);
}
}
static void Main(string[] args)
{
Ticket t1 = new Ticket(110);
Console.WriteLine(t1.Price);
Console.ReadKey();
}
测试结果:这道题不知道哪里出现问题了,结果并没有运用到Show方法


随机输入你心中想到的一个名字,然后输出它的字符串长度 Length:可以得字符串的长度

两个学员输入各自最喜欢的课程名称, 判断是否一致,如果相等,则输出你们俩喜欢相同的课程. 如果不相同,则输出你们俩喜欢不相同的课程.

让用户输入一个日期格式如:2008-01-02,你输出你输入的日期为2008年1月2日
static void Main(string[] args)
{
Console.WriteLine("请输入日期");
string date = Console.ReadLine();
string[] strs = date.Split(‘-‘);
string datetine = strs[0] + "年" + strs[1] + "月"+strs[2]+"日" ;
Console.WriteLine(datetine);
Console.ReadKey();
}

今天就总结到这里吧,下周一在继续学习吧。加油!
C#中析构函数,命名空间及字符串的运用(Ninth day)
标签:style blog http io ar os 使用 sp strong
原文地址:http://www.cnblogs.com/ysaw/p/4098159.html