02. 方法重写

方法重写:

  定义:重写方法用相同的签名重写所继承的方法,即具有相同的方法名、返回类型和参数表。

 1 /**
 2  * 
 3  *  1、设计一个基类,在其中创建方法 MConvert ,
 4  *  这个方法接受一个代表公里数的参数,将其转换为对等的英里数之后,
 5  *  然后创建一个继承此类的子类,增加可将输入的公斤数转换为磅的新方法 KConvert , 
 6  *  最后产生子类的实例对象, 测试这两个功能。
 7  *  提示信息:1千米(公里) = 0.62英里; 1千克(公斤) = 2.2磅 
 8  */
 9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Threading.Tasks;
14 
15 namespace SecondAssignment
16 {
17     class Program
18     {
19         static void Main(string[] args)
20         {
21             //产生子类的实例对象
22             Child c = new Child();
23             c.KConvert(2);
24             Father f = new Father();
25             f.MConvert(1);
26             Console.ReadKey();
27         }
28     }
29     class Father
30     {
31         //MConvert 方法
32         public void MConvert(double Km)
33         {
34             double mile = Km * 0.62;
35             Console.WriteLine("公里转英里:"+mile);
36         }
37     }
38     class Child : Father
39     {
40         //KConvert方法。公斤转为磅
41         public void KConvert(double Kg)
42         {
43             double pound = Kg * 2.2;
44             Console.WriteLine("公斤转为磅:"+pound);
45         }
46     }
47 }

 

 1 /**
 2  *  2、设计一个基类,在其中创建方法 MConvert,
 3  *  这个方法接受一个代表公里数的参数,将其转换为对等的英里数之后,
 4  *  然后创建一个继承此类的子类,
 5  *  提示信息:1千米(公里) = 0.62英里 
 6  *  将其中的 MConvert 方法声明为 virtual, 
 7  *  然后在子类中进行重写,以其所接受的参数为正方形边长,转换为英里后计算其面积。
 8  */
 9 using System;
10 using System.Collections.Generic;
11 using System.Linq;
12 using System.Text;
13 using System.Threading.Tasks;
14 
15 namespace SecondAssignment
16 {
17     class Program
18     {
19         static void Main(string[] args)
20         {
21             Child c = new Child();
22             c.MConvert(1);
23             Console.ReadKey();
24         }
25     }
26     class Father
27     {
28         //MConvert 方法
29         public virtual void MConvert(double Km)
30         {
31             double mile = Km * 0.62;
32             Console.WriteLine("公里转英里:"+mile);
33         }
34     }
35     class Child : Father
36     {
37         //在子类中重写
38         //其所接受的参数为正方形边长,转换为英里后计算其面积。
39         public override void MConvert(double Km)
40         {
41             double mile = Km * 0.62;
42             double _area = mile * mile;
43             Console.WriteLine("面积为:"+_area);
44         }
45     }
46 }

 

 1 /**
 2  *   3: 创建一个类,重写ToString() 方法, 当其被引用的时候,能够输出此类对象的说明文字,
 3  *   如下:“测试用的myObject 类对象”
 4  */
 5 using System;
 6 using System.Collections.Generic;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 
11 namespace SecondAssignment
12 {
13     class Child
14     {
15         public override string ToString()
16         {
17             Console.WriteLine("测试用的myObject 类对象");
18             return base.ToString();
19         }
20     }
21     class Program
22     {
23         static void Main(string[] args)
24         {
25             Child c = new Child();
26             c.ToString();
27             Console.ReadKey();
28         }
29     }
30 }

 

posted @ 2017-10-08 21:52  LiuChangwei  阅读(260)  评论(0编辑  收藏  举报