Advertisement
ivan_yosifov

Cat_Class_Example

Nov 14th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2.  
  3. public class Cat
  4. {
  5.     // Fields
  6.     private string name;
  7.     private string color;
  8.  
  9.     // Properties
  10.     public string Name
  11.     {
  12.         get
  13.         {
  14.             return this.name;
  15.         }
  16.         set
  17.         {
  18.             this.name = value;
  19.         }
  20.     }
  21.     public string Color
  22.     {
  23.         get
  24.         {
  25.             return this.color;
  26.         }
  27.         set
  28.         {
  29.             this.color = value;
  30.         }
  31.     }
  32.  
  33.     // Default (parameterless) constructor
  34.     public Cat()
  35.     {
  36.         this.name = "Unnamed";
  37.         this.color = "gray";
  38.     }
  39.  
  40.     // Constructor with parameters
  41.     public Cat(string name, string color)
  42.     {
  43.         this.name = name;
  44.         this.color = color;
  45.     }
  46.  
  47.     // Method
  48.     public void SayMiau()
  49.     {
  50.         Console.WriteLine("Cat {0} said: Miauuuuuu!", name);
  51.     }
  52. }
  53. class Program
  54. {
  55.     static void Main()
  56.     {
  57.         Cat someCat = new Cat();
  58.         someCat.SayMiau();
  59.         Console.WriteLine("The color of cat {0} is {1}", someCat.Name,someCat.Color);
  60.  
  61.         Console.WriteLine();
  62.  
  63.         Cat myCat = new Cat("Johnny", "brown");
  64.         myCat.SayMiau();
  65.         Console.WriteLine("The color of cat {0} is {1}", myCat.Name, myCat.Color);
  66.  
  67.         Console.WriteLine();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement