【C#】上机实验三

 

实验1:

定义一个 TimePeiod 类

包含: hour , minute , second 

实现时间的在 时分秒上的加法。


 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace MyProject 7 { 8 class Program 9  {10 static void Main(string[] args)11  {12 // 显示"08:47:00"13 TimePeriod time = new TimePeriod(8, 47, 0);14  Console.WriteLine( time );15 16 // 显示"12:00:00"17 time.Hour = 10;18 time.Minute = 58;19 time.Second = 59;20 time.Add_Hour(1);21 time.Add_Minute(1);22 time.Add_Second(1);23  Console.WriteLine( time );24 25 // 显示"01:02:03"26 time.Hour = time.Minute = time.Second = 0;27 time.Add_Second( 3600 + 120 + 3 );28  Console.WriteLine(time);29 30 // 显示"Day + 1 , 01:02:03"31 time.Hour = time.Minute = time.Second = 0;32 time.Add_Second(25 * 3600 + 120 + 3);33  Console.WriteLine(time);34  }35  }36 class TimePeriod37  {38 //类中定义 时,分,秒 3个私有字段39 private int hour;40 private int minute;41 private int second;42 43 //类中定义3个公有属性 分别对应上述3个字段44 public int Hour { get { return hour; } set { hour = value; } }45 public int Minute { get { return minute; } set { minute = value; } }46 public int Second { get { return second; } set { second = value; } }47 48 //类中定义3个公有方法分别将时间增加n秒,n分,n时.49 public void Add_Hour( int hour)50  {51 this.hour += hour ;52 if( this.hour >= 24)53  {54 Console.Write( "Day + {0} ," , this.hour/24 );55 this.hour %= 24;56  }57  }58 public void Add_Minute( int minute)59  {60 this.minute += minute;61 if( this.minute >= 60)62  {63 Add_Hour(this.minute / 60);64 this.minute %= 60;65  }66  }67 public void Add_Second( int second)68  {69 this.second += second;70 if( this.second >= 60)71  {72 Add_Minute(this.second / 60);73 this.second %= 60;74  }75  }76 77 //类中定义3个参数的构造函数,初始化3个私有字段78 public TimePeriod( int hour , int minute, int second)79  {80 this.hour = hour;81 this.minute = minute;82 this.second = second;83  }84 public TimePeriod() : this(0, 0, 0) { }85 86 //类中定义公有方法输出:时,分,秒,3个字段的值87 public override string ToString()88  {89 return string.Format( "{0:d2}:{1:d2}:{2:d2}",hour,minute,second);90  }91  }92 }

时间类的练习

 

实验2:

实现Shape类,以及Box类的继承以及多态的练习


 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace MyProject 7 { 8 class Program 9  { 10 static void Main(string[] args) 11  { 12 #region 测试Shape类及方法 13 /* 1、默认构造函数 对Length,breadth进行赋值 14  * 2、利用属性进行输出后,再利用属性进行赋值 15  * 3、最后利用ToString 输出对应的对象信息  16 */ 17 #endregion 18  19 Shape s1 = new Shape(1, 2); 20 Console.WriteLine("s1->Length:{0} , breadth:{1}",s1.Length,s1.Breadth); 21 s1.Length = 3; 22 s1.Breadth = 4; 23  Console.WriteLine(s1); 24  25 #region 测试Box类及方法 26 /* 1、默认构造函数 对Length,breadth,height进行赋值 27  * 2、利用属性进行输出后,再利用属性进行赋值 28  * 3、最后利用ToString 输出对应的对象信息  29 */ 30 #endregion 31  32 Box b1 = new Box(1, 2, 3); 33 Console.WriteLine("b1->Length :{0}\tbreadth :{1}\theight :{2}",b1.Length,b1.Breadth,b1.Height); 34 b1.Length = 3; 35 b1.Breadth = 4; 36 b1.Height = 5; 37  Console.WriteLine(b1); 38  39 #region 测试多态 40 /* 41  * 多态体现在,父指针,将子类实例化. 42 */ 43 #endregion 44 Shape b2 = new Box(2, 3, 4); 45  Console.WriteLine(b2); 46  47  } 48  } 49  50 public class Shape 51  { 52 private int length; 53 private int breadth; 54  55 public int Length { get => length; set => length = value; } 56 public int Breadth { get => breadth; set => breadth = value; } 57  58 public Shape ( int length , int width) 59  { 60 this.Length = length; 61 this.Breadth = Breadth;  62  } 63 public Shape():this(0, 0) { } 64 public override string ToString() 65  { 66 return string.Format("Shape -> length:{0}\t Breadth:{1}", length, Breadth); 67  } 68 #region 主要注释 69 /* 70  * 类中的字段属性封装 71  * 构造函数 : 两参数,零参数 72  * 重载ToString类 73 */ 74 #endregion 75  } 76  77 public class Box : Shape 78  { 79 private int height; 80 public int Height { get => height; set => height = value; } 81 public Box(int length, int Breadth, int height):base(length, Breadth) 82  { 83 this.height = height; 84  } 85 public Box() : this(0, 0, 0) { } 86 public override string ToString() 87  { 88 return string.Format("Box -> length:{0}\t breadth:{1}\t height:{2}", Length, Breadth, height); 89  } 90 #region Box类 91 /* 92  * Box继承了Shape的所有结构 93  * 94  * 1、无法直接访问父类的字段,但可通过"属性"间接访问 95  * 2、在重载 构造函数时,可以直接调用父类的构造函数实现部分字段的赋值 96  *  97 */ 98 #endregion 99  }100 }

类及继承练习

 

相关文章