
10266A_07.ppt
- Количество слайдов: 15
Module 7 Encapsulating Data and Methods
Module Overview • Controlling Visibility of Type Members • Sharing Methods and Data
Lesson 1: Controlling Visibility of Type Members • What Is Encapsulation? • Comparing Private and Public Members • Comparing Internal and Public Types
What Is Encapsulation? Definition An essential object-oriented principle Hide internal data and algorithms Provide a well-defined public operation Benefits Makes external code simpler and more consistent Enables you to change implementation details later
Comparing Private and Public Members private Least permissive access level class Sales { private double monthly. Profit; private void Set. Monthly. Profit(double monthly. Profit) { this. monthly. Profit = monthly. Profit; } } public Only accessible from within the Sales class Most permissive access level class Sales { private double monthly. Profit; public void Set. Monthly. Profit(double monthly. Profit) { this. monthly. Profit = monthly. Profit; } } Accessible to any other type
Comparing Internal and Public Types Internal is the default access modifier for types Internal permits access only to types in the same assembly Public permits access to other types in other assemblies Fabrikam. Sales. dll class Sales {. . . } class Costs {. . . } Types only accessible to types in the Fabrikam. Sales. dll Fabrikam. Services. dll Fabrikam. Dal. dll Fabrikam. Hr. dll public class Person {. . . Accessible to types in } other assemblies public class Record {. . . }
Lesson 2: Sharing Methods and Data • Creating and Using Static Fields • Creating and Using Static Methods • Creating Static Types and Using Static Constructors • Creating and Using Extension Methods
Creating and Using Static Fields Instance fields contain data pertinent to a specific instance of a type Static fields contain data pertinent to the type itself class Sales { public static double sales. Tax. Percentage = 20; } To access a static field, you use the name of the class, followed by a period, followed by the name of the field Sales. sales. Tax. Percentage = 32;
Creating and Using Static Methods Static methods provide functionality pertinent to the type itself and not an instance of the type You can use static methods to implement utility classes, like the File class in the System. IO namespace class Sales { public static double Get. Monthly. Sales. Tax(double monthly. Profit) {. . . } } You do not need to create an instance of the type to use static methods double monthly. Sales. Tax = Sales. Get. Monthly. Sales. Tax(34267);
Creating Static Types and Using Static Constructors You can declare types as static by using the static modifier Static types can only contain static members static class Sales { Defined with the static modifier } Both static and instance types can have constructors You can explicitly invoke instance constructors with the new keyword Static constructors are invoked implicitly by the CLR static class Sales { static Sales() { } } Defined with the static modifier Defined without any parameters Defined without an access modifier
Creating and Using Extension Methods Add extension methods to an existing class without: - Having access to the source of the existing class - Modifying or recompiling the existing class - Deriving a new type from the existing class namespace Fabrikam. Extensions { static class Int. Extension Static method Extension type { internal static int Next. Rand(this int seed, int max. Value) { Random random. Number. Generator = new Random(seed); return random. Number. Generator. Next(max. Value); } } } using Fabrikam. Extensions; . . . int i = 8; int j = i. Next. Rand(20); No reference to the Int. Extension class
Lab: Encapsulating Data and Methods • Exercise 1: Hiding Data Members • Exercise 2: Using Static Members to Share Data • Exercise 3: Implementing an Extension Method Logon information Virtual machine 10266 A-GEN-DEV User name Student Password Pa$$w 0 rd Estimated time: 60 minutes
Lab Scenario
Lab Review Questions • What access modifier would you use to stop fields being accessed from outside the parent type? • When declaring a constructor in a static type, how many parameters can the constructor take? • When declaring an extension method, what keyword must you use to prefix the first parameter?
Module Review and Takeaways • Review Questions • Best Practices