实验二类及类的定义.docx

上传人:b****6 文档编号:6949406 上传时间:2023-01-13 格式:DOCX 页数:13 大小:143.03KB
下载 相关 举报
实验二类及类的定义.docx_第1页
第1页 / 共13页
实验二类及类的定义.docx_第2页
第2页 / 共13页
实验二类及类的定义.docx_第3页
第3页 / 共13页
实验二类及类的定义.docx_第4页
第4页 / 共13页
实验二类及类的定义.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

实验二类及类的定义.docx

《实验二类及类的定义.docx》由会员分享,可在线阅读,更多相关《实验二类及类的定义.docx(13页珍藏版)》请在冰豆网上搜索。

实验二类及类的定义.docx

实验二类及类的定义

实验报告

课程名称

面向对象程序设计

实验名称

实验二、类及类的定义

日期

2015.10.16

学生学号

姓名

班级

实验目的:

1.熟悉C#类、对象、方法的概念与用法;

2.掌握编写简单的面向对象的应用程序;

3.熟悉并掌握C#构造方法及其重载方法的用法;

4.熟悉并掌握类和成员的访问修饰符的用法。

实验要求:

1.认真阅读、掌握和本实验相关的教材内容。

2.设计并编写代码完成题目要求的任务。

3.撰写实验报告。

实验内容与步骤:

1.很多软件都要求我们先输入用户名和密码才能使用,最常见的例子当属Windows操作系统,制作一个类,完成系统的登录验证工作。

1)新建一个名为MySoftware的控制台应用程序

2)在解决方案资源管理器中选中该项目,点鼠标右键盘,在弹出的菜单中选择【添加】|【类】

3)在弹出的对话框中将类名改为Login

4)在代码classLogin前添加public关键字

5)添加Login类的成员变量

privatestring_userName;

privatestring_password;

6)添加Login类的属性

publicstringUserName

{

get

{

returnuserName;

}

set

{

userName=value;

}

}

publicstringPassword

{

get

{

returnpassword;

}

set

{

password=value;

}

}

7)添加Login类的方法

//方法

publicboolIsValidateUser()

{

if(userName.Equals("admin")&&password.Equals("admin"))

returntrue;

else

returnfalse;

}

8)完整代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceMySoftware

{

publicclassLogin

{

//私有字段

privatestringuserName;

privatestringpassword;

//属性

publicstringUserName

{

get

{

returnuserName;

}

set

{

userName=value;

}

}

publicstringPassword

{

get

{

returnpassword;

}

set

{

password=value;

}

}

//方法

publicboolIsValidateUser()

{

if(userName.Equals("admin")&&password.Equals("admin"))

returntrue;

else

returnfalse;

}

}

}

9)双击Program.cs,在Main方法中输入如下代码:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

namespaceMySoftware

{

classProgram

{

staticvoidMain(string[]args)

{

LoginloginModule=newLogin();

Console.WriteLine("####################################################");

Console.WriteLine("C#之星V1.0Beata版");

Console.WriteLine("####################################################");

Console.WriteLine("");

Console.WriteLine("");

Console.Write("请输入您的用户名:

");

stringuserName=Console.ReadLine();

Console.Write("请输入您的密码:

");

stringpassword=Console.ReadLine();

loginModule.UserName=userName;

loginModule.Password=password;

if(loginModule.IsValidateUser())

Console.WriteLine("欢迎使用C#之星软件V1.0版本");

else

Console.WriteLine("您的用户名输入错误,系统拒绝登录!

");

}

}

}

10)按Ctrl+F5运行程序,查看程序执行效果

2.扩展登录类,实现以下功能:

1)在命名空间中增加用户类型枚举,包括管理员、普通用户两种

2)在Login类中添加枚举类型对应的成员变量,并将该变量封装成属性

3)修改IsValidateUser()方法,对于不同的用户身份,进入不同的欢迎界面

4)用户拥有5次登录尝试机会,如果5次登录都失败,程序结束,否则,允许用户重新输入用户名和密码。

Login.cs代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceMySoftware

{

publicenumusertype

{

admin,guest

}

publicclassLogin

{

privatestringuserName;

privatestringpassword;

privateusertypeuser;

publicusertypeUser//属º?

性?

{

get

{

returnuser;

}

set

{

user=value;

}

}

publicstringUserName

{

get

{

returnuserName;

}

set

{

userName=value;

}

}

publicstringPassword

{

get

{

returnpassword;

}

set

{

password=value;

}

}

publicintIsValidateUser()

{

switch(user)

{

caseusertype.admin:

{

if(userName.Equals("admin")&&password.Equals("admin"))

return0;

else

return2;

}

caseusertype.guest:

{

if(userName.Equals("guest")&&password.Equals("guest"))

return1;

else

return2;

}

default:

{return2;};

}

}

}

}

 

Program.cs代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceMySoftware

{

classProgram

{

staticvoidMain(string[]args)

{

intnum=5;

while(num>0)

{

LoginloginModule=newLogin();

Console.WriteLine("####################################################");

Console.WriteLine("C#之星V1.0Beata版");

Console.WriteLine("####################################################");

Console.WriteLine("");

Console.WriteLine("");

Console.WriteLine("请选择用户类型(0-管理员1-普通用户):

");

usertypeusertype=(usertype)Enum.Parse(typeof(usertype),Console.ReadLine());

Console.Write("请输入您的用户名:

");

stringuserName=Console.ReadLine();

Console.Write("请输入您的密码:

");

stringpassword=Console.ReadLine();

loginModule.UserName=userName;

loginModule.Password=password;

loginModule.User=usertype;

intstate=loginModule.IsValidateUser();

Console.WriteLine("");

switch(state)

{

case0:

Console.WriteLine("尊敬的管理员");

break;

case1:

Console.WriteLine("尊敬的用户");

break;

case2:

Console.WriteLine("您的用户名或密码输入错误系统拒绝登录!

");

break;

}

num--;

if(state==2&&num>0)

{

Console.WriteLine("您还有{0}次机会输入登录信息",num);

}

else

{

Console.WriteLine("欢?

迎使用C#之星软件V1.0版本?

");

break;

}

}

Console.ReadKey();

}

}

}

 

测试结果:

1.身份为管理员,用户名和密码都正确,成功进入系统。

 

 

 

2.身份为管理员,用户名和密码都正确,成功进入管理员界面。

 

 

第一次没有成功输入,将会提示用户重新输入登录信息。

而且提示剩余可输入信息次数。

 

实验总结(结论或问题分析):

本次实验,主要是在命名空间中增加用户类型枚举,并且在Login类中添加枚举类型对应的成员变量,并将该变量封装成属性。

在完成这两步操作,并且将程序功能修改后发现在管理员模式下程序正确执行,但是在用户模式下,程序不能正确执行。

之后检查发现缺了一句loginModule.User=usertype;这样就没有将选择的用户模式赋给loginModule.User,就不能选择用户模式,它的值是一直0,所以只能是在管理员模式下工作。

 

实验成绩

任课教师签名

郭俊恩

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1