/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A base class reference can refer to a derived class object.
using System;
class X {
public int a;
public X(int i) {
a = i;
}
}
class Y : X {
public int b;
public Y(int i, int j) : base(j) {
b = i;
}
}
public class BaseRef {
public static void Main() {
X x = new X(10);
X x2;
Y y = new Y(5, 6);
x2 = x; // OK, both of same type
Console.WriteLine("x2.a: " + x2.a);
x2 = y; // still Ok because Y is derived from X
Console.WriteLine("x2.a: " + x2.a);
// X references know only about X members
x2.a = 19; // OK
// x2.b = 27; // Error, X doesn't have a b member
}
}
Class Interface
A simple class hierarchy
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple class hierarchy.
using System;
public class Shapes {
public static void Main() {
Triangle t1 = new Triangle();
Triangle t2 = new Triangle();
t1.width = 4.0;
t1.height = 4.0;
t1.style = "isosceles";
t2.width = 8.0;
t2.height = 12.0;
t2.style = "right";
Console.WriteLine("Info for t1: ");
t1.showStyle();
t1.showDim();
Console.WriteLine("Area is " + t1.area());
Console.WriteLine();
Console.WriteLine("Info for t2: ");
t2.showStyle();
t2.showDim();
Console.WriteLine("Area is " + t2.area());
}
}
// A class for two-dimensional objects.
public class TwoDShape {
public double width;
public double height;
public void showDim() {
Console.WriteLine("Width and height are " +
width + " and " + height);
}
}
// Triangle is derived from TwoDShape.
public class Triangle : TwoDShape {
public string style; // style of triangle
// Return area of triangle.
public double area() {
return width * height / 2;
}
// Display a triangle's style.
public void showStyle() {
Console.WriteLine("Triangle is " + style);
}
}
Class Hierarchy with two children class
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
class Window
{
// constructor takes two integers to
// fix location on the console
public Window(int top, int left)
{
this.top = top;
this.left = left;
}
// simulates drawing the window
public virtual void DrawWindow()
{
Console.WriteLine(“Window: drawing Window at {0}, {1}”,
top, left);
}
// these members are protected and thus visible
// to derived class methods. We'll examine this
// later in the chapter
protected int top;
protected int left;
}
// ListBox derives from Window
class ListBox : Window
{
// constructor adds a parameter
public ListBox(
int top,
int left,
string contents):
base(top, left) // call base constructor
{
listBoxContents = contents;
}
// an overridden version (note keyword) because in the
// derived method we change the behavior
public override void DrawWindow()
{
base.DrawWindow(); // invoke the base method
Console.WriteLine (“Writing string to the listbox: {0}”,
listBoxContents);
}
private string listBoxContents; // new member variable
}
class Button : Window
{
public Button(
int top,
int left):
base(top, left)
{
}
// an overridden version (note keyword) because in the
// derived method we change the behavior
public override void DrawWindow()
{
Console.WriteLine(“Drawing a button at {0}, {1}
“,
top, left);
}
}
public class TesterClassArray1
{
static void Main()
{
Window win = new Window(1,2);
ListBox lb = new ListBox(3,4,”Stand alone list box”);
Button b = new Button(5,6);
win.DrawWindow();
lb.DrawWindow();
b.DrawWindow();
Window[] winArray = new Window[3];
winArray[0] = new Window(1,2);
winArray[1] = new ListBox(3,4,”List box in array”);
winArray[2] = new Button(5,6);
for (int i = 0;i < 3; i++) { winArray[i].DrawWindow(); } } } [/csharp]
Class Hierarchy test
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
class Window
{
// constructor takes two integers to
// fix location on the console
public Window(int top, int left)
{
this.top = top;
this.left = left;
}
// simulates drawing the window
public void DrawWindow()
{
Console.WriteLine("Drawing Window at {0}, {1}",
top, left);
}
// these members are private and thus invisible
// to derived class methods; we'll examine this
// later in the chapter
private int top;
private int left;
}
// ListBox derives from Window
class ListBox : Window
{
// constructor adds a parameter
public ListBox(
int top,
int left,
string theContents):
base(top, left) // call base constructor
{
mListBoxContents = theContents;
}
// a new version (note keyword) because in the
// derived method we change the behavior
public new void DrawWindow()
{
base.DrawWindow(); // invoke the base method
Console.WriteLine ("Writing string to the listbox: {0}",
mListBoxContents);
}
private string mListBoxContents; // new member variable
}
public class HierarchyTester
{
public static void Main()
{
// create a base instance
Window w = new Window(5,10);
w.DrawWindow();
// create a derived instance
ListBox lb = new ListBox(20,30,"Hello world");
lb.DrawWindow();
}
}
Illustrates versioning
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example7_5.cs illustrates versioning
*/
using System;
// declare the MotorVehicle class
class MotorVehicle
{
// declare the fields
public string make;
public string model;
// define a constructor
public MotorVehicle(string make, string model)
{
this.make = make;
this.model = model;
}
// define the Accelerate() method
public virtual void Accelerate()
{
Console.WriteLine("In MotorVehicle Accelerate() method");
Console.WriteLine(model + " accelerating");
}
}
// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{
// define a constructor
public Car(string make, string model) :
base(make, model)
{
// do nothing
}
// define the Accelerate() method (uses the new keyword to
// tell the compiler a new method is to be defined)
public new void Accelerate()
{
Console.WriteLine("In Car Accelerate() method");
Console.WriteLine(model + " accelerating");
}
}
public class Example7_5
{
public static void Main()
{
// create a Car object
Console.WriteLine("Creating a Car object");
Car myCar = new Car("Toyota", "MR2");
// call the Car object's Accelerate() method
Console.WriteLine("Calling myCar.Accelerate()");
myCar.Accelerate();
}
}
Private field and public Property in inheritance
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
namespace nsInherit
{
using System;
public class clsMainInherit
{
static public void Main ()
{
clsDerived derived = new clsDerived();
derived.Property = 42;
derived.ShowField();
}
}
//
// Define a base class with a private field and a public Property
class clsBase
{
private int m_Field;
public int Property
{
get {return (m_Field);}
set {m_Field = value;}
}
public void ShowField ()
{
Console.WriteLine ("The value of m_Field is " + m_Field);
}
}
//
// Define a derived class that inherits from the clsBase
class clsDerived : clsBase
{
// For now, the derived class needs no members
}
}
illustrates inheritance
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example7_1.cs illustrates inheritance
*/
using System;
// declare the MotorVehicle class (the base class)
class MotorVehicle
{
// declare the fields
public string make;
public string model;
// define a constructor
public MotorVehicle(string make, string model)
{
this.make = make;
this.model = model;
}
// define a method
public void Start()
{
Console.WriteLine(model + " started");
}
}
// declare the Car class (derived from the MotorVehicle base class)
class Car : MotorVehicle
{
// declare an additional field
public bool convertible;
// define a constructor
public Car(string make, string model, bool convertible) :
base(make, model) // calls the base class constructor
{
this.convertible = convertible;
}
}
// declare the Motorcycle class (derived from the MotorVehicle base class)
class Motorcycle : MotorVehicle
{
// declare an additional field
public bool sidecar;
// define a constructor
public Motorcycle(string make, string model, bool sidecar) :
base(make, model) // calls the base class constructor
{
this.sidecar = sidecar;
}
// define an additional method
public void PullWheelie()
{
Console.WriteLine(model + " pulling a wheelie!");
}
}
public class Example7_1
{
public static void Main()
{
// declare a Car object, display the object's fields, and call the
// Start() method
Car myCar = new Car("Toyota", "MR2", true);
Console.WriteLine("myCar.make = " + myCar.make);
Console.WriteLine("myCar.model = " + myCar.model);
Console.WriteLine("myCar.convertible = " + myCar.convertible);
myCar.Start();
// declare a Motorcycle object, display the object's fields, and call the
// Start() method
Motorcycle myMotorcycle = new Motorcycle("Harley-Davidson", "V-Rod", false);
Console.WriteLine("myMotorcycle.make = " + myMotorcycle.make);
Console.WriteLine("myMotorcycle.model = " + myMotorcycle.model);
Console.WriteLine("myMotorcycle.sidecar = " + myMotorcycle.sidecar);
myMotorcycle.Start();
myMotorcycle.PullWheelie();
}
}