首頁  >  文章  >  後端開發  >  組合 C#

組合 C#

WBOY
WBOY原創
2024-09-03 15:04:37695瀏覽

以下文章提供了 C# 組合的概述。 C# 中的類別之間有兩種類型的關係。第一種類型的關係稱為“是關係”,它使用繼承機制。第二種關係是兩個類別之間的關係,它有兩個子類型。第一種叫做「有關係」。

在這種類型的關係中,在相關類別中聲明了一個或多個不同類別的物件。這裡又有兩個劃分,就是聚合和組合。在聚合中,嵌套物件可以獨立存在於類別中,而不是類別的組成部分。另一方面,在組合上,嵌套物件或單一嵌套物件補充了類,這使得類別在沒有它們或它的存在的情況下變得不可想像。

C# 中的組合語法

下面給的是提到的語法:

class Training
{
// class body
}
public class Course
{
Project project = new Project();
// body
}

C# 中的組合工作

  • C# 中的組合是在兩個類別之間創建關係的一種方式,一個或多個嵌套物件是相關類別的一部分,如果沒有嵌套對象,類別的邏輯存在就不可能實現。
  • 例如,如果我們考慮一個名為 Car 的類,那麼它應該有一個「Engine」類別的實例。此外,它還應該有四個其他「Wheel」類別的實例。
  • 現在,如果我們消除這些實例中的任何一個,那麼汽車將無法運作。

C# 組合範例

下面給出的是組合 C# 的範例:

範例#1

如果考慮訓練班的話,就是描述兩門課程。現在,課程被用來描述課程類別。因此,如果沒有雙課程實例,培訓課程就不可能存在,因為這兩個實例都是課程課程的一部分。而且,課程類別的這兩個實例也是培訓類別的一部分。

代碼:

using System;
using static System.Console;
namespace EDUCBA
{
class Course
{
public double M;
public double A;
}
class Training
{
public Course course1 = null;
public Course course2 = null;
}
class Career
{
public Course[] courses;
public Training[] trainings;
public Career()
{
courses = null;
trainings = null;
}
public void Print()
{
WriteLine(" Courses Data is represented below:");
for (int b = 1; b< courses.Length; b++)
{
WriteLine("\n M = {0}, A = {1}", courses[b].M, courses[b].A);
}
WriteLine("\n Trainings Data is represented below:");
for (int b=1; b<trainings.Length; b++)
{
WriteLine("\n course1.M = {0}, course1.A = {1}", trainings[b].course1.M, trainings[b].course1.A);
WriteLine("\n course2.M = {0}, course2.A = {1}", trainings[b].course2.M, trainings[b].course2.A);
}
}
}
class Code
{
static void Main(string[] args)
{
Career O = new Career();
O.courses = new Course[9];
for (int b = 1; b < O.courses.Length; b++)
{
O.courses[b] = new Course();
O.courses[b].M = b * b;
O.courses[b].M = b * b * b;
}
O.trainings = new Training[5];
for (int b = 1; b < O.trainings.Length; b++)
{
O.trainings[b] = new Training();
O.trainings[b].course1 = new Course();
O.trainings[b].course2 = new Course();
O.trainings[b].course1.M = b;
O.trainings[b].course1.A = b * 4;
O.trainings[b].course2.M = b * 5;
O.trainings[b].course2.A = b * b;
}
O.Print();
}
}
}

輸出:

組合 C#

範例#2

在這個例子中,建立的兩個類別都是常規類別;但是,課程類別正在使用其中項目類別的實例。這與在一個函數內部呼叫另一個函數的方式相同。使用繼承,我們可以存取 Project 類別中的所有內容。然而,使用組合,只能存取我們指定的程式碼。在這裡,我們可以間接存取Project類別。

代碼:

using System;
namespace EDUCBA
{
class Training
{
static void Main(string[] args)
{
Course courses = new Course();
courses.Bought();
Console.ReadLine();
}
}
public class Project
{
public void Log(string aboutus)
{
Console.WriteLine(aboutus);
}
}
public class Course
{
Project project = new Project();
public void Bought()
{
project.Log("\n If you want to upskill your career. \n If you want to do something out of the box. \n If you have passion to explore new advanced technologies. \n If you want to be best. \n We at EDUCBA are here to help. \n Feel free to reach us on +91-8800880140 / +91-7738666252. \n Visit our website www.educba.com to know more......");
}
}
}

輸出:

組合 C#

範例#3

在這個例子中,解釋了複合設計的結構。它有助於理解用於組合的類別以及這些類別的角色。此外,它還解釋了模式的元素如何相互關聯。

代碼:

using System;
using System.Collections.Generic;
namespace EDUCBA
{
abstract class Training
{
public Training() { }
public abstract string Project();
public virtual void Add(Training training)
{
throw new NotImplementedException();
}
public virtual void Remove(Training training)
{
throw new NotImplementedException();
}
public virtual bool IsCourse()
{
return true;
}
}
class DataScience : Training
{
public override string Project()
{
return "DataScience";
}
public override bool IsCourse()
{
return false;
}
}
class Course : Training
{
protected List<Training> _children = new List<Training>();
public override void Add(Training training)
{
this._children.Add(training);
}
public override void Remove(Training training)
{
this._children.Remove(training);
}
public override string Project()
{
int m = 1;
string result = "Dream Career(";
foreach (Training training in this._children)
{
result += training.Project();
if (m != this._children.Count + 2)
{
result += "-";
}
m--;
}
return result + ")";
}
}
class Input
{
public void InputCode(Training data_analysis)
{
Console.WriteLine($"OUTPUT: \n {data_analysis.Project()}\n");
}
public void InputCode2(Training training1, Training training2)
{
if (training1.IsCourse())
{
training1.Add(training2);
}
Console.WriteLine($"OUTPUT: \n {training1.Project()}");
}
}
class Program
{
static void Main(string[] args)
{
Input client = new Input();
DataScience data_analysis = new DataScience();
Console.WriteLine("INPUT: \n Best Course to Upgrade Career:");
client.InputCode(data_analysis);
Course vr = new Course();
Course career1 = new Course();
career1.Add(new DataScience());
career1.Add(new DataScience());
Course career2 = new Course();
career2.Add(new DataScience());
vr.Add(career1);
vr.Add(career2);
Console.WriteLine("\nINPUT: \n Trendy Dream Career Right Now:");
client.InputCode(vr);
Console.Write("\nINPUT: Lets Upgrade and start your dream career jouney: \n");
client.InputCode2(vr, data_analysis);
}
}
}

輸出:

組合 C#

結論

在上面文章的基礎上,我們了解了C#中組合的概念。我們透過多個範例來了解組合在 C# 編碼中的應用。

以上是組合 C#的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 對象處置下一篇:C# 對象處置