Addison Wesley is an imprint of

31
© 2011 Pearson Addison-Wesley. All rights reserved. Slide 1 Addison Wesley is an imprint of

description

Addison Wesley is an imprint of. Chapter 6. Advanced Classes. Updated: 3/22/2011. Contents. 6.1Structures 6.2Components 6.3Unit Testing 6.4Events 6.5Inheritance. 6.1Structures. Container for variables, properties, methods, and events lightweight class - PowerPoint PPT Presentation

Transcript of Addison Wesley is an imprint of

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 1

Addison Wesley is an imprint of

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 2

Chapter 6

Advanced Classes

Updated: 3/22/2011

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 3

Contents

• 6.1 Structures• 6.2 Components• 6.3 Unit Testing• 6.4 Events• 6.5 Inheritance

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 4

6.1 Structures

• Container for variables, properties, methods, and events

− lightweight class

• A structure variable holds its own data− unlike a class instance, which contains a reference

• The New operator is only required when calling a constructor with parameters

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 5

Copying and Comparing Structures

• Assignment operator (=) copies all structure fields• Equals method compares the contents of structure

fields• Sample Structure declaration:

Structure Point

Public Property X As Integer

Public Property Y As Integer

End Structure

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 6

6.2 Components

• A .NET assembly is a basic unit of deployment that is compiled into a DLL file

• A component is a collection of related classes that belong to a single assembly

− aka class library

• When possible, a component should be reusable• General types:

− interface components− code components

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 7

Tutorial 6-1

• Creating a Component and Referencing it From Another Application

• RegistrationLib component− contains a Student class

• You create a second application that references the RegistrationLib component

− uses an Imports statement:Imports RegistrationLib

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 8

Tutorial 6-2

• Adding an Advisor Class to the RegistrationLib Component

• Middle tier class• Determines the maximum number of credits that a

student can take, based on various criteria

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 9

Tutorial 6-3

• Using the Advisor and Student Classes• The Registration UI application calls methods in the

RegistrationLib component− Advisor and Student classes

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 10

6.3 Unit Testing

• Continuous software testing− doesn't leave testing to the end of the project

• Automated tests− easier to repeat than manual tests

• Written at the same time as the application code• May be run again at any time

− (regression test)

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 11

Two Testing Paradigms

• White box testing− tester can view the application source code

• Black box testing− tester can only see the relation between inputs and

outputs. Cannot see source code.

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 12

Unit Testing Basics

• Each unit test is designed to test one particular code unit of an application.

• When a unit test fails, it stops executing and returns immediately.

• Unit tests do not run in any predetermined sequence.

• Unit tests are executed by a utility program known as a test engine.

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 13

Unit Testing in .NET

• Microsoft.VisualStudio.TestTools.UnitTesting namespace

• Steps:1. Create a set of classes to be tested.2. Create a Visual Studio test project.3. Add unit tests to the test project.4. Run the Visual Studio testing tool.

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 14

Unit Testing Attributes

• TestClass – identifies a class that contains unit tests• TestMethod – identifies a method that performs a

unit test

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 15

Tutorial 6-4• Creating a Unit Test Project• Test the IntArray class

− method that finds the largest value in an array

<TestClass()> _ Public Class IntArrayTest

<TestMethod()> _ Public Sub GetLargestTest() Dim target As IntArray = New IntArray() target.Data = {40, 16, 12, 22, 0, -33} Dim expected As Integer = 40 Dim actual As Integer = target.GetLargest Assert.AreEqual(expected, actual) End Sub End Class

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 16

Tutorial 6-5

• Creating More Unit Tests for the IntArray Class• Rearrange the order of the integers• Fix errors as they are found when running the tests• Include a variety of test values, including an empty

array

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 17

Assert Class Methods

• The Assert class provides methods to compare expected and actual values in unit tests

• Commonly used Assert methods:− AreEqual, AreNotEqual, AreSame, AreNotSame, Fail,

IsFalse, IsTrue, IsNull, IsNotNull

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 18

Tutorial 6-6

• Testing the Advisor.MaxCredit Method• Example of testing an existing application• RegistrationLib component

− New tests:

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 19

6.4 Events

• Provide a signaling system for messages that must be broadcast from an object to other objects that are listening for the messages.

• A delegate is a template that describes the return type and parameter list for a related group of event handler methods

• An object declared using the WithEvents qualifier can raise events at runtime

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 20

Tutorial 6-7

• The WeatherStation Events Application• Demonstrates the raising and handling of events for

a simulated weather station• User interface:

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 21

6.5 Inheritance

• Inheritance refers to a parent-child relationship between two classes

• A derived class builds on attributes and behaviors of a base class

− attributes = properties− behaviors = methods and events

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 22

Examples of Inheritance

Base Class Derived ClassPerson StudentAccount CheckingAccountMessage MailMessageVehicle Automobile

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 23

Multiple Derived Classes

• Any number of derived classes can inherit from the same base class:

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 24

Accessing Base Class Members

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 25

Access Modifiers

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 26

Heroes and Villains Example

• Classes from a computerized role-playing game

When a derived object is constructed, its base class constructors execute before the object’s own constructor executes. When a Hero is constructed, the compiler automatically calls the default constructor for Person.

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 27

Assigning Object References

• Object references can be assigned upward in the inheritance hierarchy from a derived type to a base type:

Dim P As Person = New Hero("Aquaman", "Swims")

• Assigning a base type to a derived type always requires the use of a downward cast operation

− Example:Dim P As New Person("Joe")

Dim Z As Hero = CType(P, Hero)

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 28

Overriding and Overloading

• To override a method is to replace a base class method with a derived class method having the same signature.

• To overload a method is to create a new method having the same name as an existing method in the same class or a base class. The new method must have a different parameter list.

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 29

Tutorial 6-8

• Student Inheritance Application• Creates a collection of these types:

− Student (undergraduate students)− GradStudent (graduate students)

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 30

Key Terms

• Assert.AreEqual Method• Assert.AreNotEqual Method• Assert class• assertion• attribute name• automated test• base class• black box testing• class library• continuous software testing• delegate• component• derived class• downward cast

handle an eventinheritanceInherits keyword.NET assemblyoverload a methodoverride a methodOverrides keywordraise an eventregression testingtest enginetest projectunit testupward castwhite box testing

© 2011 Pearson Addison-Wesley. All rights reserved. Slide 31

The End