類別是 C# 中的基本構造,用於封裝數據和行為。類別是對象的藍圖,可以包含字段、屬性、方法和事件。
using System;
public class Person
{
// 字段
private string name;
private int age;
// 屬性
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
// 方法
public void Greet()
{
Console.WriteLine($"Hello, my name is {name} and I am {age} years old.");
}
}
public class Program
{
public static void Main(string[] args)
{
Person person = new Person();
person.Name = "Alice";
person.Age = 30;
person.Greet(); // Output: Hello, my name is Alice and I am 30 years old.
}
}
繼承允許類別從另一個類別派生,繼承其字段和方法。C# 支持單繼承,但可以實現多個介面。
using System;
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
public class Program
{
public static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat(); // Output: Eating...
dog.Bark(); // Output: Barking...
}
}
多型允許對象通過父類或介面類型來呼叫方法,行為由實際對象決定。這通常通過方法重寫(override)實現。
using System;
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal sound");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Bark");
}
}
public class Program
{
public static void Main(string[] args)
{
Animal animal = new Dog();
animal.Speak(); // Output: Bark
}
}
封裝是將數據和方法封裝在類別內部,通過屬性和方法來控制對它們的訪問。
using System;
public class Account
{
private double balance;
public double Balance
{
get { return balance; }
private set { balance = value; }
}
public void Deposit(double amount)
{
if (amount > 0)
{
Balance += amount;
}
}
public void Withdraw(double amount)
{
if (amount > 0 && amount <= Balance)
{
Balance -= amount;
}
}
}
public class Program
{
public static void Main(string[] args)
{
Account account = new Account();
account.Deposit(100);
account.Withdraw(30);
Console.WriteLine(account.Balance); // Output: 70
}
}
介面定義了成員的簽名,不包含實現。類別可以實現介面,提供這些成員的具體實現。
using System;
public interface IAnimal
{
void Speak();
}
public class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Bark");
}
}
public class Program
{
public static void Main(string[] args)
{
IAnimal animal = new Dog();
animal.Speak(); // Output: Bark
}
}
抽象類別不能實例化,可以包含抽象方法(無實現)和具體方法(有實現)。子類必須實現所有抽象方法。
using System;
public abstract class Animal
{
public abstract void Speak();
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Bark");
}
}
public class Program
{
public static void Main(string[] args)
{
Dog dog = new Dog();
dog.Speak(); // Output: Bark
dog.Eat(); // Output: Eating...
}
}
靜態類別不能實例化,所有成員都必須是靜態的,常用於工具類或擴展方法。
using System;
public static class MathHelper
{
public static int Add(int a, int b)
{
return a + b;
}
}
public class Program
{
public static void Main(string[] args)
{
int result = MathHelper.Add(3, 4);
Console.WriteLine(result); // Output: 7
}
}
列舉是一種特殊的數據類型,包含一組命名常數。
using System;
public enum DaysOfWeek
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
public class Program
{
public static void Main(string[] args)
{
DaysOfWeek today = DaysOfWeek.Monday;
Console.WriteLine(today); // Output: Monday
}
}
委派是一種類型安全的函數指標,允許方法作為參數傳遞。
using System;
public class Program
{
public delegate void PrintDelegate(string message);
public static void PrintMessage(string message)
{
Console.WriteLine(message);
}
public static void Main(string[] args)
{
PrintDelegate del = PrintMessage;
del("Hello, delegate!"); // Output: Hello, delegate!
}
}
Lambda 表達式是匿名函數,用於簡潔地表示委派或表達式樹。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
foreach (var num in evenNumbers)
{
Console.WriteLine(num); // Output: 2 4
}
}
}
泛型允許類別、介面和方法在聲明時不指定特定的數據類型,增加了代碼的重用性和類型安全性。
using System;
public class GenericList<T>
{
private T[] elements;
private int count;
public GenericList(int size)
{
elements = new T[size];
count = 0;
}
public void Add(T element)
{
if (count < elements.Length)
{
elements[count++] = element;
}
}
public T GetElement(int index)
{
if (index < count)
{
return elements[index];
}
throw new IndexOutOfRangeException();
}
}
public class Program
{
public static void Main(string[] args)
{
GenericList<int> intList = new GenericList<int>(5);
intList.Add(1);
intList.Add(2);
Console.WriteLine(intList.GetElement(1)); // Output: 2
}
}
反射允許在運行時檢查和操作類型的元數據。
using System;
using System.Reflection;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Greet()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
public class Program
{
public static void Main(string[] args)
{
Type type = typeof(Person);
Console.WriteLine($"Class: {type.Name}");
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine($"Property: {property.Name}");
}
MethodInfo method = type.GetMethod("Greet");
Console.WriteLine($"Method: {method.Name}");
object person = Activator.CreateInstance(type);
type.GetProperty("Name").SetValue(person, "Alice");
type.GetProperty("Age").SetValue(person, 30);
method.Invoke(person, null); // Output: Hello, my name is Alice and I am 30 years old.
}
}