BACS 387 - University of Northern Colorado

Download Report

Transcript BACS 387 - University of Northern Colorado

Class Members
Definition & Use






Learn how to define and use fields, methods, and
properties.
Learn how to use refactoring and automatic
properties to speed up the programming process.
Learn how to hide base class methods.
Learn the difference between hiding and overriding
methods.
Learn how to implement interfaces
Learn about partial class definitions and partial class
methods.
7/16/2015
OO Programming
2

There are two general categories of class members in C#.
 Data members –store data associated with the class or instances
of the class.
 Function members – execute code and model the actions of the
real-world object that the class represents.

C# classes can have any number of data and function
members. They can come from any combination of 9
possible member types.
Data Members
Function Members
Fields √
Methods
Operators
Constants √
Properties
Indexers
Constructors √
Events
Destructors √
Last update: 7/16/2015
OO Programming
3
Accessibility Keyword
Meaning
public
Members are accessible from any code.
private
Members are accessible only from code that is part of
the class (default).
internal
Members are accessible only from code within the
assembly where they are defined.
protected
Members are accessible only from code that is part of
either the class or a derived class.
protected internal
Members that are accessible from code-derived classes
within the assembly where they are defined.
Last update: 7/16/2015
OO Programming
4
Method Keyword
Meaning
static
The member is accessible only through the class.
virtual
The method may be overridden
abstract
The method must be overridden in a non-abstract derived
class.
override
The method overrides a base class method. Required if a
method is being overridden (versus hidden).
extern
The method definition is elsewhere.
Last update: 7/16/2015
OO Programming
5


Fields are defined using standard variable declaration
format.
They can also use the accessibility modifiers (e.g., public,
private, …).
class ExampleClass
{
public int AnInteger;
int aPrivateInt;
static double myDouble;
const int aConstant = 12;
public readonly float MyFloat = 23.3;
}

The standard is to use PascalCasing for public fields and
camelCasing for private fields.
Last update: 7/16/2015
OO Programming
6


Methods use standard function format along with
accessibility (e.g., public, internal,…) and optional
modifier keywords (e.g., virtual, override, …).
A basic public method is defined below:
class MethodExample
{
public int GetNumber()
{
return type
return 7;
}
required when void not used
}

Methods use PascalCasing and can accept arguments.
They also can have a return type and must include a
return statement if a type other than void is used.
Last update: 7/16/2015
OO Programming
7





Properties are similar to fields in that they provide
access to data about an object.
Properties differ from fields in that they do not
provide direct access to the data.
Properties can be thought of as small, special
purpose methods, that control access to the private
fields of a class.
Properties can be set up to allow read-only, writeonly, or read-write access to fields.
Properties are one of the ways that C# implements
encapsulation (i.e., information hiding).
Last update: 7/16/2015
OO Programming
8



Traditional OO-Programming uses Accessors (i.e., “get
methods”) and Mutators (i.e., “set methods”) to
control access to the private field data in a class.
They were typically named with the words “get” and
“set” to indicate their function. For example:
GetLength() and SetLength() could be the
accessor and mutator for a rectangle object.
The .NET languages implement the notion of properties
to handle this task more uniformly. The .NET
environment also provides some property automation
that helps speed up the programming task.
Last update: 7/16/2015
OO Programming
9

The following code defines a basic property.
...
private int myIntProperty;
...
public int MyIntProperty
{
get
{
return myIntProperty;
}
set
{
myIntProperty = value;
}
}
Last update: 7/16/2015
OO Programming
field used by the property.
Note that it is ‘private’.
Note: no ( )
accessor returns private
field value to outside world
mutator sets private field
from outside value
you don’t have
to define ‘value’
10

The previous example did not really do much. A useful
property would probably do some error checking prior
to setting the private value. For example:
...
set
{
if (value >= 0 && value <= 10)
myIntProperty = value;
else
throw new ArgumentoutOfRangeException
(‘MyIntProperty”, value, “Value must be
between 9 and 10”));
}
...
Last update: 7/16/2015
OO Programming
11

C# has a shortcut method to build properties that does
not require you to write all the code. For example:
public int MyIntProperty
{
get;
set;
}


This simplified syntax automatically generates the code
needed to access the private field myIntProperty.
If you use it, you must have both get and set
components and you can only access the field via the
property.
Last update: 7/16/2015
OO Programming
12
Methods can be overridden by using the override
keyword in the derived class.
 When you do this, you are effectively replacing the
method in the instance chain.
 You can also hide the base class method by the
derived class method by using the new keyword.
 This makes the derived method the primary one, but
still leaves the original base class method available
through the base class.

Last update: 7/16/2015
OO Programming
13

You use the override keyword in the derived method to
replace a base class function.
public class MyBaseClass
says that it may be overridden
{
public virtual void MyMethod()
{
// base class method
sets up inheritance
}
}
public class MyDerivedClass : MyBaseClass
{
public override void MyMethod()
must match
{
base class name
// override derived class method
}
}
override keyword
Last update: 7/16/2015
OO Programming
14

You use the override keyword in the derived method to
replace a base class function.
public class MyBaseClass
doesn’t have to be virtual to work
{
public virtual void MyMethod()
{
// base class method
}
}
public class MyDerivedClass : MyBaseClass
{
new public void MyMethod()
{
// hide base class method
}
}
‘new’ keyword makes it work
Last update: 7/16/2015
OO Programming
15
‘this’ is a special keyword in C#. When used within a
class member, this refers to an instance of the
object. Specifically, it refers to the current object.
 This means that you can’t use it in static members
since they are not part of any object instance.
 The most useful function of this is to pass a
reference to the current object instance to a
method.

public void MyMethod()
{
MyClass myObj = new MyClass();
myObj.AnotherMethod(this);
}
Last update: 7/16/2015
OO Programming
16

Interface members have the following characteristics:
 They cannot have any access modifiers (i.e., public, private,
protected, or internal). All are implicitly public.
 They cannot contain code bodies.
 They can’t define field members.
 The can’t be defined using the static, virtual, abstract, or
sealed keywords.
 Type definition members as not allowed.


You can define interface members with the new
keyword to hide members inherited from base
interfaces.
Properties can be defined and may contain either or
both of the get and set blocks. Automatic generation
also works.
Last update: 7/16/2015
OO Programming
17

A class that implements an interface must contain
implementations for all members of that interface.
public interface IMyInterface1
{
Note that the interface does not
void ActionOne();
have any code body.
void ActionTwo();
}
public class MyClass : IMyInterface
{
public void ActionOne()
Class that uses
{
interface must
// code goes here
implement all
}
members.
public void ActionTwo()
{
// code goes here
}
}
Last update: 7/16/2015
OO Programming
18



When classes contain many members, they become too
large to work with easily.
Partial class definitions allow you to split class
definitions across multiple files.
This can be used to hide code that is necessary, but not
pertinent to the main function of the class.
public partial
{
// code goes
}
...
public partial
{
// code goes
}
Last update: 7/16/2015
class MyClass
here for 1st part
class MyClass
here, 2nd part
OO Programming
Part of the class
definition goes here.
The rest of the class
definition goes here (in
another file).
19

Method definitions may also be separated into multiple
classes.

Specifically, partial classes may contain partial methods.

In this setup, one partial class defines a method without a
body and another partial class contains the method body.

Both method definitions must contain the word partial.

Partial methods can be static, but are always private
and cannot have a return value. Also, no parameters may
have the out modifier (although they can use ref).

Finally, they cannot use the virtual, abstract,
override, new, sealed, or extern modifier.
Last update: 7/16/2015
OO Programming
20
public partial class MyClass
{
partial void MyPartialMethod();
}
Header defined in
one partial class.
...
public partial class MyClass
{
partial void MyPartialMethod()
{
// the method body goes here
}
}
Last update: 7/16/2015
OO Programming
Body defined in
another partial class.
21





Fields, methods, and properties are types of class members that
can be defined within your classes.
C# contains ways to speed up the programming process.
Specifically, a technique called refactoring allows you to use prewritten pieces of code while automatic properties give you a
shortcut way to define basic properties.
Base class methods can be hidden using new keyword and can be
overridden (i.e., replaced) using the override keyword.
Interfaces can be defined using special syntax. These cause
classes that inherit them to conform to specific standards.
Partial classes and partial methods can be used to break the
program down into smaller pieces and compile more efficient
executables.
Last update: 7/16/2015
OO Programming
22