C# 繼承


C# 繼承

繼承是面向對象程序設計中最重要的概念之一。繼承允許我們根據一個類來定義另一個類,這使得創建和維護應用程序變得更容易。同時也有利於重用代碼和節省開發時間。

當創建一個類時,程序員不需要完全重新編寫新的數據成員和成員函數,只需要設計一個新的類,繼承了已有的類的成員即可。這個已有的類被稱為的基類,這個新的類被稱為派生類

繼承的思想實現了 屬於(IS-A) 關系。例如,哺乳動物 屬於(IS-A) 動物,狗 屬於(IS-A) 哺乳動物,因此狗 屬於(IS-A) 動物。

基類和派生類

一個類可以派生自多個類或接口,這意味着它可以從多個基類或接口繼承數據和函數。

C# 中創建派生類的語法如下:

<訪問修飾符符> class <基類>
{
 ...
}
class <派生類> : <基類>
{
 ...
}

 假如一個基類是Shape,一個派生類是Rectangle。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace week2._1
{    
    // 基類
    class Shape
    {     
        public void setLength(int len)
        {
            length = len;
        }
        public void setWidth(int wid)
        {
            width = wid;
        }
        protected int length;
        protected int width;
    }
    //派生類
    class Rectangle : Shape
    {
        private int height;
        public int getArea()
        {
            return length * width;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //創建對象
            Rectangle rect = new Rectangle();
            rect.setLength(5);
            rect.setWidth(6);
            
            Console.WriteLine(rect.getArea());
            Console.ReadLine();
        }
    }
}

 結果:

30

基類的初始化

派生類繼承了基類的成員變量和成員方法。因此父類對象應在子類對象創建之前被創建。您可以在成員初始化列表中進行父類的初始化。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace week2._1
{
    class Rectangle
    {
        //成員變量
        protected double width;
        protected double lenght;
        //構造方法
        public Rectangle(double l, double w)
        {
            width = w;
            lenght = l;
        }
        //面積
        public  double GetArea()
        {
            return width *lenght;
        }
        //顯示
        public void Display()
        {
            Console.WriteLine("長度:{0}", lenght);
            Console.WriteLine("寬度:{0}", width );
            Console.WriteLine("面積:{0}", GetArea());
        }
        //繼承Rectangle
        class Tabletop : Rectangle
        {
            private double cost;
            public Tabletop(double l,double w) : base(l, w)
            {
            }
            public double Getcost()
            {
                double cost;
                cost = GetArea() * 70;
                return cost;
            }
            //重寫Display方法
            public void Display()
            {
//base.Display(); Console.WriteLine(
"成本:{0}", Getcost()); } } class Test { static void Main(string[] args) { Tabletop t = new Tabletop(4.5, 5.5); t.Display(); Console.ReadKey(); } } } }

 

結果:

成本:1732.5

 

C# 多重繼承

多重繼承指的是一個類別可以同時從多於一個父類繼承行為與特征的功能。與單一繼承相對,單一繼承指一個類別只可以繼承自一個父類。

C# 不支持多重繼承。但是,您可以使用接口來實現多重繼承。下面的程序演示了這點:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace week2._1
{
    class Shape
    {
        protected int  width;
        protected int  height;
        public void setWidth(int w)
        {
            width = w;
        }
        public void setHeight(int h)
        {
            height = h;
        }
    }
    //基類PaintCost
    public interface PaintCost
    {
        int getCost(int area);
    }
    //派生類
    class Rectangle: Shape, PaintCost
    {
        public int getArea()
        {
            return width * height;
        }
        public int getCost(int area)
        {
            return area * 70;
        }
 
    }

    class test
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            int area;
            r.setHeight(7);
            r.setWidth(8);
            area = r.getArea();
            Console.WriteLine(area);
            Console.WriteLine(r.getCost(area));
            Console.ReadKey();
        }
    }
    
}

 

結果:

56
3920

 

關於繼承的幾點:

  •  1、繼承的語法:class 子類類名 : class 父類類名{ //子類類體 }
  •  2、繼承的特點:子類擁有所有父類中所有的字段、屬性和方法
  •  3、一個類可以有多個子類,但是父類只能有一個
  •  4、一個類在繼承另一個類的同時,還可以被其他類繼承
  •  5、在 C# 中,所有的類都直接或者間接的繼承自 Object 類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace week2._1
{
    class Program
    {
        class father
        {
            public father()
            {
                Console.WriteLine("father");
            }
        }
        class son : father
        {
            public son()
            {
                Console.WriteLine("son");
            }
        }
        static void Main(string[] args)
        {
            son s = new son();
            Console.ReadKey();
        }
    }
}

 

結果:

father
son


免責聲明!

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



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