Call class methods 2


   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 public class MyTime1 {
     // private member variables
     int year;
     int month;
     int date;
     int hour;
     int minute;
     int second;

     // public method
     public void DisplayCurrentTime()
     {
         System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
             month, date, year, hour, minute, second);
     }

     // constructor
     public MyTime1(int theYear, int theMonth, int theDate,
         int theHour, int theMinute, int theSecond)
     {
         year = theYear;
         month = theMonth;
         date = theDate;
         hour = theHour;
         minute = theMinute;
         second = theSecond;
     }
     static void Main()
     {
         MyTime1 timeObject = new MyTime1(2005,3,25,9,35,20);
         timeObject.DisplayCurrentTime();
     }

 }



           
          


Class a class method


   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

 using System;

 class MyClass
 {
     public void SomeMethod(int firstParam, float secondParam)
     {
         Console.WriteLine(
             "Here are the parameters received: {0}, {1}",
             firstParam, secondParam);
     }

 }

 public class Tester111
 {
     static void Main()
     {
         int howManyPeople = 5;
         float pi = 3.14f;
         MyClass mc = new MyClass();
         mc.SomeMethod(howManyPeople, pi);
     }
 }
           
          


Define methods that return a value and accept parameters


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example5_3.cs illustrates how to define methods
  that return a value and accept parameters
*/


// declare the Car class
class Car
{

  public int yearBuilt;
  public double maximumSpeed;

  // the Age() method calculates and returns the
  // age of the car in years
  public int Age(int currentYear)
  {
    int age = currentYear - yearBuilt;
    return age;
  }

  // the Distance() method calculates and returns the
  // distance traveled by the car, given its initial speed,
  // maximum speed, and time for the journey
  // (assuming constant acceleration of the car)
  public double Distance(double initialSpeed, double time)
  {
    return (initialSpeed + maximumSpeed) / 2 * time;
  }

}


public class Example5_3
{

  public static void Main()
  {

    // declare a Car object reference and
    // create a Car object
    System.Console.WriteLine("Creating a Car object and " +
      "assigning its memory location to redPorsche");
    Car redPorsche = new Car();

    // assign values to the fields
    redPorsche.yearBuilt = 2000;
    redPorsche.maximumSpeed = 150;

    // call the methods
    int age = redPorsche.Age(2001);
    System.Console.WriteLine("redPorsche is " + age + " year old.");
    System.Console.WriteLine("redPorsche travels " +
      redPorsche.Distance(31, .25) + " miles.");

  }

}

           
          


Method Attributes

   
 

using System;
using System.Reflection;
public class TransactionableAttribute : Attribute {
    public TransactionableAttribute() {
    }
}

class SomeClass {
    [Transactionable]
    public void Foo() { }

    public void Bar() { }

    [Transactionable]
    public void Goo() { }
}

class Test {
    [STAThread]
    static void Main(string[] args) {
        Type type = Type.GetType("SomeClass");
        foreach (MethodInfo method in type.GetMethods()) {
            foreach (Attribute attr in
                method.GetCustomAttributes(true)) {
                if (attr is TransactionableAttribute) {
                    Console.WriteLine(method.Name);
                }
            }
        }
    }
}

    


Build a derived class of Vehicle for trucks


   

/*
C# A Beginner's Guide
By Schildt

Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/

/* 
  Project 8-1 
 
  Build a derived class of Vehicle for trucks. 
*/ 
using System; 
 
class Vehicle {    
  int pri_passengers; // number of passengers    
  int pri_fuelcap;    // fuel capacity in gallons   
  int pri_mpg;        // fuel consumption in miles per gallon   
   
  // This is a constructor for Vehicle.  
  public Vehicle(int p, int f, int m) {  
    passengers = p;  
    fuelcap = f;  
    mpg = m;  
  }  
 
  // Return the range.   
  public int range() {   
    return mpg * fuelcap;   
  }   
   
  // Compute fuel needed for a given distance.  
  public double fuelneeded(int miles) {   
    return (double) miles / mpg;   
  } 
 
  // Properties 
  public int passengers { 
    get { return pri_passengers; } 
    set { pri_passengers = value; } 
  }   
 
  public int fuelcap { 
    get { return pri_fuelcap; } 
    set { pri_fuelcap = value; } 
  }   
 
  public int mpg { 
    get { return pri_mpg; } 
    set { pri_mpg = value; } 
  }   
}    
  
// Use Vehicle to create a Truck specialization.    
class Truck : Vehicle {  
  int pri_cargocap; // cargo capacity in pounds  
  
  // This is a constructor for Truck.  
  public Truck(int p, int f, int m, int c) : base(p, f, m)  
  {  
    cargocap = c;  
  }  
 
  // Property for cargocap. 
  public int cargocap { 
    get { return pri_cargocap; } 
    set { pri_cargocap = value; } 
  }   
}  
    
public class TruckDemo {    
  public static void Main() {    
  
    // construct some trucks 
    Truck semi = new Truck(2, 200, 7, 44000);    
    Truck pickup = new Truck(3, 28, 15, 2000);    
    double gallons;   
    int dist = 252;   
   
    gallons = semi.fuelneeded(dist);    
    
    Console.WriteLine("Semi can carry " + semi.cargocap +  
                       " pounds."); 
    Console.WriteLine("To go " + dist + " miles semi needs " +   
                       gallons + " gallons of fuel.
");   
       
    gallons = pickup.fuelneeded(dist);    
    
    Console.WriteLine("Pickup can carry " + pickup.cargocap +  
                       " pounds."); 
    Console.WriteLine("To go " + dist + " miles pickup needs " +   
                       gallons + " gallons of fuel.");  
  }    
}


           
          


a multilevel hierarchy


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


/*  In a multilevel hierarchy, the  
    first override of a virtual method 
    that is found while moving up the 
    heirarchy is the one executed. */ 
  
using System;  
  
class Base {  
  // Create virtual method in the base class.   
  public virtual void who() {  
    Console.WriteLine("who() in Base");  
  }  
}  
  
class Derived1 : Base {  
  // Override who() in a derived class.  
  public override void who() {  
    Console.WriteLine("who() in Derived1");  
  }  
}  
  
class Derived2 : Derived1 {  
  // This class also does not override who().  
}  
 
class Derived3 : Derived2 {  
  // This class does not override who().  
}  
 
public class NoOverrideDemo2 {  
  public static void Main() {  
    Derived3 dOb = new Derived3();  
    Base baseRef; // a base-class reference  
  
    baseRef = dOb;   
    baseRef.who(); // calls Derived1's who()  
  }  
}


           
          


Pass a derived class reference to a base class reference


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Pass a derived class reference to a base class reference. 
 
using System; 
 
class TwoDShape { 
  double pri_width;  // private 
  double pri_height; // private 
 
  // Default constructor. 
  public TwoDShape() { 
    width = height = 0.0; 
  } 
 
  // Constructor for TwoDShape. 
  public TwoDShape(double w, double h) { 
    width = w; 
    height = h; 
  } 
 
  // Construct object with equal width and height. 
  public TwoDShape(double x) { 
    width = height = x; 
  } 
 
  // Construct object from an object. 
  public TwoDShape(TwoDShape ob) { 
    width = ob.width; 
    height = ob.height; 
  } 
 
  // Properties for width and height. 
  public double width { 
     get { return pri_width; } 
     set { pri_width = value; } 
  } 
 
  public double height { 
     get { return pri_height; } 
     set { pri_height = value; } 
  } 
 
  public void showDim() { 
    Console.WriteLine("Width and height are " + 
                       width + " and " + height); 
  } 
} 
 
// A derived class of TwoDShape for triangles. 
class Triangle : TwoDShape { 
  string style; // private 
   
  // A default constructor. 
  public Triangle() { 
    style = "null"; 
  } 
 
  // Constructor for Triangle. 
  public Triangle(string s, double w, double h) : base(w, h) { 
    style = s;  
  } 
 
  // Construct an isosceles triangle. 
  public Triangle(double x) : base(x) { 
    style = "isosceles";  
  } 
 
  // Construct an object from an object. 
  public Triangle(Triangle ob) : base(ob) { 
    style = ob.style; 
  } 
 
  // Return area of triangle. 
  public double area() { 
    return width * height / 2; 
  } 
 
  // Display a triangle's style. 
  public void showStyle() { 
    Console.WriteLine("Triangle is " + style); 
  } 
} 
 
public class Shapes7 { 
  public static void Main() { 
    Triangle t1 = new Triangle("right", 8.0, 12.0); 
 
    // make a copy of t1 
    Triangle t2 = new Triangle(t1); 
 
    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()); 
  } 
}