




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
千里之行,始于足下讓知識(shí)帶有溫度。第第2頁(yè)/共2頁(yè)精品文檔推薦c#習(xí)題1-5附答案解讀1、簡(jiǎn)要回答下列問題。
(1)舉例說明new關(guān)鍵字可用于哪些方面?
(2)sealed關(guān)鍵字的作用是什么?什么狀況下需要使用sealed關(guān)鍵字?
(3)哪些關(guān)鍵字可以用于版本控制?
【解答】
1)在C#中,new關(guān)鍵字可用作運(yùn)算符或修飾符。作為運(yùn)算符用于在堆上創(chuàng)建對(duì)象和調(diào)用構(gòu)造函數(shù)。作為修飾符用于躲藏基類成員的繼承成員。
2)在類聲明中使用sealed修飾符可防止其它類繼承此類。在辦法聲明中使用sealed修飾符可防止擴(kuò)充類重寫此辦法。
sealed修飾符主要用于防止非故意的派生,但是它還能促使某些運(yùn)行時(shí)優(yōu)化。詳細(xì)說來(lái),因?yàn)槊芊忸愑谰貌粫?huì)有任何派生類,所以對(duì)密封類的實(shí)例的虛擬函數(shù)成員的調(diào)用可以轉(zhuǎn)換為非虛擬調(diào)用來(lái)處理。
3)override關(guān)鍵字和new關(guān)鍵字均可用于版本控制。
在C#中,默認(rèn)狀況下辦法不是虛擬的。若要使辦法成為虛擬辦法,必需在基類的辦法聲明中使用virtual修飾符。然后,派生類可以使用override關(guān)鍵字重寫基類中的虛擬辦法,或使用new關(guān)鍵字躲藏基類中的虛擬辦法。假如override關(guān)鍵字和new關(guān)鍵字均未指定,編譯器將發(fā)出警告,并且派生類中的辦法將躲藏基類中的辦法。
2、簡(jiǎn)要回答抽象類和接口的主要區(qū)分。
【解答】
抽象類和接口的一個(gè)主要差別是:類可以實(shí)現(xiàn)多個(gè)接口,但僅能從一個(gè)抽象類或任何其它類型的類繼承。
3、編寫一個(gè)控制臺(tái)應(yīng)用程序,完成下列功能,并回答提出的問題。
(1)創(chuàng)建一個(gè)類A,在構(gòu)造函數(shù)中輸出“A”,再創(chuàng)建一個(gè)類B,在構(gòu)造函數(shù)中輸出“B”。(2)從A繼承一個(gè)名為C的新類,并在C內(nèi)創(chuàng)建一個(gè)成員B。不要為C創(chuàng)建構(gòu)造函數(shù)。(3)在Main辦法中創(chuàng)建類C的一個(gè)對(duì)象,寫出運(yùn)行程序后輸出的結(jié)果。
(4)假如在C中也創(chuàng)建一個(gè)構(gòu)造函數(shù)輸出“C”,囫圇程序運(yùn)行的結(jié)果又是什么?
【解答】
usingSystem;
publicclassA
{
publicA()
{
Console.WriteLine("A");
}
}
publicclassB
{
publicB()
{
Console.WriteLine("B");
}
}
publicclassC:A
{
Bnewb=newB();
}
classMainClass
{
publicstaticvoidMain()
{
Cnewc=newC();
Console.ReadLine();
}
}
輸出結(jié)果:
B
A
假如在C中也創(chuàng)建一個(gè)構(gòu)造函數(shù)輸出“C”,即添加:
publicC()
{
Console.WriteLine("C");
}
則囫圇程序運(yùn)行的結(jié)果為:
B
A
C
4、編寫一個(gè)控制臺(tái)應(yīng)用程序,完成下列功能,并寫出運(yùn)行程序后輸出的結(jié)果。
(1)創(chuàng)建一個(gè)類A,在A中編寫一個(gè)可以被重寫的帶int類型參數(shù)的辦法MyMethod,并在該辦法中輸出傳遞的整型值加10后的結(jié)果。
(2)再創(chuàng)建一個(gè)類B,使其繼承自類A,然后重寫A中的MyMethod辦法,將A中接收的整型值加50,并輸出結(jié)果。
(3)在Main辦法中分離創(chuàng)建類A和類B的對(duì)象,并分離調(diào)用MyMethod辦法。
【解答】
usingSystem;
publicclassA
{
publicvirtualvoidMyMethod(intnum)
{
num+=10;
Console.WriteLine(num);
}
}
publicclassB:A
{
publicoverridevoidMyMethod(intnum)
{
num+=50;
Console.WriteLine(num);
}
}
classMainClass
{
publicstaticvoidMain()
{
Anewa=newA();
newa.MyMethod(2);
Bnewb=newB();
newb.MyMethod(2);
Console.ReadLine();
}
}
輸出結(jié)果:
12
52
5、假設(shè)Node類的每一個(gè)節(jié)點(diǎn)包括有兩個(gè)字段:m_data(引用節(jié)點(diǎn)的數(shù)據(jù))和m_next(引用鏈接列表中的下一項(xiàng))。這兩個(gè)字段都是由構(gòu)造函數(shù)辦法設(shè)置的。該類有兩個(gè)功能,第一個(gè)功能是通過名為Data和Next的只讀屬性拜訪m_data和m_next字段。其次個(gè)功能是對(duì)System.Object的ToString虛擬辦法舉行重寫。試分離用類和泛型兩種辦法編寫程序?qū)崿F(xiàn)上述功能。
【解答】
usingSystem;
classNode
{
Objectm_data;
Nodem_next;
publicNode(Objectdata,Nodenext)
{
m_data=data;
m_next=next;
}
//拜訪結(jié)點(diǎn)數(shù)據(jù)
publicObjectData
{
get{returnm_data;}
}
//拜訪下一個(gè)結(jié)點(diǎn)
publicNodeNext
{
get{returnm_next;}
}
//獵取結(jié)點(diǎn)數(shù)據(jù)描述
publicoverrideStringToString()
{
returnm_data.ToString();
}
}
//鏈表結(jié)點(diǎn)類的泛型定義
classNode
{
Tm_data;
Nodem_next;
publicNode(Tdata,Nodenext)
{
m_data=data;
m_next=next;
}
//拜訪結(jié)點(diǎn)數(shù)據(jù)
publicTData
{
get{returnm_data;}
set{m_data=value;}
}
//拜訪下一個(gè)結(jié)點(diǎn)
publicNodeNext
{
get{returnm_next;}
set{m_next=value;}
}
//獵取結(jié)點(diǎn)數(shù)據(jù)描述
publicoverrideStringToString()
{
returnm_data.ToString();
}
}
//使用結(jié)點(diǎn)類型或泛型結(jié)點(diǎn)類型
classLinkedList
{
staticvoidMain(string[]args)
{
////創(chuàng)建整數(shù)鏈表
//Nodehead=newNode(5,null);
//head=newNode(10,head);
//head=newNode(15,head);
////遍歷鏈表求整數(shù)和
//Int32sum=0;
//for(Nodecurrent=head;current!=null;
//current=current.Next)
//{
//sum+=(Int32)current.Data;
//}
////輸出結(jié)果
//Console.WriteLine("Sumofnodes={0}",sum);
//用泛型創(chuàng)建整數(shù)鏈表
Nodehead=newNode(5,null);
head=newNode(10,head);
head=newNode(15,head);
//遍歷求和
Int32sum=0;
for(Nodecurrent=head;current!=null;
current=current.Next)
{
sum+=current.Data;
}
//輸出
Console.WriteLine("Sumofnodes={0}",sum.ToString());
}
}
1、使用保持銜接方式編寫程序,計(jì)算各年級(jí)平均成果,并顯示結(jié)果。【解答】
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace習(xí)題9_1
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
//添加Button按鈕在ListBox中顯示結(jié)果
privatevoidbutton1_Click(objectsender,EventArgse)
{
listBox1.Items.Add("年級(jí)平均成果");
stringconnectionString=Properties.Settings.Default.MyDatabaseConnectionString;
//按照銜接字符串創(chuàng)建SqlConnection實(shí)例
SqlConnectionconn=newSqlConnection(connectionString);
//創(chuàng)建SqlCommand實(shí)例,并設(shè)置SQL語(yǔ)句和使用的銜接實(shí)例
SqlCommandcmd=newSqlCommand();
cmd.CommandText="selectsubstring(學(xué)號(hào),1,2)as年級(jí),avg(成果)as平均成果fromMyTable2groupbysubstring(學(xué)號(hào),1,2)";
cmd.Connection=conn;
try
{
conn.Open();
SqlDataReaderr=cmd.ExecuteReader();
while(r.Read()==true)
{
listBox1.Items.Add(string.Format("{0}級(jí){1}",r[0],r[1]));
}
r.Close();
}
catch(Exceptionerr)
{
MessageBox.Show(err.Message,"計(jì)算成果失敗");
}
finally
{
conn.Close();
}
}
}
}
2、使用保持銜接方式編寫程序,查詢MyTable2中不及格同學(xué)的學(xué)號(hào)、姓名、性別和成果。并將結(jié)果在ListBox中顯示出來(lái)。
【解答】
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace習(xí)題9_2
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
listBox1.Items.Add("學(xué)號(hào)姓名性別成果");
stringconnectionString=Properties.Settings.Default.MyDatabaseConnectionString;
//按照銜接字符串創(chuàng)建SqlConnection實(shí)例
SqlConnectionconn=newSqlConnection(connectionString);
//創(chuàng)建SqlCommand實(shí)例,并設(shè)置SQL語(yǔ)句和使用的銜接實(shí)例
SqlCommandcmd=newSqlCommand();
cmd.CommandText=
"Select學(xué)號(hào),姓名,性別,成果FromMyTable2Where(成果18",conn);
DataSetdataset=newDataSet();
adapter.Fill(dataset,"person");
dataGridView1.DataSource=dataset.Tables["person"];
adapter.Dispose();
}
catch(Exceptionerr)
{
MessageBox.Show(err.Message);
}
finally
{
conn.Close();
}
}
}
}
5、調(diào)用存儲(chǔ)過程,設(shè)計(jì)程序完成下列功能:隨意給出一個(gè)漢字,統(tǒng)計(jì)MyTable2中全部包含該漢字的人數(shù),并顯示統(tǒng)計(jì)結(jié)果。
【解答】
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace習(xí)題9_5
{
publicpartialclassForm1:Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
SqlConnectionconn=
newSqlConnection(Properties.Settings.Default.MyDatabaseConnectionString);
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
//設(shè)置SQL語(yǔ)句為存儲(chǔ)過程名,命令類型為存儲(chǔ)過程
cmd.CommandText="
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 出國(guó)出境勞務(wù)合同范本
- 北京土方備案合同范本
- 廠房水電安裝合同范本
- 副食進(jìn)貨合同范本
- 合同范本模板收費(fèi)
- 南園新村租房合同范本
- 會(huì)議安保合同范本
- 合伙開汽貿(mào)合同范例
- 包裝面包售賣合同范本
- 公司股權(quán)買賣合同范本
- DL∕T 478-2013 繼電保護(hù)和安全自動(dòng)裝置通 用技術(shù)條件 正式版
- DL∕T 516-2017 電力調(diào)度自動(dòng)化運(yùn)行管理規(guī)程
- 《原來(lái)數(shù)學(xué)這么有趣》小學(xué)數(shù)學(xué)啟蒙課程
- 中醫(yī)內(nèi)科臨床診療指南-塵肺病
- DZ∕T 0399-2022 礦山資源儲(chǔ)量管理規(guī)范(正式版)
- 2024年鄂爾多斯市國(guó)資產(chǎn)投資控股集團(tuán)限公司招聘公開引進(jìn)高層次人才和急需緊缺人才筆試參考題庫(kù)(共500題)答案詳解版
- 競(jìng)賽試卷(試題)-2023-2024學(xué)年六年級(jí)下冊(cè)數(shù)學(xué)人教版
- 《研學(xué)旅行課程設(shè)計(jì)》課件-辨識(shí)與研學(xué)旅行場(chǎng)混淆的概念
- 部編版道德與法治三年級(jí)下冊(cè)教案全冊(cè)
- 山西眾輝電力服務(wù)公司歷年真題
- 修建蓄水池施工合同協(xié)議書范本
評(píng)論
0/150
提交評(píng)論