/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example6_5.cs illustrates how to use a "has a" relationship */ // declare the Engine class class Engine { // declare the fields public int cylinders; public int horsepower; // define the method public void Start() { System.Console.WriteLine("Engine started"); } } // declare the Car class class Car { // declare the fields public string make; public Engine engine; // Car has an Engine // define the method public void Start() { engine.Start(); } } public class Example6_5 { public static void Main() { // declare a Car object reference named myCar System.Console.WriteLine("Creating a Car object"); Car myCar = new Car(); myCar.make = "Toyota"; // Car objects have an Engine object System.Console.WriteLine("Creating an Engine object"); myCar.engine = new Engine(); myCar.engine.cylinders = 4; myCar.engine.horsepower = 180; // display the values for the Car and Engine object fields System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.engine.cylinders = " + myCar.engine.cylinders); System.Console.WriteLine("myCar.engine.horsepower = " + myCar.engine.horsepower); // call the Car object's Start() method myCar.Start(); } }
Class Interface
Illustrates how to assign default values to fields using initializers
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_2.cs illustrates how to assign default values to fields using initializers */ // declare the Car class class Car { // declare the fields public string make = "Ford"; public string model = "T"; public string color; // default value of null public int yearBuilt = 1910; // define the methods public void Start() { System.Console.WriteLine(model + " started"); } public void Stop() { System.Console.WriteLine(model + " stopped"); } } public class Example5_2 { public static void Main() { // create a Car object Car myCar = new Car(); // display the default values for the Car object fields System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.model = " + myCar.model); if (myCar.color == null) { System.Console.WriteLine("myCar.color is null"); } System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); } }
Illustrates how to declare classes, object references, and create objects
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_1.cs illustrates how to declare classes, object references, and create objects */ // declare the Car class class Car { // declare the fields public string make; public string model; public string color; public int yearBuilt; // define the methods public void Start() { System.Console.WriteLine(model + " started"); } public void Stop() { System.Console.WriteLine(model + " stopped"); } } public class Example5_1 { public static void Main() { // declare a Car object reference named myCar Car myCar; // create a Car object, and assign its address to myCar System.Console.WriteLine("Creating a Car object and assigning " + "its memory location to myCar"); myCar = new Car(); // assign values to the Car object's fields using myCar myCar.make = "Toyota"; myCar.model = "MR2"; myCar.color = "black"; myCar.yearBuilt = 1995; // display the field values using myCar System.Console.WriteLine("myCar details:"); System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.model = " + myCar.model); System.Console.WriteLine("myCar.color = " + myCar.color); System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); // call the methods using myCar myCar.Start(); myCar.Stop(); // declare another Car object reference and // create another Car object System.Console.WriteLine("Creating another Car object and " + "assigning its memory location to redPorsche"); Car redPorsche = new Car(); redPorsche.make = "Porsche"; redPorsche.model = "Boxster"; redPorsche.color = "red"; redPorsche.yearBuilt = 2000; System.Console.WriteLine("redPorsche is a " + redPorsche.model); // change the object referenced by the myCar object reference // to the object referenced by redPorshe System.Console.WriteLine("Assigning redPorsche to myCar"); myCar = redPorsche; System.Console.WriteLine("myCar details:"); System.Console.WriteLine("myCar.make = " + myCar.make); System.Console.WriteLine("myCar.model = " + myCar.model); System.Console.WriteLine("myCar.color = " + myCar.color); System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); // assign null to myCar (myCar will no longer reference an object) myCar = null; } }
Declare class and use it
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ using System; class Rect { public int width; public int height; public Rect(int w, int h) { width = w; height = h; } public int area() { return width * height; } } public class UseRect { public static void Main() { Rect r1 = new Rect(4, 5); Rect r2 = new Rect(7, 9); Console.WriteLine("Area of r1: " + r1.area()); Console.WriteLine("Area of r2: " + r2.area()); } }
Assign value to class
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // This program will not compile. class X { int a; public X(int i) { a = i; } } class Y { int a; public Y(int i) { a = i; } } public class IncompatibleRef { public static void Main() { X x = new X(10); X x2; Y y = new Y(5); x2 = x; // OK, both of same type x2 = y; // Error, not of same type } }
Declaring Class Instances
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class DeclaringClassInstances { static void Main(string[] args) { ClassInstantied MyClass = new ClassInstantied(); } } class ClassInstantied { public void Display() { Console.WriteLine("Hello World"); } } }
Declaring and Defining Classes
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class DeclaringandDefiningClasses { static private int MyInt = 5; static public int MyInt2 = 10; static public int[] MyIntArray; static private int ObjectCount = 0; static void Main(string[] args) { MyIntArray = new int[10]; ObjectCount++; } public static int MyMethod(int myInt) { MyInt = MyInt + myInt; return MyInt; } private static long MyLongMethod(ref int myInt) { return myInt; } } }