在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、虚方法练习
设计一个控制台应用程序,定义一个Shape类,具体要求如下: (1)类中定义2个私有字段长度(length)、宽度(breadth)。 (2)类中定义相应公有属性分别对应上述2个字段; (3)类中定义可重载的构造函数初始化上述2个字段; (4)定义1个默认构造函数; (5)类中定义公有方法输出对象的长度、宽度等详细信息; (6)类中定义虚方法Draw,输出当前图型类别。 (7)在main方法中测试Shape类及方法。 定义一个Box类,父类为Shape,具体要求如下: (1)类中定义3个私有字段长度(length)、宽度(breadth)、高度(height); (2)类中定义相应公有属性分别对应上述3个字段; (3)类中定义可重载的构造函数初始化上述3个字段; (4)定义1个默认构造函数; (5)类中定义公有方法输出对象的长度、宽度、高度等详细信息; (6)类中定义虚方法Draw,输出当前图型类别。 (7)在main方法中测试Box类及方法; (8)在main方法中测试多态。
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 #region 测试虚函数 49 #endregion 50 s1.Draw(); 51 b1.Draw(); 52 } 53 } 54 55 public class Shape 56 { 57 private int length; 58 private int breadth; 59 60 public int Length 61 { 62 get { return length; } 63 set { length = value; } 64 } 65 public int Breadth 66 { 67 get { return breadth; } 68 set { breadth = value; } 69 } 70 71 public Shape ( int length , int width) 72 { 73 this.Length = length; 74 this.Breadth = Breadth; 75 } 76 public Shape():this(0, 0) { } 77 public override string ToString() 78 { 79 return string.Format("Shape -> length:{0}\t Breadth:{1}", Length, Breadth); 80 } 81 public virtual void Draw() 82 { 83 Console.WriteLine(" Draw -> Shape " + "Length :{0} , Breadth :{1} ",Length , Breadth ); 84 } 85 #region 主要注释 86 /* 87 * 类中的字段属性封装 88 * 构造函数 : 两参数,零参数 89 * 重载ToString类 90 */ 91 #endregion 92 } 93 94 public class Box : Shape 95 { 96 private int height; 97 98 public int Height 99 { 100 get { return height; } 101 set { height = value; } 102 } 103 104 public Box(int length, int Breadth, int height):base(length, Breadth) 105 { 106 this.height = height; 107 } 108 public Box() : this(0, 0, 0) { } 109 public override string ToString() 110 { 111 return string.Format("Box -> length:{0}\t breadth:{1}\t height:{2}", Length, Breadth, Height); 112 } 113 public override void Draw() 114 { 115 Console.WriteLine(" Draw -> Shape " + "Length :{0} , Breadth :{1} , Height :{2}", Length, Breadth,Height); 116 } 117 #region Box类 118 /* 119 * Box继承了Shape的所有结构 120 * 121 * 1、无法直接访问父类的字段,但可通过"属性"间接访问 122 * 2、在重载 构造函数时,可以直接调用父类的构造函数实现部分字段的赋值 123 * 124 */ 125 #endregion 126 } 127 }
2、IComparable接口练习 (1)定义Car类,包含两个字段:name和price; (2)Car类中包含相应的属性、构造函数及ToString方法; (3)Car类继承接口IComparable,并实现CompareTo方法; (4)在Main方法中,使用Array.Sort方法对Car数组拍序, 拍序依据姓名和价格升序排序, 拍序后输出当前数组所有成员。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Myproject 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Car[] array = new Car[5]; 14 array[0] = new Car("A", 90); 15 array[1] = new Car("S", 100); 16 array[2] = new Car("B", 80); 17 array[3] = new Car("b", 80); 18 array[4] = new Car("Z", 80); 19 20 Console.WriteLine("Length {0}", array.Length); 21 22 Console.WriteLine("待排序序列"); 23 foreach (var item in array) 24 { 25 Console.WriteLine(item); 26 } 27 28 Array.Sort(array, 0, array.Length); 29 30 Console.WriteLine("已排序序列"); 31 foreach (var item in array) 32 { 33 Console.WriteLine(item); 34 } 35 36 } 37 } 38 public class Car : IComparable 39 { 40 string name; 41 int price; 42 43 public string Name 44 { 45 get { return name; } 46 set { name = value; } 47 } 48 49 public int Price 50 { 51 get { return price; } 52 set { price = value; } 53 } 54 55 public Car(string name, int price) 56 { 57 this.name = name; 58 this.price = price; 59 } 60 61 public Car() : this("", 0) { } 62 public override string ToString() 63 { 64 return string.Format("Car Name : {0} , Price : {1} ", Name, Price); 65 } 66 67 public int CompareTo(Object obj) 68 { 69 //1、判断 70 if( obj == null) 71 { 72 return 1; 73 } 74 //2、转换 75 Car rhs = obj as Car; 76 //3、判断 77 int r = 0; 78 if( rhs == null ) 79 { 80 throw new ArgumentException("Object is not a Car"); 81 } 82 else 83 { 84 r = this.price.CompareTo(rhs.price); 85 if ( r == 0) 86 { 87 return this.name.CompareTo(rhs.name); 88 } 89 else 90 { 91 return r; 92 } 93 } 94 } 95 } 96 }
3、接口练习,设计一个控制台应用程序,模拟银行存取款业务。具体要求如下: (1)定义一个接口IBankAccount,包含4个成员, 分别是 存款方法PayIn( )、 取款方法Withdraw( )、 转账方法TransferTo( )和 余额属性Banlance。 (2)定义一个类CurrentAccount,继承接口IBankAccount,并实现该接口所有成员。 (3)Main方法中调试并实现存款、取款、转账等操作。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Myproject 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 CurrentAccount c1 = new CurrentAccount("Tom", 10000); 14 Console.WriteLine( c1 ); 15 c1.Payln(10000); 16 Console.WriteLine( c1 ); 17 18 CurrentAccount c2 = new CurrentAccount("Jerry", 0); 19 c1.TransferTo(c2, 5000); 20 21 Console.WriteLine( c1 ); 22 Console.WriteLine( c2 ); 23 } 24 } 25 26 public interface IBankAccount 27 { 28 decimal Balance { get; } 29 decimal Payln(decimal x); 30 bool Withdraw(decimal x); 31 bool TransferTo(IBankAccount target ,decimal x); 32 } 33 34 public class CurrentAccount : IBankAccount 35 { 36 private string id; 37 private decimal balance; 38 39 public CurrentAccount (string id , decimal x) 40 { 41 this.id = id; 42 this.balance = x; 43 } 44 45 public CurrentAccount() : this("NA", 0) { } 46 public decimal Balance 47 { 48 get { return balance; } 49 } 50 51 public decimal Payln(decimal x) 52 { 53 balance += x ; 54 return balance; 55 } 56 57 public bool Withdraw(decimal x) 58 { 59 if (balance >= x) 60 { 61 balance -= x; 62 return true; 63 } 64 else return false; 65 } 66 67 public bool TransferTo(IBankAccount target , decimal x) 68 { 69 if( Withdraw(x)) 70 { 71 target.Payln(x); 72 return true; 73 } 74 else return false; 75 } 76 77 public override string ToString() 78 { 79 return string.Format("{0} : {1}", id, balance); 80 } 81 82 } 83 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论