1 / 23

Policy Modification

Policy Modification . Please do not use the internet during the first hour of class Thanx!. Chapter 10 OO Features Chapter Objectives. Use inheritance to extend the functionality of user-defined classes Create abstract classes that include abstract methods Design and implement interfaces.

LionelDale
Télécharger la présentation

Policy Modification

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Policy Modification • Please do not use the internet during the first hour of class • Thanx!

  2. Chapter 10 OO Features Chapter Objectives • Use inheritance to extend the functionality of user-defined classes • Create abstract classes that include abstract methods • Design and implement interfaces

  3. Object-Oriented Language Features • Abstraction • Abstract or identify the objects involved in the problem • “thought of apart from concrete realities, specific objects, or actual instances “ • Encapsulation • Packaging data and behaviors into a single unit • Inheritance • Reuse of code through extension of class structure • Polymorphism – Many Forms • Multiple implementations of the same behaviors • Shape.Draw() • Shape.Area()

  4. Inheritance • Enables you to • create a general class • define specialized classes that have access to the members of the general class • Associated with an "is a" relationship • Specialized class “is a” form of the general class • AirVehicle “is a” Vehicle • Classes can also have a "has a" relationship—not associated with inheritance • "has a" relationship associated with containmentor aggregation • Airplane “has a” Wing

  5. Every object inherits from System.Object • Object Class • Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy. • http://msdn2.microsoft.com/en-us/library/system.object.aspx • Equals • The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation. • http://msdn2.microsoft.com/en-us/library/bsc2ak47.aspx • See documentation for • ToString, GetHashCode, GetType

  6. Inheriting From Other .NET FCL Classes • Easily add base class functionality to programs • public class MyGame: Microsoft.Xna.Framework.Game • Extend System.Windows.Forms.Form class to build GUIs (Button, Label, TextBox, ListBox) Base class Derived class

  7. Base Classes and Inheritance • Define classes from which other classes can inherit • Base class is called the super or parent class • Define data members with a protected access modifier • Define constructors with public access modifiers base/parent class derived/child class • public class Vehicle : AirVehicle

  8. MethodsVirtual and Override • Use virtual to indicate that a method or property in a base class may be overridden in a derived class • i.e. override Equals method to support value equality • Use override with a method or property in a derived class. You then provide the new code for the method or property in the derived class. This code is executed instead of the code in the base class whose method or property is overridden. • public override bool Equals(object obj)

  9. Overriding Methods • Replace the method defined at a higher level • Keyword override included in derived class • Base method includes virtual, abstract, or override keyword • Overriding differs from overloading a method • Overridden methods have exactly the same signature • Overloaded methods each have a different signature

  10. Overriding Methods (continued) • Overriding the ToString method is an example of polymorphism • many forms • ToString( ) method can have many different definitions • ToString( ) uses the virtual modifier implying any class can override it • public virtual string ToString() • Override with • public override string ToString()

  11. Using Members of the Base Class • Scope • Methods defined in subclass take precedence when named the same name as member of a parent class • Can call an overridden method of the base class • Use keyword base before the method name returnbase.GetSleepAmt( ) // Calls GetSleepAmt( ) in // parent class

  12. Making Stand-alone Components • Compile class and create an assembly • Assemblies are units configured and deployed in .NET • Classes can be compiled and stored as a dynamic link library (DLL) instead of into the EXE file type • Add a reference to the DLL • That referenced file with the .dll extension becomes part of the application’s private assembly

  13. Dynamic Link Libraries • Dynamic Link Libraries • Loaded “dynamically” at run time • May be loaded by multiple applications • Cannot be run directly

  14. Creating a Client Application To Use the DLL • DLL components can be reused with many different applications • Two Steps • Add a reference to the DLL components • Include a using statement with the namespace • Then declare an objects of the component type(s) • Use members of the derived, base or referenced classes

  15. In Class Exercise • Create a Bubble Sort DLL • csc /out:BubSortLib.dll /target:library BubSort.cs • Create a Bubble Sort Test Driver • Add a using statement • using MyBubSortLibNamespace • Compile with: csc /r:BubSortLib.dll BubSortTest.cs • Make sure BubSortLib.dll is in the same directory

  16. Assignment • Create an AirVehicle class and a GroundVehicle class • These classes inherit from the Vehicle class (previous homework) • Add virtual methods to your Vehicle class • Accelerate • Brake • Override these methods in the AirVehicle and GroundVehicle classes • Build a dynamic link library using the command line compiler • Create a project using Visual C# Express or the command line compiler • Add reference to Vehicle Library your project • Copy dll to project directory • Your project should not include the vehicle library cs files • Create a driver that uses your Vehicle.dll library • You can create a console application that simply uses Console.WriteLine to identify execution of the overridden Accelerate and Brake methods • Demonstrate project in class next week

  17. Summary • Major features of object-oriented languages • Abstraction • Encapsulation • Inheritance • Polymorphism • Multitier applications using component-based development methods

  18. Chapter Summary (continued) • Use inheritance to extend the functionality of user defined classes • virtual – indicates the method may be overriden in a derived class • override – indicates base class functionality being replaced by derived class functionality • new – hides base class member in a derived class • public new void MyMethod()

  19. Adding a Reference using Visual Studio

  20. Adding a Reference to the Base Class One of the first things to do is Add a Reference to the Parent DLL

  21. Adding a Reference to the Base Class (continued) Use Browse button to locate DLL

  22. Adding a New Using Statement • In the subclass class, if you simply type the following, you receive an error message publicclass Student : Person

  23. Adding a New Using Statement (continued) Notice fully qualified name • To avoid error, could type publicclass Student : PersonNamespace.Person • Better option is to add a using directive using PersonNamespace; // Use whatever name you // typed for the namespace for Person • After typing program statements, build the DLL from the Build option under the Build menu bar

More Related