C#實驗
實驗1 C# 基本編程
題目
VS下新建一個控制臺項目:諸如:hello world程序,運行程序輸出結(jié)果。并解釋C#程序的結(jié)構(gòu):
諸如:一個基本的C#程序包含幾部分 ,每一部分的功能是什么。。。
完整代碼
using System;//導(dǎo)入System命名空間
namespace HelloWorldApplication//命名空間
{
/* 類名為 HelloWorld */
class HelloWorld
{
/* main函數(shù) */
static void Main(string[] args)
{
/* 我的第一個 C# 程序 */
Console.WriteLine("Hello World!");//在控制臺輸出Hello World!并換行
Console.ReadKey();//等待鍵盤輸入,防止一閃而過
}
}
}
運行結(jié)果
這段代碼是一個經(jīng)典的 “Hello World” 程序,用于展示 C# 語言的基本結(jié)構(gòu)和輸出功能。下面對代碼進(jìn)行解析:
導(dǎo)入 System
命名空間
其中包含了 Console
類,用于處理控制臺輸入輸出。
csharpCopy code
using System;
定義命名空間 HelloWorldApplication
命名空間用于組織和管理代碼。
csharpCopy codenamespace HelloWorldApplication
{
}
定義一個類 HelloWorld
包含了程序的主要邏輯。
csharpCopy codeclass HelloWorld
{
}
在HelloWorld
類中、定義了 static
的 Main
方法,是程序的入口點。
csharpCopy codestatic void Main(string[] args)
{
}
在 Main
方法中,使用 Console.WriteLine
方法打印文本 “Hello World!” 到控制臺,并使用 Console.ReadKey 方法等待用戶按下任意鍵。
csharpCopy codeConsole.WriteLine("Hello World!");
Console.ReadKey();
以上就是該代碼的解析,它實現(xiàn)了一個簡單的控制臺應(yīng)用程序,輸出 “Hello World!” 到控制臺,并等待用戶按下任意鍵才退出。這是 C# 程序的基本結(jié)構(gòu),用于入門學(xué)習(xí)和展示語言特性。
實驗2 C#面向?qū)ο缶幊?/h4>
題目
建立一個控制臺程序,設(shè)計一個cat 類,在該類中定義cat的顏色 年齡 等屬性,并建立一個方法輸出“叫聲”。
在程序中建立類的對象讀取 和 設(shè)置 cat 的屬性值,并調(diào)用方法輸出 貓的 “叫聲”
完整代碼
using System;
namespace HelloWorld
{
class Cat
{
private string color;//貓的顏色
private int age; //年齡字段
public int Age//年齡屬性,用戶對外操作age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Color//顏色屬性,用戶對外操作age
{
get
{
return color;
}
set
{
color = value;
}
}
public void hall()
{
Console.WriteLine("一只"+this.age+"歲"+this.color+"的貓正在叫:喵喵喵!");
}
}
class Program
{
public static void Main(string[] args)
{
Cat cat = new Cat();
cat.Age = 10;
cat.Color = "灰色";
cat.hall();
Console.ReadKey();
}
}
}
運行結(jié)果
這段代碼定義了一個名為 Cat
的類,并在 Main
方法中創(chuàng)建了一個 Cat
的實例并使用其屬性和方法。
定義 Cat
類
csharpCopy codeclass Cat
{
private string color;
private int age;
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Color
{
get
{
return color;
}
set
{
color = value;
}
}
public void hall()
{
Console.WriteLine("一只"+this.age+"歲"+this.color+"的貓正在叫:喵喵喵!");
}
}
在這個類中,我們定義了私有字段 color
和 age
,分別表示貓的顏色和年齡。然后,使用屬性 Age
和 Color
封裝了這兩個字段,以提供對它們的訪問和設(shè)置。Age
和 Color
屬性具有 get
和 set
訪問器,使得我們可以通過 cat.Age
和 cat.Color
來獲取和設(shè)置貓的年齡和顏色。最后,定義了一個名為 hall
的方法,用于輸出貓的叫聲。
在 Main
方法中使用 Cat
類
csharpCopy codepublic static void Main(string[] args)
{
Cat cat = new Cat();
cat.Age = 10;
cat.Color = "灰色";
cat.hall();
Console.ReadKey();
}
在 Main
方法中,我們創(chuàng)建了一個名為 cat
的 Cat
類型的實例。然后,使用屬性訪問器 cat.Age
和 cat.Color
分別設(shè)置貓的年齡為 10 和顏色為 “灰色”。接著,調(diào)用 cat.hall()
方法,輸出貓的叫聲。最后,使用 Console.ReadKey()
等待用戶按下任意鍵退出程序。
實驗3 C#面向?qū)ο蟾呒壘幊?/h4>
題目
建立一個控制臺程序,(1)定義一個Person類,具有姓名(Name)、年齡(Age)、性別(Sex)等屬性;
(2)從Person類派生一個Student類,具有三個課程成績的數(shù)據(jù)成員,并具有SetScores方法(輸入學(xué)生的3門成績)、GetAverage方法(求平均成績);
(3)Student類要求其構(gòu)造函數(shù)具有三種重載形式:1、無參;2、具有姓名、年齡、性別三個參數(shù)的構(gòu)造函數(shù);3、具有姓名、年齡、性別、成績六個參數(shù)的構(gòu)造函數(shù);
(4)在Program類的Main方法中,使用Student的三個重載形式創(chuàng)建對象,并調(diào)用其GetAverage方法顯示平均成績;
完整代碼
using System;
namespace HelloWorld
{
class Person
{
protected string Name;
protected string Sex;
protected int Age;
}
// 派生類
class Student : Person
{
private int math;
private int english;
private int chinese;
public Student()
{
}
public string name
{
set
{
Name = value;
}
get
{
return Name;
}
}
public string sex
{
set
{
Sex = value;
}
get
{
return Sex;
}
}
public int age
{
set
{
Age = value;
}
get
{
return Age;
}
}
public Student(string Name,string Sex,int Age)
{
this.Name = Name;
this.Sex = Sex;
this.Age = Age;
}
public Student(string Name, string Sex, int Age,int math,int english,int chinese)
{
this.Name = Name;
this.Sex = Sex;
this.Age = Age;
this.math = math;
this.chinese = chinese;
this.english = english;
}
public void SetScores(int math,int chinese,int english)
{
this.math = math;
this.english = english;
this.chinese = chinese;
}
public void GetAverage()
{
Console.WriteLine(this.name+"的平均分為:"+((this.chinese)+(this.english)+(this.math))/3.0);
}
}
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.name = "蒲賀良";
stu1.sex = "男";
stu1.age = 21;
stu1.SetScores(100,100,100);
stu1.GetAverage();
Student stu2 = new Student("小黑","男",22);
stu2.SetScores(99,98,97);
stu2.GetAverage();
Student stu3 = new Student("小白", "男", 22,96,96,96);
stu3.GetAverage();
Console.ReadKey();
}
}
}
運行結(jié)果
這段代碼定義了一個 Person
類和一個派生類 Student
,并在 Main
方法中創(chuàng)建了幾個 Student
的實例。
定義 Person
類和 Student
類
csharpCopy codeclass Person
{
protected string Name;
protected string Sex;
protected int Age;
}
Person
類定義了三個保護(hù)字段 Name
、Sex
和 Age
,用于表示人的姓名、性別和年齡。
csharpCopy codeclass Student : Person
{
private int math;
private int english;
private int chinese;
// 構(gòu)造函數(shù)
public Student()
{
}
// 屬性
public string name
{
set
{
Name = value;
}
get
{
return Name;
}
}
public string sex
{
set
{
Sex = value;
}
get
{
return Sex;
}
}
public int age
{
set
{
Age = value;
}
get
{
return Age;
}
}
// 構(gòu)造函數(shù)重載
public Student(string Name, string Sex, int Age)
{
this.Name = Name;
this.Sex = Sex;
this.Age = Age;
}
public Student(string Name, string Sex, int Age, int math, int english, int chinese)
{
this.Name = Name;
this.Sex = Sex;
this.Age = Age;
this.math = math;
this.chinese = chinese;
this.english = english;
}
// 方法
public void SetScores(int math, int chinese, int english)
{
this.math = math;
this.english = english;
this.chinese = chinese;
}
public void GetAverage()
{
Console.WriteLine(this.name + "的平均分為:" + ((this.chinese) + (this.english) + (this.math)) / 3.0);
}
}
Student
類是 Person
類的派生類,它添加了私有字段 math
、english
和 chinese
,用于表示學(xué)生的數(shù)學(xué)、英語和語文成績。類中包含了構(gòu)造函數(shù)重載,用于根據(jù)不同參數(shù)創(chuàng)建 Student
類的實例。另外,類中還定義了屬性 name
、sex
和 age
,用于訪問和設(shè)置 Person
類中的保護(hù)字段。此外,類中還定義了 SetScores
方法,用于設(shè)置學(xué)生的成績,以及 GetAverage
方法,用于計算并輸出學(xué)生的平均分。
在 Main
方法中使用 Student
類
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.name = "蒲賀良";
stu1.sex = "男";
stu1.age = 21;
stu1.SetScores(100,100,100);
stu1.GetAverage();
Student stu2 = new Student("小黑","男",22);
stu2.SetScores(99,98,97);
stu2.GetAverage();
Student stu3 = new Student("小白", "男", 22,96,96,96);
stu3.GetAverage();
Console.ReadKey();
}
在 Main
方法中使用 Student
類
實驗四 C#中的文件處理
題目
建立一個控制臺程序,利用所學(xué)讀寫文件類 封裝一個讀文件接口 一個 寫文件接口,并完成對文件的讀寫。
完整代碼
using System;
using System.IO;
namespace FileApplication
{
class File_Test
{
private string path;
public File_Test(string path)
{
this.path = path;
}
public void Read()
{
try
{
// 創(chuàng)建一個 StreamReader 的實例來讀取文件
StreamReader sr = new StreamReader(path, false);
string line;
// 從文件讀取并顯示行,直到文件的末尾
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
catch (Exception e)
{
// 向用戶顯示出錯消息
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
public void Write(string[] content)
{
using (StreamWriter sw = new StreamWriter(path))
{
foreach (string s in content)
{
sw.WriteLine(s);
}
}
}
}
class Program
{
static void Main(string[] args)
{
string Path = @"C:\Users\puheliang\Desktop\Project\CS_Project\實驗4\test.txt";
File_Test f = new File_Test(Path);
string[] test = { "任浩真帥", "任浩帥呆了", "任浩帥","好帥的任浩" };
f.Write(test);
f.Read();
}
}
}
運行結(jié)果
這段代碼演示了使用 StreamReader
和 StreamWriter
類來讀取和寫入文件。
定義 File_Test
類
csharpCopy codeclass File_Test
{
private string path;
public File_Test(string path)
{
this.path = path;
}
public void Read()
{
try
{
// 創(chuàng)建一個 StreamReader 的實例來讀取文件
StreamReader sr = new StreamReader(path, false);
string line;
// 從文件讀取并顯示行,直到文件的末尾
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
catch (Exception e)
{
// 向用戶顯示出錯消息
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
public void Write(string[] content)
{
using (StreamWriter sw = new StreamWriter(path))
{
foreach (string s in content)
{
sw.WriteLine(s);
}
}
}
}
File_Test
類包含了兩個方法:Read()
和 Write(string[] content)
。Read()
方法使用 StreamReader
類來讀取文件內(nèi)容并逐行輸出到控制臺。Write(string[] content)
方法使用 StreamWriter
類來將字符串?dāng)?shù)組 content
的內(nèi)容寫入到文件中。
使用 File_Test
類
csharpCopy codestatic void Main(string[] args)
{
string Path = @"C:\Users\puheliang\Desktop\Project\CS_Project\實驗4\test.txt";
File_Test f = new File_Test(Path);
string[] test = { "任浩真帥", "任浩帥呆了", "任浩帥", "好帥的任浩" };
f.Write(test);
f.Read();
}
在 Main
方法中,我們首先定義了一個文件路徑 Path
。然后,創(chuàng)建了一個 File_Test
的實例 f
,并傳入文件路徑。接著,定義了一個字符串?dāng)?shù)組 test
,包含了要寫入文件的內(nèi)容。調(diào)用 f.Write(test)
方法將內(nèi)容寫入文件,然后調(diào)用 f.Read()
方法讀取文件并將內(nèi)容輸出到控制臺。最后,使用 Console.ReadKey()
等待用戶按下任意鍵退出程序。
實驗五:線程技術(shù)使用
題目
建立一個控制臺程序,建立四個線程,每個線程的功能為:輸出0-9 共計10個數(shù)字,要求線程的輸出為連續(xù)輸出,借助信號量或者互斥鎖進(jìn)行實現(xiàn)。
完整代碼
using System;
using System.Threading;
namespace MultithreadingApplication
{
class ThreadCreationProgram
{
static Mutex mutex = new Mutex();
static Semaphore sem = new Semaphore(1, 1);
public static void CallToChildThread01()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread1:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
public static void CallToChildThread02()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread2:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
public static void CallToChildThread03()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread3:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
public static void CallToChildThread04()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread4:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
static void Main(string[] args)
{
ThreadStart childref01 = new ThreadStart(CallToChildThread01);
Thread childThread01 = new Thread(childref01);
childThread01.Name = "Thread1";
childThread01.Start();
ThreadStart childref02 = new ThreadStart(CallToChildThread02);
Thread childThread02 = new Thread(childref02);
childThread02.Name = "Thread2";
childThread02.Start();
ThreadStart childref03 = new ThreadStart(CallToChildThread03);
Thread childThread03 = new Thread(childref03);
childThread03.Name = "Thread3";
childThread03.Start();
ThreadStart childref04 = new ThreadStart(CallToChildThread04);
Thread childThread04 = new Thread(childref04);
childThread04.Name = "Thread4";
childThread04.Start();
;
Console.ReadKey();
}
}
}
運行結(jié)果
這段代碼演示了使用多線程進(jìn)行并發(fā)操作,并使用 Mutex
和 Semaphore
實現(xiàn)線程同步。
定義線程函數(shù)
csharpCopy codepublic static void CallToChildThread01()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread1:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
public static void CallToChildThread02()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread2:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
public static void CallToChildThread03()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread3:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
public static void CallToChildThread04()
{
sem.WaitOne();
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("thread4:" + i);
Thread.Sleep(5);
}
}
sem.Release();
}
在這里定義了四個線程函數(shù),分別是 CallToChildThread01
、CallToChildThread02
、CallToChildThread03
和 CallToChildThread04
。每個函數(shù)都會使用 sem.WaitOne()
獲取信號量,然后執(zhí)行一個簡單的循環(huán)輸出,并使用 Thread.Sleep(5)
使線程休眠 5 毫秒,模擬一些處理時間。最后,通過 sem.Release()
釋放信號量。
創(chuàng)建并啟動線程
csharpCopy codeThreadStart childref01 = new ThreadStart(CallToChildThread01);
Thread childThread01 = new Thread(childref01);
childThread01.Name = "Thread1";
childThread01.Start();
ThreadStart childref02 = new ThreadStart(CallToChildThread02);
Thread childThread02 = new Thread(childref02);
childThread02.Name = "Thread2";
childThread02.Start();
ThreadStart childref03 = new ThreadStart(CallToChildThread03);
Thread childThread03 = new Thread(childref03);
childThread03.Name = "Thread3";
childThread03.Start();
ThreadStart childref04 = new ThreadStart(CallToChildThread04);
Thread childThread04 = new Thread(childref04);
childThread04.Name = "Thread4";
childThread04.Start();
在 Main
方法中,我們使用 ThreadStart
創(chuàng)建了四個線程的啟動函數(shù),并使用 Thread
類創(chuàng)建了四個線程實例 childThread01
、childThread02
、childThread03
和 childThread04
。我們?yōu)槊總€線程指定了名稱,然后通過調(diào)用 Start()
方法啟動線程。
主線程等待
csharpCopy code
Console.ReadKey();
childThread01.Name = "Thread1";
childThread01.Start();
ThreadStart childref02 = new ThreadStart(CallToChildThread02);
Thread childThread02 = new Thread(childref02);
childThread02.Name = "Thread2";
childThread02.Start();
ThreadStart childref03 = new ThreadStart(CallToChildThread03);
Thread childThread03 = new Thread(childref03);
childThread03.Name = "Thread3";
childThread03.Start();
ThreadStart childref04 = new ThreadStart(CallToChildThread04);
Thread childThread04 = new Thread(childref04);
childThread04.Name = "Thread4";
childThread04.Start();
在 Main
方法中,我們使用 ThreadStart
創(chuàng)建了四個線程的啟動函數(shù),并使用 Thread
類創(chuàng)建了四個線程實例 childThread01
、childThread02
、childThread03
和 childThread04
。我們?yōu)槊總€線程指定了名稱,然后通過調(diào)用 Start()
方法啟動線程。文章來源:http://www.zghlxwxcb.cn/news/detail-478165.html
主線程等待
csharpCopy code
Console.ReadKey();
最后,使用 Console.ReadKey()
讓主線程等待用戶按下任意鍵,以保持程序運行。文章來源地址http://www.zghlxwxcb.cn/news/detail-478165.html
到了這里,關(guān)于C#程序設(shè)計實驗的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!