在 C# 中從多個類繼承

Saad Aslam 2022年4月20日
在 C# 中從多個類繼承

一個類或物件可以從 OOP 語言中的一個或多個父物件或類繼承特徵和特性。當子類需要訪問任何或所有父類的屬性時,就會使用繼承。

當一個子類需要合併許多基類構造體時,它也很方便。在本教程中,我們將討論如何在 C# 中實現多重繼承。

C# 中實現多重繼承

在 C# 語言中,最著名和最直接的技術是使用介面。這裡我們有一個例子,我們將在其中實現從多個類的繼承來計算 BMI。

我們將逐步學習這一點。首先,我們需要匯入 System 庫來訪問 C# 中使用的方法。

using System;

我們將使用 setter 方法建立一個名為 Attributes 的類,我們將使用它從其他類中檢索值。在其中宣告兩個雙精度型變數,名為 weightheight

我們將保持它們 protected,這意味著我們類以外的任何人都無法使用它們。接下來,建立一個帶有雙型別引數 w 的方法 setWeight(),將提供的值分配給 weight,並建立 setHeight() 方法,將 height 的值設定為相同就像這個一樣。

class Attributes {
   protected double weight;
   protected double height;
   public void setWeight(double w) {
     weight = w;
   }

   public void setHeight(double h) {
     height = h;
   }
}

然後,建立一個介面 RetrieveAge,其中包含一個雙型別方法 retrieveAge,該方法採用雙資料型別的引數 age

public interface RetrieveAge {
   double retrieveAge(double age);
}

建立一個將繼承兩個類 AttributesRetrieveAgeBMI 類。我們將建立 4 個方法來從此類中的父類中檢索值。

第一個方法是一個名為 retrieveBMI() 的雙精度資料型別,它從父類 Attributes 獲取體重和身高,然後將體重除以身高的平方,然後返回結果。接下來的兩個方法 retrieveHeight()retrieveWeight() 將獲取身高和體重值並返回它們。

最後一個方法 retrieveAge() 擴充套件了 RetrieveAge 的方法,該方法接受一個雙精度型別的引數 age 並返回它。

class BMI : Attributes, RetrieveAge {

   public double retrieveBMI() {
      return (weight / (height * height));
   }

   public double retrieveHeight() {
     return height;
   }

   public double retrieveWeight() {
     return weight;
   }

   public double retrieveAge(double age) {
     return age;
   }
}

我們建立最終類,該類將具有 Main() 函式。在 main 函式中,建立一個 BMI 類例項。

使用 setHeight()setWeight() 方法來分配身高和體重。最後,我們需要輸出身高、體重、計算出的 BMI 和年齡。

完整程式碼:

using System;

class Attributes {

   protected double weight;
   protected double height;
   public void setWeight(double w) {
      weight = w;
   }

   public void setHeight(double h) {
      height = h;
   }

}

public interface RetrieveAge {
   double retrieveAge(double age);
}

class BMI : Attributes, RetrieveAge {

   public double retrieveBMI() {
      return (weight / (height * height));
   }

   public double retrieveHeight() {
      return height;
   }

   public double retrieveWeight() {
      return weight;
   }

   public double retrieveAge(double age) {
      return age;
   }
}

class TotalBMI {

   static void Main() {
      BMI bmi = new BMI();
      bmi.setWeight(80);
      bmi.setHeight(2.07);

      Console.WriteLine("Your Height: {0} m", bmi.retrieveHeight());
      Console.WriteLine("Your Weight: {0} kg", bmi.retrieveWeight());
      Console.WriteLine("BMI: {0}", bmi.retrieveBMI());
      Console.WriteLine("Age: {0}" , bmi.retrieveAge(40));
     }
}

輸出:

Your Height: 2.07 m
Your Weight: 80 kg
BMI: 18.6702140073281
Age: 40
Author: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn

相關文章 - Csharp Class