/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple inventory example.
using System;
using System.Collections;
class Inventory {
string name;
double cost;
int onhand;
public Inventory(string n, double c, int h) {
name = n;
cost = c;
onhand = h;
}
public override string ToString() {
return
String.Format("{0,-10}Cost: {1,6:C} On hand: {2}",
name, cost, onhand);
}
}
public class InventoryList {
public static void Main() {
ArrayList inv = new ArrayList();
// Add elements to the list
inv.Add(new Inventory("Pliers", 5.95, 3));
inv.Add(new Inventory("Wrenches", 8.29, 2));
inv.Add(new Inventory("Hammers", 3.50, 4));
inv.Add(new Inventory("Drills", 19.88, 8));
Console.WriteLine("Inventory list:");
foreach(Inventory i in inv) {
Console.WriteLine(" " + i);
}
}
}
Class Interface
Using Initializers
public class Product {
public string make = "Ford";
public string model = "T";
public string color; // default value of null
public int yearBuilt = 1910;
public void Start() {
System.Console.WriteLine(model + " started");
}
public void Stop() {
System.Console.WriteLine(model + " stopped");
}
}
class MainClass {
public static void Main() {
Product myProduct = new Product();
System.Console.WriteLine("myProduct.make = " + myProduct.make);
System.Console.WriteLine("myProduct.model = " + myProduct.model);
if (myProduct.color == null) {
System.Console.WriteLine("myProduct.color is null");
}
System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);
}
}
simulate a bank account
using System;
public class BankAccount {
public static int nNextAccountNumber = 1000;
public int nAccountNumber;
public double dBalance;
public void InitBankAccount() {
nAccountNumber = ++nNextAccountNumber;
dBalance = 0.0;
}
public void Deposit(double dAmount) {
if (dAmount > 0.0) {
dBalance += dAmount;
}
}
public double Withdraw(double dWithdrawal) {
if (dBalance <= dWithdrawal) {
dWithdrawal = dBalance;
}
dBalance -= dWithdrawal;
return dWithdrawal;
}
}
[/csharp]
Illustrates the use of various access modifiers
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_10.cs illustrates the use of various
access modifiers
*/
// declare the Car class
class Car
{
// declare the fields
public string make;
protected internal string model;
internal string color;
protected int horsepower = 150;
private int yearBuilt;
// define the methods
public void SetYearBuilt(int yearBuilt)
{
this.yearBuilt = yearBuilt;
}
public int GetYearBuilt()
{
return yearBuilt;
}
public void Start()
{
System.Console.WriteLine("Starting car ...");
TurnStarterMotor();
System.Console.WriteLine("Car started");
}
private void TurnStarterMotor()
{
System.Console.WriteLine("Turning starter motor ...");
}
}
public class Example5_10
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
// assign values to the Car object fields
myCar.make = "Toyota";
myCar.model = "MR2";
myCar.color = "black";
// myCar.horsepower = 200; // protected field not accessible
// myCar.yearBuilt = 1995; // private field not accessible
// call the SetYearBuilt() method to set the private yearBuilt field
myCar.SetYearBuilt(1995);
// display the values for the Car object fields
System.Console.WriteLine("myCar.make = " + myCar.make);
System.Console.WriteLine("myCar.model = " + myCar.model);
System.Console.WriteLine("myCar.color = " + myCar.color);
// call the GetYearBuilt() method to get the private yearBuilt field
System.Console.WriteLine("myCar.GetYearBuilt() = " + myCar.GetYearBuilt());
// call the Start() method
myCar.Start();
// myCar.TurnStarterMotor(); // private method not accessible
}
}
illustrates member hiding
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example7_4.cs illustrates member hiding
*/
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)
{
Console.WriteLine("In MotorVehicle constructor");
this.make = make;
this.model = model;
Console.WriteLine("this.make = " + this.make);
Console.WriteLine("this.model = " + this.model);
}
// define the DisplayModel() method
public void DisplayModel()
{
Console.WriteLine("In MotorVehicle DisplayModel() method");
Console.WriteLine("model = " + model);
}
}
// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{
// hide the base class model field
public new string model;
// define a constructor
public Car(string make, string model) :
base(make, "Test")
{
Console.WriteLine("In Car constructor");
this.model = model;
Console.WriteLine("this.model = " + this.model);
}
// hide the base class DisplayModel() method
public new void DisplayModel()
{
Console.WriteLine("In Car DisplayModel() method");
Console.WriteLine("model = " + model);
base.DisplayModel(); // calls DisplayModel() in the base class
}
}
public class Example7_4
{
public static void Main()
{
// create a Car object
Console.WriteLine("Creating a Car object");
Car myCar = new Car("Toyota", "MR2");
Console.WriteLine("Back in Main() method");
Console.WriteLine("myCar.make = " + myCar.make);
Console.WriteLine("myCar.model = " + myCar.model);
// call the Car object's DisplayModel() method
Console.WriteLine("Calling myCar.DisplayModel()");
myCar.DisplayModel();
}
}
illustrates member accessibility
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example7_3.cs illustrates member accessibility
*/
using System;
// declare the MotorVehicle class
class MotorVehicle
{
// declare the fields
private string make;
protected string model;
// define a constructor
public MotorVehicle(string make, string model)
{
this.make = make;
this.model = model;
}
// define the Start() method (may be overridden in a
// derived class)
public virtual void Start()
{
TurnStarterMotor();
System.Console.WriteLine("Vehicle started");
}
// define the TurnStarterMotor() method
private void TurnStarterMotor()
{
System.Console.WriteLine("Turning starter motor...");
}
}
// declare the Car class (derived from MotorVehicle)
class Car : MotorVehicle
{
// define a constructor
public Car(string make, string model) :
base(make, model)
{
// do nothing
}
// override the base class Start() method
public override void Start()
{
Console.WriteLine("Starting " + model); // model accessible
base.Start(); // calls the Start() method in the base class
// Console.WriteLine("make = " + make); // make is not accessible
}
}
public class Example7_3
{
public static void Main()
{
// create a Car object and call the object's Accelerate() method
Car myCar = new Car("Toyota", "MR2");
myCar.Start();
// make and model are not accessible, so the following two lines
// are commented out
// Console.WriteLine("myCar.make = " + myCar.make);
// Console.WriteLine("myCar.model = " + myCar.model);
}
}
Public vs private access
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Public vs private access.
using System;
class MyClass {
private int alpha; // private access explicitly specified
int beta; // private access by default
public int gamma; // public access
/* Methods to access alpha and beta. It is OK for a
member of a class to access a private member
of the same class.
*/
public void setAlpha(int a) {
alpha = a;
}
public int getAlpha() {
return alpha;
}
public void setBeta(int a) {
beta = a;
}
public int getBeta() {
return beta;
}
}
public class AccessDemo {
public static void Main() {
MyClass ob = new MyClass();
/* Access to alpha and beta is allowed only
through methods. */
ob.setAlpha(-99);
ob.setBeta(19);
Console.WriteLine("ob.alpha is " + ob.getAlpha());
Console.WriteLine("ob.beta is " + ob.getBeta());
// You cannot access alpha or beta like this:
// ob.alpha = 10; // Wrong! alpha is private!
// ob.beta = 9; // Wrong! beta is private!
// It is OK to directly access gamma because it is public.
ob.gamma = 99;
}
}