浏览器地址框输入域名并回车,通过DNS解析找到对应的IP地址,然后通过HTTP协议建立链接,找到目标服务器位置,接着就是TCP三次握手建立可靠链接,发送数据,服务器处理数据,TCP四次挥手断开链接,最后浏览器根据返回的数据解析渲染呈现页面。
SLOID原则
单一职责原则(SRP原则)
高内聚,低耦合
开放封闭原则(OCP原则)
封闭修改,开放拓展
里斯替换原则(LSP原则)
子类替换父类,实现多态
接口隔离原则(ISP原则)
使用专用小接口
依赖倒置原则(DIP原则)
高层调用接口,底层实现接口
对象变量赋值,判断,装箱
public override bool Equals(object obj){//重写父类equal方法,引用类型不是数据内容的比较,重写GetHashCode方法。
if(obj==null){
return false;
}
else{
if(obj is MyClass){
return this.value==(obj as MyClass).value;
}
}
return false;
}
继承
Parent p=new Child();
Console.WriteLine(p.value);//调用的父类的对象
Console.WriteLine((p as Child).value);
接口与回调
新写一个类实现ICallBack接口,然后将其加入到Controller对象的内部集合
static void Main(string[] args){
Controller controller=new controller();
controller.AddCallBack(new CallBackClass());//CallBackClass实现了ICallBack接口
controller.Begin();
}
class CallBackClass:ICallBack{
Console.WriteLine(DateTime.Now.ToString());
}
多态
继承多态基于抽象类和抽象方法
abstract class Fruit{
public abstract void GrowInArea();
}
FeedAnimals(IEnumerable ans)
对象注入的构造方式
class B{
}
class A{
private B innerobj;
public A(B obj){
innerobj=obj;
}
}
基于接口的对象注入
interface MyInterface{
}
calss B:MyInterface{
}
class A{
private MyInterface innerObj;
public A(MyInterface obj){
innerobj=obj;
}
}
A obj=new A(new B());
1、泛型
属性
private int age;
public int Age{
get{return age;}
set{
if(age
age=18;
else
age=value;
}
}
相较于传值为object的方法
public static SayHi(T tparameter) where T:people //类型约束
{Console.WriteLine(tparameter.SayHi());
}
public static SayHiObject(object tparameter)
{Console.WriteLine(tparameter);
}
泛型约束
类型后添加
where T:class ,new() //默认是个类,有构造函数
where T:class //引用类型 默认null
where T:struct //值类型
new() //含有无参构造函数
//具体类型,或该类型的子类
Set() 调用T对应的数据实体
1.1序列化
[Serializable]//使类支持序列化
class CollegeStudent{
public string Name="空";
public bool IsMale=true;
}
private void SeriallizeObj(String FileName,CollegeStudent stu){
//创建FileStream对象
using(FileStream writer=new FileStream(FileName,FileMode.Create)){
//创建格式化器对象
IFormatter formatter=new BinaryFormatter();
//格式化器对对象使用FileStream对象序列化对象
formatter.Serialize(writer,stu);
MessageBox.Show("对象成功保存到文件:"+FileName);
}
}
//大批量的复制对象反序列化
MyClass obj=new MyClass();
//创建内存流对象
using(MemoryStream ms=new MemoryStream){
IFormatter formator=new BinaryFormatter();
//对象序列化到内存流
formator.Serialize(ms,obj);
//克隆100个对象
for(int i=0;i
//回到流开头
ms.Seek(0,SeekOrigin.Begin);
//反序列化对象
obj=(formator.Deserialize(ms) as MyClass);
obj.Index+=i;
Console.WriteLine("对象{0}已处理",obj.Index);
}
}
2、反射
便于代码配置
strig IHelperConfig=ConfigurationManager.AppSettings["IHelperConfig"]
Assembly assembly=Assembly.load(IHelperConfig.split(',',0));//动态加载dll
Module[] modules=assembly.GetModules();//取dll所有命名空间
Type[] types=assembly.GetTypes()//取dll所有类
Type typeHelper=assembly.GetType(IHelperConfig)//根据命名空间.类名获取类名
object oHelper=Activator.CreateInstance(typeHelper,false)//根据类名获取对象实例,后面参数为是否调用private构造函数
IHelper iHelper=(IHelper)oHelper;//强转类型为继承的接口,以便调用接口方法
iHelper.Query();
public class SalserverHelper:IHelper{
public void Query(){
Console.WriteLine("1");
}
}
public interface IHelper{ //接口
void Query();
}
//获取方法
foreach(MethodInfo method in typeHelper.GetMethods()){
Console.WriteLine(method.Name);
}
MethodInfo Query=type.GetMethod("Query",new Type[]{typeof(int)});//根据具体方法名称
Query.Invoke(iHelper,null);
//调用私有方法
Methodinfo show1=type.GetMethod("Show1",BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);//构造,公开,非公开
//属性赋值
foreach(var prop in type.GetProperties()){
if(prop.Name.Equals("ID")){
prop.SetValue(iHelper,12); //赋值
}
Console.WriteLine(prop.GetValue(iHelper)); //取值
}
例)
实体类
Public class user:BaseModel{
public string Name{set;get;};
public string ID{set;get;}
}
Public class DBHelper:IDBHelper{
Private static string ConnectionStringCustomers=ConfigurationManager.ConnectionStrings["Customs"].ConnectionString;
Public DBHelper(){
//构造函数
}
public T QueryDomain(){
Type type=typeof(T);
string colunms=string.Join(",",type.GetProperties().Select(p=>string.Format("[{0}]",p.Name)));
string sql=string.Format("select {0} from {1} where id={2}",colunms,type.Name,id);
using(SqlConnection conn=new SqlConnection(ConnectionStringCustomers)){
SqlCommand command=new SqlCommand(sql,conn)
conn.open();
SqlDataAdapter reader=command.ExcuteReader(CommandBehavior.CloseConnection)//自动关闭
if(reader.Read()){
foreach(var prop in type.GetProperties()){
string propertyName=prop.Name;
prop.SetValue(t,reader["propertyName"]);
}
}
}
}
}
2.1、抽象类,接口
抽象类 //类只能被继承一个
public abstract class BasePhone{//描述是什么
Public int Price{get;set;}
public void call(){}
public abstract void system(){}//由于存在未实现的方法,不能被实例化
}
接口 //接口能被实现多个
public interface IPhoneExtend{ //描述做什么
void Pay();
}
3、委托
委托与类同级
private delegate int MyDelegate(string id);声明委托
MyDelegate myDelegate=new MyDelegate (SayHi);//委托实例化
myDelegate+=SayHi;
myDelegate-=SayHi;//方法执行,但是只返回最后一次执行的值
myDelegate()或者myDelegate.Invoke//委托实例调用
委托逻辑分离,去耦合,便于拓展
pravite delegate string MyDelegate(string id);
public string SayHi(string a){
if(a=="1"){
return “逻辑1”
}else
return “逻辑2”
}
public string DelegateSayHi(string method,Delegate myDelegate){
myDelegate(method);
}
public string SayHi1(){
return “逻辑1”;
}
public string SayHi2(){
return “逻辑2”;
}
总结:代码增多,但去掉方法间关联,便于拓展
4、事件
public event EventHander Click;//事件为一种特殊委托
Click事件的响应方法必须符合EventHander委托的要求:
public delegate void EventHander(Object sender,EventArgs e)
form窗体中实现切换text焦点
foreach(Control ctl in groupBox1.Controls){
if(ctl is TextBox){
(ctl as TextBox).KeyDown+=this.EnterToTab;//委托挂接事件
}
}
private void EnterToTab(Object sender,EventArgs e){
TextBox txt=null;
if(e.KeyCode==Keys.Enter){
groupBox1.SelectNextControl(sender as Control,true, true, true, true);
txt=(sender as TextBox);
if(txt!=null){
txt.SelectAll();
}
}
}
protected override void onClick(EventArgs e){
base.OnClick(e);
ClickCount++;
//激发自定义事件
if(MyClick!=null){
MyClick(this,new MyClickEventArgs(ClickCount));
}
}
组件化开发
1、重用已有组件
2、开发部分新组件
3、组合新旧组件搭建新系统
代码风格
毛病
1、重复代码(同样的代码重复出现,不同代码完成同一功能)
2、过长函数
3、过大的类(明确各类的职责,一个类拆分多个独立的类,采用继承,公有代码移到基类)
4、过多的参数(改为传送对象)
5、原子弹的链式反应(类之间耦合性太强,类实现的功能太多,降低底层数据库变化对上层的影响)
6、曝光隐私(避免暴露内部数据结构,内部具体算法,仅公开必要职责)
7、乱起名字(命名要易于理解,劲量用英语,名字可以很长)
糟糕的设计
1、代码写死文件读取路径
2、UI控件设定底层数据源字段名
3、数据表新增字段,导致N处要修改
脆弱的设计
无法重用的设置
过于复杂的设计
DRY原则-老路不走两遍
KISS原则-绝不画蛇添足
KISS原则-简单即美
DDIY原则-不要一切自己动手
private void Init(){
InitUIControl();
setEventHandler();
}
private void InitUIControl(){//统一控件初始化
txtNum.Text="o";
}
private void setEventHandler(){//统一设置事件响应代码
EventHandler calculateEventHandler=()=>{Calculate();}
txtNum.TextChanged+=calculateEventHandler;
}
public void Calculate(){//公用算法放公共类
...
}
重构改进软件内部结构
软件测试(黑盒&&白盒)
单元测试,功能测试,集成测试,场景测试,系统测试,压力测试
基于职责设计类
命令方法和查询方法
5、Lambda表达式//匿名方法
public delegate void NoReturn(int x,int y);//无法返回值委托
public delegate int WithReturn(int x);//有返回值委托
public static void Show(){
1)无返回值委托
NoReturn method1=new NoReturn(
delegate(int x,int y)//匿名方法{
console.writeLine("delegate");
}
)
NoReturn method2=new NoReturn(
(int x,int y)=>{
console.writeLine("delegate");
}
)
NoReturn method3=new NoReturn(
( x, y)=>{//委托严格现在了取值类型
console.writeLine("delegate");
}
)
NoReturn method4=new NoReturn(
( x, y)&