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`,b.id
第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.49028c.com/leotsai/p/aspnet-tests-for-juniors.html#!comments
新聞熱點
疑難解答