首页 > 代码库 > 练习题
练习题
public enum QuestionType { Text=0, MultipleChoice=1 }
第1题:请定义一个接口IQuestion,有【标题】和【问题种类】两个属性,其中【问题种类】是只读的枚举类型QuestionType,另外还有一个方法获取该问题的答案(无参,返回字符串)。
interface IQuestion { string QuestionTitle { get; set; } QuestionType QuestionType { get; set; } string GetAnswer(); }
第2题:请定义一个抽象类QuestionBase,实现第一题中的IQuestion接口,其中【问题种类】属性不在该抽象类中实现,而留在该抽象类的子类中实现;获取答案的方法有默认实现,返回字符串“默认答案”。
abstract class QuestionBase : IQuestion { string IQuestion.QuestionTitle { get; set; } public abstract QuestionType QuestionType { get; set; } public virtual string GetAnswer() { return "Default answer"; } }
子类
class OneQuestion : QuestionBase { public QuestionType QuestionOneType; public override QuestionType QuestionType { get { return QuestionOneType; } set { QuestionOneType = value; } } }
第3题:请定义一个TextQuestion类,继承自第2题中的QuestionBase;获取答案的方法返回字符串”文本答案”。再定义一个MultipleChoiceQuestion类,继承自第2题中的QuestionBase;获取答案的方法返回字符串”单选答案”。
class TextQuestion : QuestionBase { public QuestionType QuestionOneType; public override QuestionType QuestionType { get { return QuestionOneType; } set { QuestionOneType = value; } } public override string GetAnswer() { return "Text Answer"; } } class MultipleChoiceQuestion : QuestionBase { public QuestionType QuestionOneType; public override QuestionType QuestionType { get { return QuestionOneType; } set { QuestionOneType = value; } } public override string GetAnswer() { return "Sigle choice Answer"; } }
第4题:假设有实体类Product定义如下:
public class Product { public string Name { get; set; } public string IsDeleted { get; set; } }
现在有一个方法从IQueryable<Product>中获取没有删除的Product列表,该方法实现如下:
public List<Product> GetActiveProducts(IQueryable<Product> query) { return query.WhereNotDeleted().ToList(); }
请完成扩展方法:WhereNotDeleted
static class Extener { public static IQueryable<Product> WhereNotDeleted(this IQueryable<Product> query) { query = query.Where(t => t.IsDeleted == "NO"); return query; } }
第5题:假设数据库中有User和Income两张表如下,请仔细分析下方的示例数据,然后写出SQL得到右方的查询结果。
SELECTb.`Name`,a.`Year`,a.`Month`,SUM(a.Amount) as "Income"FROMincome a LEFT JOIN`user` bon a.UserId=b.IdGROUP BYa.`Month`,b.`Name`
第6题:根据第5题的数据结构,有如下两个实体类和查询结果类的定义:
public class User{ public int Id { get; set; } public string Name { get; set; }}public class Income{ public int Id { get; set; } public int UserId { get; set; } public decimal Amount { get; set; } public int Year { get; set; } public int Month { get; set; }}public class UserIncomeDto{ public string Name { get; set; } public int Year { get; set; } public int Month { get; set; } public decimal Income { get; set; }}
现有一个方法用LINQ的方式得到第5题的查询结果,该方法定义如下:
public List<UserIncomeDto> GetUserIncomeDtos(IQueryable<User> users, IQueryable<Income> incomes) { throw new NotImplementedException(); }
请完成该方法的实现。
待定。。。。。。。。。
第7题:在ASP.NET MVC应用程序中,假设有如下HTML表单:
<form action="/admin/mobile/user/login"> <input type="text" placeholder="username"/> <input type="password" placeholder="password"/> <input type="submit" value="login"/></form>
当该表单同步提交的时候,如何修改以上HTML和路由配置以使该请求进入下方的action中:
public class UserController : Controller{ [HttpPost] public ActionResult Login(string username, string password) { throw new NotImplementedException(); }}
待定。。。。
第8题:请看如下代码:
public class Product{ public string Name { get; set; } public string Description { get; set; } public void Validate1() { if (string.IsNullOrEmpty(this.Name)) { throw new Exception("please enter a name for the product"); } if (string.IsNullOrEmpty(this.Description)) { throw new Exception("product description is required"); } } public void Validate2() { this.Require(x => x.Name, "please enter a name for the product"); this.Require(x => x.Description, "product description is required"); }}
请完成Validate2方法中Require方法的定义和实现,从而使得Validate2与Validate1方法实现同样的效果。
class Products { public string Name { get; set; } public string Description { get; set; } public void Validate1() { if (string.IsNullOrEmpty(this.Name)) { throw new Exception("please enter a name for the product"); } if (string.IsNullOrEmpty(this.Description)) { throw new Exception("product description is required"); } } public void Validate2() { this.Require(x => x.Name, "please enter a name for the product"); this.Require(x => x.Description, "product description is required"); } public void Require(Func<Products,string> func, string s2) {
if (string.IsNullOrEmpty(func(this))) { throw new Exception(s2); } } }
原题的地址。。。http://www.cnblogs.com/leotsai/p/aspnet-tests-for-juniors.html#!comments
练习题