Opteronic

cs oop 006 Consructor with Different Parametyrs

Mar 30th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2. public class Collar
  3. {
  4.     // Static field, holding the current sequence value
  5.     private int c = 0;
  6.     // Intentionally deny instantiation of this class
  7.     public Collar()
  8.     {
  9.     }
  10. }
  11.  
  12. class Dog
  13. {
  14.     private string name;
  15.     private int age;
  16.     private double length;
  17.     private Collar collar;// Field name
  18.  
  19.  
  20.     // No parameters
  21.     public Dog()
  22.     {
  23.         this.name = "Sharo";
  24.         this.age = 1;
  25.         this.length = 0.3;
  26.         this.collar = new Collar();
  27.     }
  28.     // One parameter
  29.     public Dog(string name)
  30.     {
  31.         this.name = name;
  32.         this.age = 1;
  33.         this.length = 0.3;
  34.         this.collar = new Collar();
  35.     }
  36.     // Two parameters
  37.     public Dog(string name, int age)
  38.     {
  39.         this.name = name;
  40.         this.age = age;
  41.         this.length = 0.3;
  42.         this.collar = new Collar();
  43.     }
  44.     // Three parameters
  45.     public Dog(string name, int age, double length)
  46.     {
  47.         this.name = name;
  48.         this.age = age;
  49.         this.length = length;
  50.         this.collar = new Collar();
  51.     }
  52.     // Four parameters
  53.     public Dog(string name, int age, double length, Collar collar)
  54.     {
  55.         this.name = name;
  56.         this.age = age;
  57.         this.length = length;
  58.         this.collar = collar;
  59.     }
  60.  
  61.     static void Main()
  62.     {
  63.         Dog myDog = new Dog("Bobi", 2, 0.4); // Three parameters Constructors
  64.  
  65.         Console.WriteLine("My dog " + myDog.name +
  66.               " is " + myDog.age + " year(s) old. " +
  67.                     " and it has length: " + myDog.length + " m");
  68.         Console.ReadKey();
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment