برنامه سازی پیشرفته
اسلاید 1: برنامه سازي پيشرفته2
اسلاید 2: Object-Oriented DesignThe focus of methods is on doing things; roughly speaking, we can say that methods focus on the verbs. In object-oriented design, we focus on the nounsIn object-oriented design, we tend to group methods together according to the nounsImportant for large, complex programs
اسلاید 3: User-Defined ClassA user-defined class is also called a user-defined typeclass written by a programmerA class encapsulates (wrap together) data and methods:data members (member variables or instance variables)methods that manipulate data members
اسلاید 4: ObjectsAn object has:state - descriptive characteristicsbehaviors - what it can do (or be done to it)For example, consider a coin in a computer gameThe state of the coin is its current face (head or tail)The behavior of the coin is that it can be flippedNote the interactions between state and behaviorsthe behavior of an object might change its statethe behavior of an object might depend on its state
اسلاید 5: تعاريفبه دستهها «کلاس» (Class) ميگويند.به نمونههاي هر کلاس «شي» (Object) ميگويند.مشخصات هر شي را «صفت» (Data Member) مينامند.به رفتارهاي هر شي «متد» (Method) ميگويند.
اسلاید 6: اصول برنامهنويسي شيگرابستهبندي (Encapsulation) وراثت (Inheritance)چندريختي (Polymorphism)
اسلاید 7: بستهبندي (Encapsulation)يعني اين که دادههاي مرتبط، با هم ترکيب شوند و جزييات پيادهسازي مخفي شود.
اسلاید 8: وراثت (Inheritance)در دنياي واقعي، وراثت به اين معناست که يک شي وقتي متولد ميشود، خصوصيات و ويژگيهايي را از والد خود به همراه دارد.امتياز وراثت در اين است که از کدهاي مشترک استفاده ميشود و علاوه بر اين که ميتوان از کدهاي قبلي استفاده مجدد کرد، در زمان نيز صرفهجويي شده و استحکام منطقي برنامه هم افزايش مييابد.
اسلاید 9: چندريختي (Polymorphism)که به آن چندشکلي هم ميگويند به معناي يک چيز بودن و چند شکل داشتن است. چندريختي بيشتر در وراثت معنا پيدا ميکند.
اسلاید 10: public int x, y;private char ch;class MyClassDefining ClassesUse Project menu -> Add Class to add a new class to your projectA class contains data declarations and method declarationsData declarationsMethod declarationsMember (data/method) Access Modifierspublic : member is accessible outside the classprivate : member is accessible only inside the class definition
اسلاید 11: Data DeclarationsYou can define two types of variables in a class (called class variables)static class variablesnonstatic variables are called instance variables (fields) because each instance (object) of the class has its own copyclass variables can be accessed in all methods of the class
اسلاید 12: Method DeclarationsA class can define many types of methods:Access methods : read or display dataPredicate methods : test the truth of conditionsConstructorsinitialize objects of the classthey have the same name as the classThere may be more than one constructor per class (overloaded constructors)can take argumentsIf a class has no constructor, a default constructor is providedIt has no code and takes no parametersthey do not return any valueit has no return type, not even void
اسلاید 13: يک کلاس ميتواند سازندههاي مختلفي داشته باشد. سادهترين آنها، سازندهاي است که هيچ پارامتري ندارد. به اين سازنده سازندۀ پيشفرض (Default Constructor) ميگويند. اگر در يک کلاس، سازندۀ پيشفرض تعريف نشود، کامپايلر به طور خودکار آن را براي کلاس مذکور ايجاد ميکند.
اسلاید 14: Example: Time1 classWe define the Time1 class to represent time.The state of a time object can be represented by:hour, minute, second : integers representing timeWe might define the following methods:a Time1 constructor, to set up the objecta SetTime method, to set timea ToUniversalString method, to convert the internal representation to a string representing the time in 24 hour formata ToStandardString method, to convert the internal representation to a string representing the time in 12 hour format
اسلاید 15: 1 // Time1.cs2 // Class Time1 maintains time in 24-hour format.3 4 using System;5 6 // Time1 class definition7 public class Time18 {9 private int hour; // 0-2310 private int minute; // 0-5911 private int second; // 0-5912 13 // Time1 constructor initializes instance variables to 14 // zero to set default time to midnight15 public Time1()16 {17 SetTime( 0, 0, 0 );18 }19 20 // Set new time value in 24-hour format. Perform validity21 // checks on the data. Set invalid values to zero.22 public void SetTime( int hourValue, int minuteValue, int secondValue )23 {24 hour = ( hourValue >= 0 && hourValue < 24 ) ? hourValue : 0;25 minute = ( minuteValue >= 0 && minuteValue < 60 ) ? minuteValue : 0;26 second = ( secondValue >= 0 && secondValue < 60 ) ? secondValue : 0;27 }سازنده پيش فرض (Default Constructor)
اسلاید 16: 28 // convert time to universal-time (24 hour) format string29 public string ToUniversalString()30 {31 return String.Format( 32 {0:D2}:{1:D2}:{2:D2}, hour, minute, second );33 }33 35 // convert time to standard-time (12 hour) format string36 public string ToStandardString()37 {38 return String.Format( {0}:{1:D2}:{2:D2} {3},39 ( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ),40 minute, second, ( hour < 12 ? AM : PM ) );41 } 42 43 } // end class Time1
اسلاید 17: 1 // TimeTest1.cs2 // Demonstrating class Time1.3 4 using System;5 using System.Windows.Forms;6 7 // TimeTest1 uses creates and uses a Time1 object8 class TimeTest19 {10 // main entry point for application11 static void Main( )12 {13 Time1 timeObj1 = new Time1(); // calls Time1 constructor14 string output;15 16 // assign string representation of time to output17 output = Initial universal time is: +18 timeObj1.ToUniversalString() +19 nInitial standard time is: +20 timeObj1.ToStandardString();21 22 // attempt valid time settings23 timeObj1.SetTime( 13, 27, 6 );24 25 // append new string representations of time to output26 output += nnUniversal time after SetTime is: +27 timeObj1.ToUniversalString() +28 nStandard time after SetTime is: +29 timeObj1.ToStandardString();30 31 // attempt invalid time settings32 timeObj1.SetTime( 99, 99, 99 );33 براي ايجاد شي از عملگر new استفاده ميشود.با ايجاد شي تابع سازنده فراخواني ميشود.
اسلاید 18: 34 output += nnAfter attempting invalid settings: +35 nUniversal time: + time.ToUniversalString() +36 nStandard time: + time.ToStandardString();37 38 MessageBox.Show( output, Testing Class Time1 );39 40 } // end method Main41 42 } // end class TimeTest1
اسلاید 19: Class View and Object BrowserClass View and Object Browser are features of Visual Studio that facilitate the design of object-oriented applicationsClass ViewDisplays variables and methods for all classes in a projectDisplays as treeview hierarchical structure+ at nodes allows nodes to be expanded- at nodes allows nodes to be collapsedCan be seen by selecting View < Class View
اسلاید 20: Class View: Example
اسلاید 21: public class Time2 { private int hour; // 0-23 private int minute; // 0-59 private int second; // 0-59 public Time2() { SetTime( 0, 0, 0 ); } public Time2( int hour ) { SetTime( hour, 0, 0 ); } public Time2( int hour, int minute ) { SetTime( hour, minute, 0 ); } public Time2( int hour, int minute, int second ) { SetTime( hour, minute, second ); } public Time2( Time2 time ) { SetTime( time.Hour, time.Minute, time.Second ); }
اسلاید 22: 1 // TimeTest2.cs2 // Using overloaded constructors.3 4 using System;5 using System.Windows.Forms;6 7 // TimeTest2 demonstrates constructors of class Time28 class TimeTest29 {10 // main entry point for application11 static void Main( )12 {13 Time2 time1, time2, time3, time4, time5, time6;14 15 time1 = new Time2(); // 00:00:0016 time2 = new Time2( 2 ); // 02:00:0017 time3 = new Time2( 21, 34 ); // 21:34:0018 time4 = new Time2( 12, 25, 42 ); // 12:25:4219 time5 = new Time2( 27, 74, 99 ); // 00:00:0020 time6 = new Time2( time4 ); // 12:25:42
اسلاید 23: Encapsulation: Two Views of an ObjectYou can take one of two views of an object:internal - the structure of its data, the algorithms used by its methodsexternal - the interaction of the object with other part of the world
اسلاید 24: Encapsulation: An Object As a Black BoxFrom the external view, an object is an encapsulated entity, providing a set of specific servicesThese services define the interface to the objectAn encapsulated object can be thought of as a black boxThe user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplishedClientMethodsData
اسلاید 25: Accomplish Encapsulation: Access ModifiersIn C#, we accomplish encapsulation through the appropriate use of access modifiersAn access modifier is a C# keyword that specifies the accessibility of a method, data field, or classWe will discuss two access modifiers: public, privateWe will discuss the other two modifiers (protected, internal) later
اسلاید 26: The public and private Access ModifiersClasses (types) and members of a class that are declared with public can be accessed from anywhereMembers of a type that are declared with private can only be accessed from inside the classMembers of a class declared without an access modifier have default private accessibility26
اسلاید 27: 1 // RestrictedAccess.cs2 // Demonstrate compiler errors from attempt to access 3 // private class members.4 5 class RestrictedAccess6 {7 // main entry point for application8 static void Main( string[] args )9 {10 Time1 time = new Time1();11 12 time.hour = 7;13 time.minute = 15;14 time.second = 30;15 }16 17 } // end class RestrictedAccess
اسلاید 28: پيادهسازي بستهبنديبه عنوان يك قانون كلي، هيچ عضو دادهاي به صورت public تعريف نميشود.از خصوصيات (properies) براي دسترسي به دادهها استفاده ميكنيم.اعضاي دادهاي به صورت private و خصوصيات آنها به صورت public تعريف ميشوند.خصوصيات دو method دارند :get : دسترسي براي خواندن اعضاي دادهايset : دسترسي براي نوشتد اعضاي دادهاي
اسلاید 29: پيادهسازي سه خصوصيت براي كلاس Time2// property Hourpublic int Hour{ get { return hour; } set { hour = ( ( value >= 0 && value < 24 ) ? value : 0 ); }} // end property Hour// property Minutepublic int Minute{ get { return minute; } set { minute = ( ( value >= 0 && value < 60 ) ? value : 0 ); }} // end property Minute // property Secondpublic int Second{ get { return second; } set { second = ( ( value >= 0 && value < 60 ) ? value : 0 ); }} // end property Second
اسلاید 30: Time2 time = new Time2();time.Hour = 15;time.Minute = 45;time.Second = 20;...
نقد و بررسی ها
هیچ نظری برای این پاورپوینت نوشته نشده است.