ASP.Net软件工程师基础(二)
1、封装
答:属性封装了字段,通过get和set访问器限制字段对外开放的程度;将重复的代码封装成方法,实现DCR原则(Don't Copy yourself);方法的参数组合可以用类实现,即在方法中不要传入超过3个以上的参数,否则定义一个类,直接将类对应的对象作为参数传入;将实现统一功能的类中所以的方法提取到接口或抽象类中(最明显的体现在三层中,DAL层抽象出一个只定义了增删查改功能的IDAL接口),至少也要写成虚方法,以实现面向对象编程(多态的实现)。
public interface IBaseDAL<T> where T : class,new()
{
T Add(T model);
bool Update(T model);
bool delete(int id);
T GetModel(int id);
List<T> GetList();
}
公用DAL层接口
public class BookDAL : IBaseDAL<Book>
{ public Book Add(Book model)
{
throw new NotImplementedException();
} public bool Update(Book model)
{
throw new NotImplementedException();
} public bool delete(int id)
{
throw new NotImplementedException();
} public Book GetModel(int id)
{
throw new NotImplementedException();
} public List<Book> GetList()
{
throw new NotImplementedException();
}
}
这里我就不写全了
具体类实现
2、继承
答:在C#中类是单继承的,而接口是可以多继承的(通常对于接口的继承我们称之为“实现接口”)
public class Student : Person,ICloneable,IEnumerable
{ public object Clone()
{
throw new NotImplementedException();
} public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
继承的实现
继承的好处:代码重用;多态实现的方式之一(涉及到一个软件开发原则:里氏替换原则),举个例子,在方法中,参数传父类对象,而在调用是,只要传入子类对象就可以了,前提他们是继承关系。
class Program
{
static void Main(string[] args)
{
Child c = new Child();
c.Name = "TQ";
c.Age = ;
Console.WriteLine(Say(c));
Console.ReadKey();
} public static string Say(Person p)
{
return p.Name;
} public class Person
{
private string _name; public string Name
{
get { return _name; }
set { _name = value; }
} public int Age
{
get;
set;
} public string this[int key]
{
get
{
string result = "";
switch (key)
{
case : result = "a"; break;
case : result = "b"; break;
}
return result;
}
}
} public class Child : Person
{ }
}
里氏替换
构造函数不能被继承,通过base.方法(),可以在子类中调用父类的方法。
class Program
{
static void Main(string[] args)
{
Child c = new Child();
c.Name = "TQ";
c.Age = ;
Console.WriteLine(Say(c));
Console.ReadKey();
} public static string Say(Person p)
{
return p.Name;
} public class Person
{
private string _name; public string Name
{
get { return _name; }
set { _name = value; }
} public int Age
{
get;
set;
} public void Speach()
{
} public string this[int key]
{
get
{
string result = "";
switch (key)
{
case : result = "a"; break;
case : result = "b"; break;
}
return result;
}
}
} public class Child : Person
{
public void Speach()
{
base.Speach();
}
}
}
base调用父类方法
类中的成员如果不写访问修饰符默认是private
访问修饰符:Public(接口成员默认)、Protected、Private(类成员默认private)、internal(类默认internal)
访问级别约束:子类的访问级别不能比父类的高;类中属性或字段的访问级别不能比类的访问级别高;方法的访问级别不能比方法的参数和返回值的访问级别高(如果将上例代码中Person类的访问修饰符改为Private,编译就会报错)。
ASP.Net软件工程师基础(二)的更多相关文章
- ASP.Net软件工程师基础(一)
本人目前是一名有1年左右ASP.Net开发经验的的软件开发工程师,目前公司用的是MVC+EF+...做的网站.写这套总结性系列文章的目的有两个:一是帮助自己总结一下自己到底有多少斤两,而不是一味的学新 ...
- ASP.Net软件工程师基础(四)
1.接口 (1)接口是一种规范.协议,定义了一组具有各种功能的方法(属性.索引器本质是方法). (2)接口存在的意义:多态.多态的意义:程序可扩展性. (3)接口解决了类的多继承的问题. (4)接口解 ...
- ASP.Net软件工程师基础(三)
1.多态 答: (1)虚方法 public class Child : Person { public void Speach() { base.Speach(); } public virtual ...
- QT软件工程师招聘市场需求报告
QT软件工程师招聘市场需求报告 目录 最流行的编程语言排行榜 QT软件工程师职位需求 QT软件工程师薪资待遇 QT软件工程师行业需求 QT软件工程师QT技术需求 QT软件工程师基础技术需求 QT软件工 ...
- GIS基础软件及操作(二)
原文 GIS基础软件及操作(二) 练习二.管理地理空间数据库 1.利用ArcCatalog 管理地理空间数据库 2.在ArcMap中编辑属性数据 第1步 启动 ArcCatalog 打开一个地理数据库 ...
- 软件工程师 Book
一.软件工程师 --Clean Code<代码整洁之道> --Implementation Patterns<实现模式> --Code Complete<代码大全& ...
- 第二节:Web前端-ASP.NET之C#基础
第二节:Web前端-ASP.NET之C#基础 学习ASP.NET,要掌握学习语言,控件等技能, <div style="text-align: center; line-height: ...
- JavaSE 软件工程师 认证考试试卷3
JavaSE 软件工程师 认证考试试卷 笔试 考试时间150分钟 总分 100分 姓 名_______________________ 身份证号___________________ ...
- Java 初级软件工程师 认证考试试卷1
Java 初级软件工程师 认证考试试卷 笔试(A卷) 考试时间150分钟 总分 100分 姓 名_______________________ 身份证号_____________ ...
随机推荐
- mysql log
mysql binlog3种格式,row,mixed,statement. 解析工作 mysqlbinlog --base64-output=DECODE-ROWS -v mysql-bin.0001 ...
- 从MySQL到Redis 提升数据迁移的效率
场景是从MySQL中将数据导入到Redis的Hash结构中.当然,最直接的做法就是遍历MySQL数据,一条一条写入到Redis中.这样可能没什么错,但是速度会非常慢.而如果能够使MySQL的查询输出数 ...
- 日常维护管理-DBA运维交接清单
序号 交接内容 交接目标与要点 交接物 交接状态 交接开始时间 交接结束时间 负责人 备注 1 人事关系 与开发项目组成员互识,并了解其职责 开发项目组成员清单 2016/2/29 2016/2/29 ...
- Python函数,参数,变量
func1.py def sayHello(): print ('hello world') sayHello() func_parm.py def printMax(a,b): if a>b: ...
- android studio提示unable to run mksdcard sdk
如题,android studio提示unable to run mksdcard sdk sudo apt-
- 黄聪:wordpress源码解析-数据库表结构(转)
如果是一个普通的用户,不需要了解wordpress数据库的结构.但是,如果你正在写一个插件,你应该会对wordpress如何处理它的数据和关系感兴趣.如果你已经尝试使用已经存在的wordpress a ...
- 我的wordpress插件总结
酷壳(CoolShell.cn)WordPress的插件 注意: 下面的这些插件的链接是其插件主页的链接,你可以在WordPress后台管理中添加插件时直接搜索安装就可以了. 插件不是越多越好.WP的 ...
- 3. c的输入输出
putchar与getchar操作输入输出通道 #include <stdio.h> #include <ctype.h> main(){ int c; while((c = ...
- ibatis CDATA
在使用ibatis时,经常需要配置待执行的sql语句.使用过ibatis的朋友都知道,无可避免的都会碰到一些不兼容.冲突的字符,多数人也都知道用<![CDATA[ ]]>标记避免Sql ...
- [CSS]当选择器没有指定元素时
当选择器没有指定元素时,样式会作用于(匹配)所有html元素. 如下面代码: <!DOCTYPE html> <html> <head> <style> ...