本套試題共8個題,主要考察C#面向對象基礎,SQL和ASP.NET MVC基礎知識。
第1-3題會使用到一個枚舉類,其定義如下:
1 public enum QuestionType 2 { 3 Text = 0, 4 MultipleChoice = 1 5 }
第1題:請定義一個接口IQuestion,有【標題】和【問題種類】兩個屬性,其中【問題種類】是只讀的枚舉類型QuestionType,另外還有一個方法獲取該問題的答案(無參,返回字符串)。
第2題:請定義一個抽象類QuestionBase,實現第一題中的IQuestion接口,其中【問題種類】屬性不在該抽象類中實現,而留在該抽象類的子類中實現;獲取答案的方法有默認實現,返回字符串“默認答案”。
第3題:請定義一個TextQuestion類,繼承自第2題中的QuestionBase;獲取答案的方法返回字符串”文本答案”。再定義一個MultipleChoiceQuestion類,繼承自第2題中的QuestionBase;獲取答案的方法返回字符串”單選答案”。
第4題:假設有實體類Product定義如下:
1 public class Product 2 { 3 public string Name { get; set; } 4 public bool IsDeleted { get; set; } 5 }
現在有一個方法從IQueryable<Product>中獲取沒有刪除的Product列表,該方法實現如下:
1 public List<Product> GetActiveProducts(IQueryable<Product> query) 2 { 3 return query.WhereNotDeleted().ToList(); 4 }
請完成擴展方法:WhereNotDeleted
第5題:假設數據庫中有User和Income兩張表如下,請仔細分析下方的示例數據,然后寫出SQL得到右方的查詢結果。
第6題:根據第5題的數據結構,有如下兩個實體類和查詢結果類的定義:
1 public class User 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 } 6 7 public class Income 8 { 9 public int Id { get; set; } 10 public int UserId { get; set; } 11 public decimal Amount { get; set; } 12 public int Year { get; set; } 13 public int Month { get; set; } 14 } 15 16 public class UserIncomeDto 17 { 18 public string Name { get; set; } 19 public int Year { get; set; } 20 public int Month { get; set; } 21 public decimal Income { get; set; } 22 }
現有一個方法用LINQ的方式得到第5題的查詢結果,該方法定義如下:
1 public List<UserIncomeDto> GetUserIncomeDtos(IQueryable<User> users, IQueryable<Income> incomes) 2 { 3 throw new NotImplementedException(); 4 }
請完成該方法的實現。
第7題:在ASP.NET MVC應用程序中,假設有如下HTML表單:
1 <form action="/admin/mobile/user/login"> 2 <input type="text" placeholder="username"/> 3 <input type="password" placeholder="password"/> 4 <input type="submit" value="login"/> 5 </form>
當該表單同步提交的時候,如何修改以上HTML和路由配置以使該請求進入下方的action中:
1 public class UserController : Controller 2 { 3 [HttpPost] 4 public ActionResult Login(string username, string password) 5 { 6 throw new NotImplementedException(); 7 } 8 }
第8題:請看如下代碼:
1 public class Product 2 { 3 public string Name { get; set; } 4 public string Description { get; set; } 5 6 public void Validate1() 7 { 8 if (string.IsNullOrEmpty(this.Name)) 9 { 10 throw new Exception("please enter a name for the product"); 11 } 12 if (string.IsNullOrEmpty(this.Description)) 13 { 14 throw new Exception("product description is required"); 15 } 16 } 17 18 public void Validate2() 19 { 20 this.Require(x => x.Name, "please enter a name for the product"); 21 this.Require(x => x.Description, "product description is required"); 22 } 23 }
請完成Validate2方法中Require方法的定義和實現,從而使得Validate2與Validate1方法實現同樣的效果。
第9題: 現有藍色底的紙板,可以自定義長寬(W*H),定義好之后不能修改。現在提供N種規格的矩形小紙板, A ,B ,C ,D ........ 實際上最多不超過10種規格,選擇其中的1種 或者 N種規格的紙板填充到底板上,如下圖所示:
為了計算出最佳的選擇方案:比如A 5個 C 1個 D 2個 使得底板的填充率最高,請提供具體的算法思路。
提示:比如下面這樣的輸入和輸出:
輸出:
文章列表