资源描述
,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,Chapter2 : C# Language | Object-Oriented C#,*,Objected-Oriented C#,MaoQiang,Xie,College of Software, Nankai University,Outline,Inheritance,Polymorphism,Abstract class and Sealed Class,Operators Overloading,Boxing and,Unboxing,Struct,Interface,2,Chapter2 : C# Language | Object-Oriented C#,1- Features of OO,Class, Instance, Object,Encapsulate, Inheritance, Polymorphism,Key problem:,如何让高效开发更具规范性,使其在大规模软件开发上发挥应有作用,OO,不是唯一方法,,IBM IFW,,,E-R,。在解决大规模问题的时候,从对象角度出发可降低系统间各部分的耦合。,3,Chapter2 : C# Language | Object-Oriented C#,1- Encapsulate,完美是不是没什么好添加的,而实在没什么好去处的时候。用,OO,描述事务的顶峰就是机器把自己完全隐藏了。,Windows,消息机制、不用洗衣粉的洗衣机,封装更多是一种思想,语言或工具只能提供给你帮助,MSDN-POP3,换一个词语“抽象”,可将思想或对象的不必要的部分去除,只保留精华的最小部分,最小相关性准则,4,Chapter2 : C# Language | Object-Oriented C#,1- Experiment,仿照,MFC,为按钮、对话框、滚动条构建类,为上述窗口提供绘制,UpdateWindows,和位姿功能,ResizeWindow,。,目的:,1.,了解你们的封装能力,2.,继承起了哪些作用?,5,Chapter2 : C# Language | Object-Oriented C#,2- Sample of Polymorphism,XWindow,XScrollBar,XButton,XForm,MyForm,6,Chapter2 : C# Language | Object-Oriented C#,1- Experiment,class,Tester,static,void,Main(),MyForm,form1 =,new,MyForm(,new,CRect(0, 0, 0, 0),Demo of my window foundation class library);,form1.InitDialog();,Console.WriteLine(-0-, form1);,form1.SetWindowPos(,new,CRect(0, 0, 400, 400);,form1.Redraw();,Console.WriteLine(-0-, form1);,form1.SetWindowPos(,new,CRect(0, 0, 350, 370);,form1.Redraw();,Console.ReadLine,();,7,Chapter2 : C# Language | Object-Oriented C#,1-,Inheritance,The other means to encapsulate.,Inheritance might be thought as vertical encapsulation, if using the other class members by instantiating the class is looked as horizon.,Where should inheritance be used?,8,Chapter2 : C# Language | Object-Oriented C#,1- Comparison,1/2,In C+,class,CBase,private:,int,m_iMem,;,protected:,int,m_iProtected,;,public:,int,m_iPublic,;,public,class,CChild,: protected,CBase,private,9,Chapter2 : C# Language | Object-Oriented C#,1-,Comparison,2/2,In C#,public/internal class,CBase,private,int,m_iMem,;,protected,int,m_iProtected,;,public,int,m_iPublic,;,public class,CChild,:,CBase,10,Chapter2 : C# Language | Object-Oriented C#,1-,Call,ctor,. of base class implicitly,public,URL(,string,name),:,base,(name),A derived class implicitly contains all members of its direct base class, except for the instance constructors, static constructors, and destructors of the base class,Location,URL,11,Chapter2 : C# Language | Object-Oriented C#,1- Inheritance,Classes support single inheritance, and the type,object,is the ultimate base class for all classes.,The classes shown in earlier examples all implicitly derive from,object,.,12,Chapter2 : C# Language | Object-Oriented C#,1- Inheritance(,Conversion,),1/2,Upcast,Location l = u;,Downcast,URL u = (URL)l;,Exception,System.InvalidCastException,13,Chapter2 : C# Language | Object-Oriented C#,1- Inheritance(,Conversion,),2/2,Operator “,as,”,u = l as URL;,Operator “,is,”,if (l is URL),(URL)l).Navigate();,Downcast,If fail to convert, return null,Is the instance of specialized class,derived from a specialized class,or implement a interface,Test before,downcasting,14,Chapter2 : C# Language | Object-Oriented C#,1-,New a method,public class URL : Location,public new void Display(),Console.WriteLine(“haha,!”);,15,Chapter2 : C# Language | Object-Oriented C#,2- Polymorphism,Polymorphic behavior enable the actions performed by a function member to vary depending on the run-time type of the instance through which the function member is invoked.,Late binding,Methods, properties, and indexers can be,virtual, which means that their implementation can be overridden in derived classes.,16,Chapter2 : C# Language | Object-Oriented C#,2- Sample of Polymorphism,Window,List Box,Button,17,Chapter2 : C# Language | Object-Oriented C#,2-,new,v.s.,override,public class Location,public virtual void Display(),Console.WriteLine(Location.Display,();,public class URL : Location,public new void Display(),Console.WriteLine(URL.Display,();,public class Tester,public static void Main(),URL u = new URL();,Location l = u;,l.Display();,override,18,Chapter2 : C# Language | Object-Oriented C#,3- Abstract Class,1/2,The,abstract,modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class.,19,Chapter2 : C# Language | Object-Oriented C#,3- Abstract Class,2/2,An abstract class cannot be instantiated directly, and it is an error to use the,new,operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be,null,or contain references to instances of non-abstract classes derived from the abstract types.,An abstract class is permitted (but not required) to contain abstract members.,An abstract class cannot be sealed.,20,Chapter2 : C# Language | Object-Oriented C#,3- Sealed classes,Abstract class supply the template for the derived subclass,While sealed class forbids being inherited occasionally.,21,Chapter2 : C# Language | Object-Oriented C#,4- Object : root of all classes,Object : root of all classes, include value type , such as,int, string, byte,Methods of Object,Equals(),GetHashCode,(),GetType,(),ReferenceEquals,(),ToString,(),Finalize(),MemberwiseClone,(),22,Chapter2 : C# Language | Object-Oriented C#,4- Boxing and,Unboxing,Boxing and,Unboxing,can make an instance to encapsulate a value-typed variable, and use it as reference type,23,Chapter2 : C# Language | Object-Oriented C#,4- Boxing,123,int,123,ref,i,o,object o = i,int,i = 123,Boxing i,24,Chapter2 : C# Language | Object-Oriented C#,4-,Unboxing,123,int,123,ref,i,o,object o = i,int,i = 123,Boxing i,123,j,int,j = (,int)o,Unboxing,o,25,Chapter2 : C# Language | Object-Oriented C#,4- Nested Class,Please reference “C# Language Specification” 10.2.6 Nested types,class A,class B,26,Chapter2 : C# Language | Object-Oriented C#,5- Operators Overloading,An operator declaration must include both a public and a static modifier.,Different from C+, C# cant declare non-static operator, and binary operator must take two value as two parameter,27,Chapter2 : C# Language | Object-Oriented C#,5- In C+,1/2,class,CString,private:,char szValue256;,public:,CString,operator +(,CString,strAppend,);,char*,GetBuffer,();,CString,CString:operator,+ (,CString,strAppend,),return,szValue,+ ;,28,Chapter2 : C# Language | Object-Oriented C#,5- In C+,2/2,class,CString,private:,char szValue256;,public:,char*,GetBuffer,();,friend,CString,operator +(,CString,strPrefix,CString,strSuffix,);,CString,operator + (,CString,strPrefix,CString,strSuffix,),return ;,29,Chapter2 : C# Language | Object-Oriented C#,5- In C#,1/2,1)Unary operator: + - ! ,2)Binary operator:,+,-,*,/,%,&,|,=,!=,=, or,and,operator,=,and,operator,=!=+=-=*=/=%=&=|=-,33,Chapter2 : C# Language | Object-Oriented C#,6-,Struct,1/3,We can consider,struct,as a light class, and it has property, method, field, operator, nested type and indexer,Its a value type, so that you should better avoid use boxing and,unboxing,on,struct,34,Chapter2 : C# Language | Object-Oriented C#,6-,Struct,2/3,Struct,doesnt support inheritance, but inherit from object implicitly.so we can look it as a sealed class, and it can implement a series of interface.,Struct,has neither destructor nor user-defined no-parameters constructor. if you declare one, you should initialize all data member.,35,Chapter2 : C# Language | Object-Oriented C#,6-,Struct,3/3,Field should not be initialized when declaring, though its commended when being declared in class.For example:,public,struct,SSomeStruct,private,int,xVal,= 0;,private,int,yVal,= 0;,36,Chapter2 : C# Language | Object-Oriented C#,6-,Demo on,Struct,1/3,following code describe a 2-d point,public,struct,Location,public,Location(int,x,int,y),this.xVal,= x;,this.yVal,= y;,public,int,x,get,return,xVal,;,set,xVal,= value;,/ define a property y which is the same with x,attribute,access modifier,struct,identifier:interface list,member of,struct,;,37,Chapter2 : C# Language | Object-Oriented C#,6-,Demo on,Struct,2/3,public override string,ToString,(),return String.Format(“(0,1)”,xVal,yVal,);,private,int,xVal,;,private,int,yVal,;,/ End of class Location,public class Tester,public static void,myFunc(Location,loc),loc.x = 50;,loc.y = 100;,public static void Main(),Location loc1 = new Location(200, 300);,myFunc(loc1);,38,Chapter2 : C# Language | Object-Oriented C#,6-,Demo on,Struct,3/3,What will happen when we change “,struct,” to “class”?,Why ?,Whats the difference between value type and reference type?(Lets look back),39,Chapter2 : C# Language | Object-Oriented C#,6- We can look at it as built-in type,we dont have to new a instance of,struct, and use it like,int, long, or byte. For example:,static void Main(),Location loc1;,loc1.xVal = 75;/,xVal,and,yVal,is public.,loc1.yVal = 225;/ Must assign all field, or /compiler will alert,40,Chapter2 : C# Language | Object-Oriented C#,7-,Interface,An interface defines a contract. A class or,struct,that implements an interface must adhere to its contract. An interface may inherit from multiple base interfaces, and a class or,struct,may implement multiple interfaces.,Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide implementations for the members that it defines. The interface merely specifies the members that must be supplied by classes or interfaces that implement the interface.,-Reference from C Sharp Language Specification,41,Chapter2 : C# Language | Object-Oriented C#,7-,Interfaces declaration,attributes,interface-modifiers,interface,identifier base interface, interface-body ,;,For example:,interface,IStorable,void Read();,void Write();,int,Statusget; set;,42,Chapter2 : C# Language | Object-Oriented C#,7-,Interfaces Implementation,1/2,attributes,access-modifiers,class,identifier interface list, implementation ,For example,public class Document :,IStorable,public void Read(),Console.WriteLine(“Implementing,Read for,IStorable,”);,/ Leave the implementing of Write for,IStorable,/ because its like Read;,43,Chapter2 : C# Language | Object-Oriented C#,7-,Interfaces Implementation,2/2,/ Implementing a property,public,int,Status,get,return status;,set,status = value;,/ a field for storing status,private,int,status=0;/Initialization is recommended,/ End of class Document,44,Chapter2 : C# Language | Object-Oriented C#,7-,Implement multi-interface,for example:,public class/,struct,Document :,IStorable,ICompressible,You should implement all the methods of interfaces which declared.,45,Chapter2 : C# Language | Object-Oriented C#,7-,interfaces extension,for example:,interface,ILoggedCompressible,:,ICompressible,void,LogSavedBytes,();,46,Chapter2 : C# Language | Object-Oriented C#,7-,interfaces Integration,for example:,interface,ILoggedCompressible,:,IStoreable,ILoggedCompressible,void,LogOriginalSize,();,47,Chapter2 : C# Language | Object-Oriented C#,7-,interfaces Demo,ILoggedCompressible,IStorable,ICompressible,Document,IStorableCompressible,IEncryptable,48,Chapter2 : C# Language | Object-Oriented C#,7-,Accessing interfaces method,Document doc = new Document(Test Document);,IStorable,isDoc,= doc as,IStorable,;,if (,isDoc,!= null),isDoc.Read,();,isDoc.Status,= -1;,else,Console.WriteLine(IStorable,not supported);,49,Chapter2 : C# Language | Object-Oriented C#,7-,Accessing interfaces method,We cant,IStorable,isDoc,= new,IStorable,();,But we can,Document doc = new Document(Test Document);,IStorable,isDoc,= doc as,IStorable,;,/(,IStorable)doc,Even,IStorable,isDoc,= (,IStorable,) new Document(“Test”);,Be careful that this statement will throw,InvalidCastException, when docs ancestor is not,IStorable,Look back “is” and “as” operator,50,Chapter2 : C# Language | Object-Oriented C#,7- Comparison between as and is,if (doc is,IStorable,),IStorable,isDoc,= (,IStorable)doc,;,isDoc.Read,();,cast 2 times,IStorable,isDoc,= doc as,IStorable,;,if (,isDoc,!= null),isDoc.Read,();,cast 1 times,Use it when testing only!,51,Chapter2 : C# Language | Object-Oriented C#,7-,Override interfaces method,public class Document :,IStorable,public virtual void Read(),public class Note : Document,public override void Read(),52,Chapter2 : C# Language | Object-Oriented C#,7-,Explicit interface member implementations,1/2,interface,IAccessable,void Read();,public class Document :,IStorable,IAccessable,public void Read(),void,IAccessable.Read,(),no access modifier,53,Chapter2 : C# Language | Object-Oriented C#,7-,Explicit interface member implementations,2/2,Solve the collision;,The only means of accessing explicit interface member implementations is convert instance to interface;,So we can hide member,Recommend to access method with interface, except,54,Chapter2 : C# Language | Object-Oriented C#,7-,Explicit interface member implementations,2/2,Value type(such as,struct,),In the procedure of conversion, system boxing the,struct,implicitly, so that you get a reference to interface. Low productive,Side effect taken by implicit boxing,55,Chapter2 : C# Language | Object-Oriented C#,Summary,1-,Access Modifier(Inheritance),Modifier,Intuitive meaning,Public,Access not limited,Private,Access limited to the containing type,Protected,Access limited to the containing class or types derived from the containing class,Internal,Access limited to current assembly,Protected internal,Access limited to this program or types derived from the containing class,Assemblies,are used for physical packaging and deployment.,An assembly acts as a container for types. An assembly may contain types, the executable code used to implement these types, and references to other assemblies.,(e.g. DLL files),57,Chapter2 : C# Language | Object-Oriented C#,1-,Assembly,Demo,csc,/t:library,polymorphism.cs,csc,/,r:polymorphism.dll,tester.cs,internal,58,Chapter2 : C# Language | Object-Oriented C#,
展开阅读全文