在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
实现了父类继承接口,父类实例化接口的方法,子类继承父类,子类调用父类的方法直接使用 代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace sortAndArea { public interface Sort { List<int> sort(); } public interface Area { long area(); } abstract class CoustomMath : Sort, Area { public CoustomMath() { // public List<int> list = new List<int> { }; } public List<int> list = new List<int> { }; //使用泛型 public int l = 0; public List<int> sort() { l = list.Count; int temp = 0; //Console.WriteLine(l); for (int i = 0; i < l ; i++) //排序 { for (int j = i+1; j < l; j++) { if (list[i] > list[j]) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } } return list; } public long area() { long sum = 0; for (int k = 0; k < l; k++) { sum += list[k]; } long r = sum; long area =(long) Math.PI * r * r; //求面积 return area; } } class Program : CoustomMath { public List<int> list = new List<int> { }; public Program() { list = new List<int> { 6, 7, 2, 12, 9, 1, 0 }; base.list = list; } //Program pp = new Program(); static void Main(string[] args) { //List<int> list = new List<int> { }; Program pp = new Program(); Console.WriteLine("从小到大的排序为:"); pp.sort(); int l1 = 0; l1 = pp.l; for (int i=0;i<l1;i++) { Console.Write(pp.list[i]+" "); } Console.WriteLine("\n"); Console.WriteLine("求和之后的面积为:"); Console.WriteLine(pp.area()); Console.ReadKey(); } } }
|
请发表评论