C#中通過類來繼承兩個接口,父類實例化接口中的方法,子類繼承父類,調用方法


實現了父類繼承接口,父類實例化接口的方法,子類繼承父類,子類調用父類的方法直接使用

代碼如下:

 

    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(); 
         }

         
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM