首页 > 文库大全 > 精品范文库 > 15号文库

C#里面对namespace理解(基础篇)

C#里面对namespace理解(基础篇)



第一篇:C#里面对namespace理解(基础篇)

C#里面对namespace理解(基础篇)收藏

.NET Framework类库是一个由Microsoft.NET Framework SDK 中包含的类,接口和值类型组成的库.该库对系统功能的访问是建立在.NET Framework应用程序,组件和控件的基础上的.整个.NET Framework类库就是由很多极大的命名空间组成,所以说命名空间就是MS.NET的核心一点也不为过,相信下面的很多命名空间初学者都有过接触: System 包含用于定义常用值和引用数据类型、事件和事件处理程序、接口、属性和处理异常的基础类和基类。

其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序环境管理以及对托管和非托管应用程序的监管。System.Data 基本上由构成 ADO.NET 结构的类组成。ADO.NET 结构使您可以生成可用于有效管理来自多个数据源的数据的组件。在断开连接的方案(如 Internet)中,ADO.NET 提供了一些可以在多层系统中请求、更新和协调数据的工具。ADO.NET 结构也可以在客户端应用程序(如 Windows 窗体)或 ASP.NET 创建的 HTML 页中实现。System.web 提供支持浏览器/服务器通信的类和接口。此命名空间包括 HTTPRequest 类(它提供有关当前 HTTP 请求的大量信息),HTTPResponse 类(它管理 HTTP 到客户端的输出)和 HTTPServerUtility 对象(它提供对服务器端实用工具和进程的访问)。System.Web 还包括用于 Cookie 操作、文件传输、异常信息和输出缓存控制的类。System.Windows.Forms 包含用于创建基于 Windows 的应用程序的类,这些应用程序可以充分利用 Microsoft Windows 操作系统中的丰富用户界面功能。

.NET框架的每个命名空间还包含近100 个类,并且很多命名空间还包含许多二级命名空间。

从运行库(SDK)的角度看,命名空间只是类型名称的一个集合。特定语言所具有的构造和相应的语法可帮助开发人员构成类型的逻辑组,但运行库在绑定类型时并不使用这些构造。因此,Object 和 String 类都是 System 命名空间的一部分,但运行库只识别每个类型的完整名称,它们分别为 System.Object 和 System.String。

可以构建单个程序集,它使类型看上去像来自两个不同的分层命名空间,如

System.Collections 和 System.Windows.Forms;也可以构建两个程序集,均可导出名称中包含 MyDll.MyClass 的类型;通俗点说namespace可以在不同的文件(这里面指的是输出文件,如.dll或者.exe),同时,在同一个程序集里面也可以有多个namespace。

namespace不相当于物理形式存在,与Java不同,Java里面的package对应的是目录,所以,你如果用到java.io.****的话,你得建一个三层目录,比较麻烦;上面讲了这么多,大家可能有些疑问,这样用namespace做有什么好处?

namespace不是类,也不是对象,它只是类与及其它类型声明的一个包容体,为了让这些类排列的更有顺序,更加利于别人寻找。尽最大程度排除重名。namespace相当于指定了一个寻址方式,告诉编译系统你可以在哪儿找到工程所用的方法。

我们可以通过已经有的NAMESPACE(微软把各种.net预编写好的各种类按照其功能进行分类的一些集合:如上面介绍的System,System.Data),做我们想做的和愿意做的,当然如果你感觉现有的还不够你的使用的话,你也可以自己来使用现有的来扩展,创建自己的NAMESPACE!这样也有助于我们从逻辑上把一个系统分类,譬如把跟Web有关的东西放一起(System.Web),把Windows有关的东西放一起(System.Windows.Forms),这样好处就不言而喻了!

下面举几个简单的namespace例子: 1.上一篇文章讲的DLL文件的生成里面就有一个关于namespace的例子,看了我想对初学者可以有不少帮助;2.namespace N1 { public class A{…} //N1.A }

namespace N2

public class A{…} //N2.A } 类A定义于各自的命名空间namespace;现在类A对整个程序来说不再是可见Visible 的了。而如若我们在程序中同时“开启”(或叫“曝光”expose)两个命名空间,那么unqualified name A将导致编译错误;此时我们必须使用“全饰名称”,如N2.A a;另外,N1和N2可以相同,编译器认为我们想在原先存在的命名空间中加入新声明declare(这样的话,两个class A就不能重名了);顺便提一下:在C#程序中,每提及一个“名称”,编译器就要对这个“名称”进行“决议(resolve)”,简言之,就要让这个“名称”可见(Visible)。

如下例: Console.WriteLine(“Hello ,EdgarSun!”);此时编译器将无法对Console这一“名称”进行决议(resolve);这种情况和C中用strcpy()函数而没包含string这个头文件一样,C中是这样解决的:#include 。C#中using指令提供了一种解决方案:using System;这条指令告诉编译器到System这个“命名空间”中查找无法决议(resolve)的“名称”;另一种说法是明确告诉编译器到何处去寻找(look for)这个“名称”,那就是我们所说的“全饰名称(full qualified name)”: System.Console.WriteLine(“Hello,EdgarSun!”)3.其它的例子就不多举了,书上有很多很好的例子,希望上面的一些总结能够给大家一些帮助;

第二篇:c#基础总结

【1】面向对象程序设计语言的三大特点是什么?答:【23】Main函数特点?答:1)Main方法是C#程序的限定,默认的为private【2】三种命名法则的特点?答:匈牙利命名法:在变限定符,返回类型为void或int类型,Main方法必须是静态方法;3)一个类或结构只能有一个有效的入驼表示法:一个标示符用若干个有意义的英语单词或口点函数;4)main方法必须定义在某一个类中。缩写组成,第一个单词首字母小写,后边的首字母大【24】多态(重写、隐藏)

写;Pascal表示法:与骆驼表示法类似,但是第一个 单词的首字母也需要大写。【3】C#注释的三种形式特点?答1)单行注释:// 2)class A /// {public virtual void F()【4】引用类型和值类型的数据类型? {Console.WriteLine(“A.F”);}} abstract class B:A(1)int valOne = 0;{public abstract override void F();}int valTwo = valOne;答:abstract override 是不可以一起修饰 int valThree = 333;例:在下面的例子里 valTwo = 333;TestValueRefRef1 = new TestValueRef();class A TestValueRefRef2 = Ref1;{public A(){PrintFields();} Ref2.value = 444;public virtual void PrintFields(){} } Console.WriteLine(“values:{0},{1}”, Ref1.value, class B:A Ref2.value);{int x=1;int y;public B(){y=-1;} Console.WriteLine(“values:{0}, {1},{2}”,valOne, public override void valTwo,valThree);PrintFields(){Console.WriteLine(“x={0},y={1}”,答:输出结果:values:444,444 x,y);} 当使用new B()创建B的实例时,产生什么输出?(2)public class EnumTest答:x=1,y=0 { enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};分析:执行new B()时,由于B继承自A,所以会调用static void Main()A的构造函数,并执行其中的PrintFields()方法,由{int x=(int)Days.Sun;inty=(int)Days.Fri;于该方法为虚方法,在继承类中被重写,所以,将执Console.WriteLine(“Sun = {0}”, x);行B中的PrintFields()方法。此时,将在DOS界面Console.WriteLine(“Fri = {0}”, y);}} 上输出“x=1,y=0”,然后,在运行B的构造函数中的答:输出结果:Sun = 2Fri = 7 y=-1。(建议同学们将此程序放入到代码中,设置断点【5】枚举类型的字段和关联值?枚举类型有三个要看执行过程。)

【25】什么是多态对象?答:用基类类名声明,但是特性 修饰符 enum 枚举名 : 基础类型 {枚举成员声明,枚举成员声明,„„,枚举成员声明}默认的基础函数来实例化的对象。这类对象的主要用途是引发多类型为int;关联值:如果没有被声明,默认为0。态,为了将它们和一般的对象(声明和创建都使用同【6】强制类型转换(例:若有double f=2.7;int 一个类型名的对象)加以区别、揭示它们的特点和用2)将源类型的对象途,这种形态的对象称为多态对象。转换成为目的类型的对象 【26】接口的特点。答:接口只能包含抽象方法,不【7】运算符&和&&的区别?答:条件“与”运算符(&&)没有访问修饰符,接口成员必须是方法属性事件或者时才计算第二个操作数。而&需要计算所有操作数,索引器不能包含常数字段运算符也不能有静态成员。并且优先级高于&& 【27】委托和事件,【8】装箱和拆箱的概念?答:装箱就是把一个值类型委托的定义修饰符 delegate 返回类型 委托类型名(参数列表); 【9】for循环和if语句联合使用的程序分析,for(;;)eg: public delegate int DelegateClass(stringinfo);

和continue的区别?答:break跳出循委托的创建(实例化)委托对象 = new 委托名(关联方法); 【11】命名空间的特点答:它提供一种命名机制,是eg: DelegateClass obj=new DelegateClass(MethodA);

合方式无关,不能表明源文件的存取方式,命名空间DelegateClass obj=MethodA;//隐式创建和初是按层次组织的。始化(不用new)【12】数组元素的的复制和读值 例:分析下列语句: int[3]{5,6,2},new int[5]{6,9,7,8,3},new Hello(string target);} int[2]{3,2}};myArray3[2][2]的值是(D)A)9;B)2;该语句的作用是:在TestCS 命名空间中定义了了一C)6;D)越界 个名为Hello 的委托类型;

【13】类和对象的关系?答:类是对象的抽象,对象【28】Windows窗体中Button按钮触发的事件是什【14】关键字this和base的区别?答:base指代基【29】Windows窗体中控件的标识符如何修改?答:【15】关键字new、using的多种用法?答:new修饰【30】如何修改Windows窗体的启动窗体?答:修改被重写,但new修饰符可终止这一特性;向下传播; 实例化一个对象。Using:导入命名空间;自动释放【31】要使用SQL Server需要使用哪两个命名空间? Using代码框里的资源。【16】索引器的特点?答:索引器允许重载;字符串Using System.Date.SqlClient: 【32】什么是DataSet、DataAdapter?两者联系?答:过签名标识;通过元素访问来访问;必须为实例成员;索引器的get访问器具有与索引器相同的形参表;除DataAdapter:数据适配器,数据库与DataSet间的桥value参数外,索引器的set访问器还具有与索引器梁,把数据库中数据下载到DataSet或回传回去。相同的形参表。【33】用户登录和密码修改(带数据库)【17】静态数据成员特点?答:为所有类所共享,区用户登录 【18】构造函数的特点?答:(1)构造函数名与类名UserName='“ + txtUsername.Text.Trim().ToLower()+ ”' and UserPwd='“ + txtPassword.Text.Trim()+ 【19】析构函数的特点?答:(1)析构函数名是在类”'“;if(OperateDB.ExecuteReader(sql))型(默认为空)和修饰符;(3)析构函数不能被重载。{username = txtUsername.Text.Trim().ToLower();【20】什么是方法的重载?重载的特点是什么?答: frmMain frm = new frmMain();frm.Show();this.Hide();} 定义一组方法。重载的特点:1)位于同一类中;2)else

方法名相同;3)方法参考列表不同,包括参数个数不{MessageBox.Show(”用户名或密码错误“, ”出错了“, 同和参数类型不同;4)与方法返回值和修饰符没关系。MessageBoxButtons.OK, MessageBoxIcon.Error);} 【21】虚函数的特点?答:1)虚方法前不允许有修改密码: 修饰符;2)虚方法不能是私有的,因此不能使用private修饰符; where UserName='” + frmLogin.username + “' and 【22】抽象类和抽象方法的主要特点?答:抽象类:UserPwd='” + txtOldPwd.Text.Trim()+ “'”;(或者if(OperateDB.ExecuteReader(sqlCheckPwd))说,不能产生对象。但是,它可以有构造函数。(2){string sql = “update UserInfo set UserPwd='” 设计abstract类的目的是为了被继承。抽象方法:是+ txtNewPwd.Text.Trim()+ “' where UserName='” + 不完整的,不能执行的。frmLogin.username + “'”;

if(OperateDB.ExecuteNonQuery(sql)== 1)

{MessageBox.Show(“密码修改成功!”);}else

{ MessageBox.Show(“密码修改失败!”);}}

else{MessageBox.Show(“旧密码不正确!”);}

【34】抽象类定义和继承使用

特点:1.没有被完整定义,因而它不能用来实例化,或者说,不能产生对象。(但是,它可以有构造函数。)2.设计abstract类的目的是为了被继承。public abstract class Employee{public virtual void Pay(){ }

public abstract void CalculatePay();} public class HourlyEmployee: Employee

{public override void Pay(){CalculatePay();}public override void CalculatePay(){ }} 【35】接口及继承类的使用

特定功能的抽象成员的集合。一个类可以继承多个接口,从而获得多个行为的描述,将它们组合成新的功能并在类中实现。继承类中必须实现接口中的所有抽象成员。

定义接口的格式:修饰符 interface 接口名:基接口列表 {接口体} 其中,接口体的声明可以包括:接口方法声明;接口属性声明;接口事件声明;接口索引器声明

public delegate void

StringListEvent(IStringList sender);public interface IStringList{ void Add(string s);//方法int Count{get;}//属性event StringListEvent Changed;//事件string this[int index]{get;set;}//索引器} 【编程题例题】

定义一MobilePhone类,包括属性成员——网络类型(NetworkType),字段成员——屏幕尺寸(screenSize)、手机品牌(brand),手机型号

(brandModel),公共方法成员——Open、Close。其中screenSize为单位是英寸的双精度数,brand为字符串,NetworkType只能是“GSM”或“CDMA”字符串。要求:(1)在此类中包含构造函数,构造函数用于对数据(屏幕尺寸、手机品牌和手机型号)进行初始化。(2)公共成员方法中输出相应提示信息(参见(3)中的输出结果格式)。(3)写一测试类,在类中实例化一MobilePhone对象,最后能在DOS界面下显示如下结果:诺基亚N81(屏幕尺寸2.0英寸),是一款GSM手机。手机关机了。using System;

public enum NetworkType {GSM,CDMA,}

public class MobilePhone {public double screenSize;public string brand;

public string brandModel;

public NetworkType networkType;public NetworkType NetworkType{get { return networkType;}}

public MobilePhone(double ss, string bra, string bm, NetworkType nt){screenSize = ss;brand = bra;brandModel = bm;networkType = nt;}public void Open()

{Console.WriteLine(“{0}{1}(屏幕尺寸{2}英寸),是一款{3}手机.”,brand,brandModel,screenSize.ToString(“.0”), networkType);}

public void Close()

{Console.WriteLine(“手机关机了。”);} }

public class Test

{public static void Main()

{MobilePhone mp = new MobilePhone(2.0, “诺基亚”, “N81”, NetworkType.GSM);mp.Open();mp.Close();

System.Console.ReadLine();} }

【例】写一名为Desk的类,包含两个字段Length(双精度类型)、Height(双精度类型)。再写一继承类ComputerDesk类。ComputerDesk类除了有Length和Height外,还有KeyboardTray(字符串类型)。Public class desk {double length;double height;}

Public class computerdesk:desk {string keyboardtray}

第三篇:初中英语阅读理解基础篇

A Once upon a time, there lived a rich man.He had a servant(仆人).He and the servant loved wine and good food very much.Each time the rich man left his home, the servant would drink the wine and eat up all the nice food in the house.The rich man knew what his servant did, but he had never caught his servant doing that.One morning, when he left home, he said to the servant, “Here are two bottles of poison(毒药)and some nice food in the house.You must take of them.” With these words, he went out.But the servant knew that the rich man had said was untrue.After the rich man was away from his home, he enjoyed a nice meal.Because he drank too much, he was drunk and fell to the ground.When the rich man came back, he couldn’t find his food and his wine.He became very angry.He woke the servant up.But the servant told his story very well.He said a cat had eaten up everything.He was afraid to be punished(惩罚), so he drank the poison to kill himself.()26.In the story, _______ liked wine and good food very much.A.the rich man

B.the servant

C.both A and B

D.neither A and B

()27.The rich man knew that it was _______ that drank the wine and ate up all the nice food.A.the cat

B.himself

C.nobody

D.the servant()28.The rich told the servant that there was poison in the two bottles, because ________.A.there was in fact poison in the bottles

B.did not want the servant to drink his wine

C.he wanted to kill the cat

D.he wanted to kill the servant

()29.In fact, _______ ate all the nice food and drank the wine.A.the servant

B.cat

C.the rich man

D.nobody()30.From the story, we know that the servant is very _______.A.lazy

B.bad

C.clever

D.kind

B Everyone likes living in a clean and comfortable environment.If the environment(环境)are bad, it will affect(影响)our body, and make us not feel well.Sometimes we may be terribly ill.At that time we don’t want to work, and we have to stay in bed and rest at home.So the environment is very important to us.It’s germs that makes us ill.There are germs everywhere, They are very small and you can’t find them with your own eyes, but you can see them with a microscope(显微镜)They are very small and there may be hundreds of them on a very small thing, Germs can always be found in dirty water.When we look at dirty water under the microscope, we shall see them in it.Germs can also be found in air and dust(灰尘).If you cut your finger, some of the dust from the floor may go into it, and you will have much pain in it.Sometimes the germs will go into all of your body, and you will have pain everywhere.To keep us healthy, we should try to our best to make our environment become cleaner and tidier.This needs us to act together.31.The writer tell us that________.A.we like working when we are ill

B.germs can’t live in the water.C.we can’t feel ill if the environment is bad.D.we feel well when the environment is good.32.Germs are________.A.very small things that you can’t see with your eyes.B.the things that don’t affect people.C.the things that you can find with your eyes.D.the things that are very big.33.Where can germs be found? They can be found_________.A.on the small thing

B.in air and dust C.only in dirty water

D.everywhere 34.How will you feel if germs go into the finger that is cut? A.I will feel nothing.B.I won’t mind.C.I will feel tense.D.I will feel painful.35.From the passage we know that________.A.environment doesn’t affect our life

B.we don’t need to improve our environment C.germs may make us ill

第四篇:C#基础编程设计实验报告

C# 基础编程 设计实验报告

一、实验目的

1、熟悉 Visual Studio.NET 开发环境。

2、掌握 C#应用程序的基本操作过程。

3、掌握 C#的数据类型,运算符以及表达式的使用。

4、掌握分支和循环语句的使用方法。

5、掌握一维数组,二维数组及数组型数组的使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有 Visual Studio.NET 软件。

四、实验步骤

1、分析题意。

2、根据题目要求,新建项目。

3、编写并输入相关的程序代码。

5、运行与调试项目。

6、保存项目。

五、实验内容

1、编写一个简单的控制台应用程序,打印一行文字(如你的姓名)。

using System;using System.Collections.Generic;

using System.Linq;using System.Text;

namespace one.first {

class Program

{

static void Main(string[] args)

{

System.Console.WriteLine(“我叫王蕾!”);

}

} } 2、编写一个简单的 Windows 应用程序,在窗体 Load 事件中书写代码,标签中显示你的姓名。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;

using System.Windows.Forms;

namespace one.second {

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

this.Text = “Windows 程序”;

Label lblShow = new Label();

lblShow.Location = new Point(20, 30);

lblShow.AutoSize = true;

lblShow.Text = “王蕾!”;

this.Controls.Add(lblShow);

}

}

} 3、编写一个一个程序,用来判断输入的是大写字母,小写字母,数字还是其他的字符。

using System;using System.Collections.Generic;using System.Text;

namespace one.third {

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“请输入一个字符:”);

char c = Convert.ToChar(Console.ReadLine());

if((c>=“a”&&c<=“z”)||(c>=“A”&&c<=“Z”))

Console.WriteLine(“这是一个字母”);

if(char.IsDigit(c))

Console.WriteLine(“这是一个数字”);

}

}

} 4、分别用 while,do-while,for 循环求 1 到 100 的和。

using System;using System.Collections.Generic;using System.Text;

namespace one.forth.one {

class Program

{

static void Main(string[] args)

{

int i = 1, sum = 0;

while(i <= 100)

{

sum = sum + i;

i++;

}

Console.WriteLine(“1 到 100 的自然数之和为:” + sum);

}

}

} using System;using System.Collections.Generic;using System.Text;

namespace one.forth.two {

class Program

{

static void Main(string[] args)

{

int i = 1, sum = 0;

do

{

sum = sum + i;

i++;

}

while(i <= 100);

Console.WriteLine(“1 到 100 的自然数的和为:” + sum);

}

}

} using System;using System.Collections.Generic;using System.Text;

namespace one.forth.three {

class Program

{

static void Main(string[] args)

{

int i , sum = 0;

for(i = 1;i <= 100;i++)

{

sum = sum + i;

}

Console.WriteLine(“1 到 100 的自然数的和为:” + sum);

}

} } 5、定义一个一维数组,用随机数为此赋值,用 foreach 循环输

出其中的内容。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace first.five {

class Program

{

static void Main(string[] args)

{

int[] a = {0,1,2,3,4};

foreach(int i in a)

{

Console.WriteLine(a[i]);

}

}

} } 6、实现二维数组的输入和输出。

using System;

using System.Collections.Generic;using System.Linq;using System.Text;

namespace first.six {

class Program

{

static void Main(string[] args)

{

int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

{

for(int i = 0;i < 2;i++)

{

for(int j = 0;j < 3;j++)

{ Console.WriteLine(a[i, j]);}

}

}

}

} }

7、实现数组型数组的输入和输出。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace first.seven {

class Program

{

static void Main(string[] args)

{

int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };

for(int i = 0;i < a.Length;i++)

{

for(int j = 0;j < a[i].Length;j++)

{

Console.WriteLine(a[i][j]);

}

}

}

} } 六、实验体会(遇到问题及解决办法,编程后的心得体会)

刚开始编程的时候觉得无从下手,尽管我们已经学了好几种高级编程语言,但每个都有其独特的地方,稍不留神就会混淆。

通过这次实验,我体会到课后复习巩固的重要性。在编程的时候,很多内容都不记得,需要去翻书。不得不说,实验是巩固课程的好方法!本次实验,我熟悉 Visual Studio.NET 开发环境;掌握了 C#应用程序的基本操作过程;掌握了 C#的数据类型,运算符以及表达式的使用;掌握了分支和循环语句的使用方法以及一维数组,二维数组及数组型数组的使用。

实验项目名称:

类与对象

实验学时:

同组学生姓名:

实验地点:

1318

实验日期:

月 26 日-11 月 9 日 实验成绩:

批改教师:

批改时间:

实验 2

类与对象

一、实验目的、要求

(1)掌握类的定义和使用;(2)掌握类的数据成员,属性的定义和使用;(3)掌握方法的定义,调用和重载以及方法参数的传递;(4)掌握构造函数的定义和使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有 Visual Studio.NET 软件。

四、实验步骤

1、分析题意; 2、根据题目要求,新建项目; 3、编写并输入相关的程序代码; 5、运行与调试项目; 6、保存项目。

五、实验内容

1、定义一个方法,实现两个数的交换(分别把参数按值传递和按引用传递)。

using System;

using System.Collections.Generic;using System.Text;

namespace second.one {

class Program

{

static void Main(string[] args)

{

Swaper s = new Swaper();

Console.WriteLine(“输入 x 的值:”);

int a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(“输入 y 的值:”);

int b=Convert.ToInt32(Console.ReadLine());

Console.WriteLine(s.Swap(a, b));

Console.WriteLine(s.Swap(ref a,ref b));

}

class Swaper

{

public string Swap(int x, int y)

{

int temp;

temp = x;

x = y;

y = temp;

return string.Format(“按值传参交换之后:x={0},y={1}”,x,y);

}

public string Swap(ref int x, ref int y)

{

int temp;

temp = x;

x = y;

y = temp;

return string.Format(“按引用传参交换之后:x={0},y={1}”, x, y);

}

}

} }2、定义一个方法,实现数组的排序。

using System;using System.Collections.Generic;using System.Text;

namespace second.two {

class Program

{

public class sort

{

public void change(int[] a)

{

Console.WriteLine(“排序前,数组顺序为:”);

show(a);

int i, j, m;

for(i = 0;i < 10;i++)

{

m = a[i];

j = i-1;//a[j]为数组前一个值

while(j >= 0 && m > a[j])//判断 i 下标的数是否大于 j 下标的数

{

a[j + 1] = a[j];//如果 i 下标大于j 把 j 往后移一个位

j--;

}

a[j+1] = m;//当不大于 j 的时候就把 M的值放到 i 下标下面 j+1 是为了下标减到最前时考虑-1 + 1 还是下标的最前面

}

Console.WriteLine(“排序后,数组顺序为:”);

show(a);

}

void show(int[] a)

{

int i;

for(i = 0;i < 10;i++)

{

Console.Write(“{0} ”, a[i]);

}

Console.WriteLine();

}

}

static void Main(string[] args)

{

int[] a ={ 4, 7, 1, 2, 5, 8, 9, 10, 3, 6 };

sort s=new sort();

s.change(a);

}

} } 3、定义一个学生类,把学生类当作对象来传递。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace second.three {

class Program

{

public class student

{

public void st()

{

int a = 999;

}

}

public class st

{

public void aa(student s)

{

Console.WriteLine(s);

}

}

static void Main(string[] args)

{

student s=new student();

st s1 = new st();

s1.aa(s);

}

} } 4、定义一个方法,求两个数的和和差,通过参数把这两个值带回。

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace

second.four

{

class Program

{

public class sum

{

public void ab(out int m, out

int n,int a, int b)

{

m = a + b;

n = a-b;

}

}

static void Main(string[] args)

{

sum s = new sum();

int a = 10;

int b = 3;

int m, n;

s.ab(out m, out n, a, b);

Console.WriteLine(“{0}+{1}={2};{0}-

{1}={3}”,a,b,m,n);

}

} } 5、用构造函数重载,实现矩形的面积,圆的面积,梯形的面积; using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace secong.five {

class Program

{

public class square

{

public double area;

public square(){ }

public square(double a)

{

area = a * a * 3.14;

}

public square(double a, double b)

{

area = a * b;

}

public square(double a, double b, double h)

{

area =(a + b)/ 2 * h;

}

}

static void Main(string[] args)

{

double a, b, h,area;

a = 2;b = 5;h = 3;

square s = new square(a,b);

Console.WriteLine(“求矩形面积,长为 a={0},宽为 b={1},面积 area={2}”,a,b,s.area);

square i = new square(a);

Console.WriteLine(“求圆形面积,半径 a={0},面积 area={1}”, a, i.area);

square j = new square(a, b, h);

Console.WriteLine(“求梯形面积,上底为a={0},下底为 b={1},高为 h={2}面积 area={3}”, a, b,h, j.area);

}

} } 6、设计一个 windows 应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号,姓名,语文,数学和英语成绩,要求:

1)能查询每个学生的总成绩。

2)能显示全班前三名的名单。

3)能显示单科成绩最高分和不及格的学生名单。

4)能统计全班学生的平均成绩。

5)能显示各科成绩不同分数段的学生人数的百分比。

Student 类:

using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {

public class Student

{

public string stuNo;

public string name;

public double chinese;

public double math;

public double english;

public double sumScore

{

get { return chinese + math + english;}

}

} } StudentList 类:

using System;using System.Collections.Generic;using System.Text;namespace Test2_6 {

public class StudentList:Student

{

int snums;

public Student[] stu=new Student[50];

public StudentList()

{

snums = 0;

}

public void addstu(Student s)

{

stu[snums] = s;

snums++;

}

public int searchstu(string name)

{

int i;

for(i = 0;i < snums;i++)

{

if(stu[i].name == name)break;

}

if(i == snums)return-1;

else return i;

}

//给所有成绩排序,用后面实现前三名的排名

public void ProThree()

{

for(int i = 0;i < snums;i++)

{

int k = i;

for(int j = i + 1;j < snums;j++)

if(stu[j].sumScore > stu[k].sumScore)k = j;

if(k!= i)

{

Student temp;

temp = stu[k];

stu[k] = stu[i];

stu[i] = temp;

}

}

}

//显示单科成绩的最高分

public int HighScore(int k)

{

int p = 0;

if(k == 0)

{

for(int i = 1;i < snums;i++)

if(stu[i].math > stu[p].math)p = i;

}

else if(k == 1)

{

for(int i = 1;i < snums;i++)

if(stu[i].chinese > stu[p].chinese)p = i;

}

else

{

for(int i = 1;i < snums;i++)

if(stu[i].chinese > stu[p].chinese)p = i;

}

return p;

}

//显示不及格名单

public string

BuhgName(int k)

{

string name=“ ”;

if(k == 0)

{

for(int i = 0;i < snums;i++)

if(stu[i].math < 60)name +=stu[i].name+“n”;

}

else if(k == 1)

{

for(int i = 0;i < snums;i++)

if(stu[i].chinese < 60)name += stu[i].name + “n”;

}

else

{

for(int i = 0;i < snums;i++)

if(stu[i].english < 60)name += stu[i].name + “n”;

}

return name;

}

public string getHL()

{

string Maxer = “ ”, Loser = “ ”;

Maxer += “ 单 科 数 学 最 高 :

” + stu[HighScore(0)].name + “n”;

Maxer += “ 单 科 语 文 最 高 :

” +

stu[HighScore(1)].name + “n”;

Maxer += “ 单 科 英 语 最 高 :

” + stu[HighScore(2)].name + “n”;

Loser += “单科数学挂科名单:” +BuhgName(0)+ “n”;

Loser += “单科语文挂科名单:” + BuhgName(1)+ “n”;

Loser += “单科英语挂科名单:” + BuhgName(2)+ “n”;

return Maxer + “n” + Loser;

}

//全班的平均成绩

public string SumScore()

{

double sum = 0;

double avg=0;

for(int i = 0;i < snums;i++)

{

sum = sum + stu[i].sumScore;

}

avg = sum / snums;

return “班级总分平均分:”+avg;

}

//各科成绩不同分数段的学生百分比

//英语成绩各分数段百分比

public string PerC()

{

double per1, per2, per3, per4, per5;

double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;

for(int i = 0;i < snums;i++)

{

if((stu[i].chinese > 90)&&(stu[i].chinese <= 100))

{

sumC1++;

}

else if((80 <= stu[i].chinese)&&(stu[i].chinese < 90))

{

sumC2++;

}

else if((70<=stu[i].chinese)&&(stu[i].chinese < 80))

{

sumC3++;

}

else if((60<=stu[i].chinese)&&(stu[i].chinese < 70))

{

sumC4++;

}

else

{sumC5++;}

}

per1 = sumC1 / snums;

per2 = sumC2 / snums;

per3 = sumC3 / snums;

per4 = sumC4 / snums;

per5 = sumC5 / snums;

return “ 语 文 成 绩 百 分 比 :”+“n”+“90~100:”+per1+“

80~90:”+per2+“

80~70:”+per3+“

70~60:”+per4+“

以下的:”+per5;

}

//数学成绩各分数段百分比

public string PerM()

{

double per1, per2, per3, per4, per5;

double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;

for(int i = 0;i < snums;i++)

{

if((stu[i].math> 90)&&(stu[i].math <= 100))

{

sumC1++;

}

else if((80 <= stu[i].math)&&(stu[i].math < 90))

{

sumC2++;

}

else if((70 <= stu[i].math)&&(stu[i].math < 80))

{

sumC3++;

}

else if((60 <= stu[i].math)&&(stu[i].math < 70))

{

sumC4++;

}

else

{ sumC5++;}

}

per1 = sumC1 / snums;

per2 = sumC2 / snums;

per3 = sumC3 / snums;

per4 = sumC4 / snums;

per5 = sumC5 / snums;

return string.Format(“数学成绩百分比:” + “n” + “90~100:” + per1 + “

80~90:” + per2 + “

80~70:” + per3 + “

70~60:” + per4 + “

以下的:” + per5);

}

//英语成绩各分数段百分比

public string PerE()

{

double per1, per2, per3, per4, per5;

double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;

for(int i = 0;i < snums;i++)

{

if((stu[i].english > 90)&&(stu[i].english <= 100))

{

sumC1++;

}

else if((80 <= stu[i].english)&&(stu[i].english < 90))

{

sumC2++;

}

else if((70 <= stu[i].english)&&(stu[i].english < 80))

{

sumC3++;

}

else if((60 <= stu[i].english)&&(stu[i].english < 70))

{

sumC4++;

}

else

{ sumC5++;}

}

per1 = sumC1 / snums;

per2 = sumC2 / snums;

per3 = sumC3 / snums;

per4 = sumC4 / snums;

per5 = sumC5 / snums;

return string.Format(“数学成绩百分比:” + “n” + “90~100:” + per1 + “

80~90:” + per2 + “

80~70:” + per3 + “

70~60:” + per4 + “

以下的:” + per5);

}

} } From 窗体代码:

using System;using System.Collections.Generic;

using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test2_6 {

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public StudentList sl = new StudentList();

private void btnAdd_Click(object sender, EventArgs e)

{

Student s = new Student();

s.stuNo = txtStuNo.Text;

s.name = txtName.Text;

s.chinese = Convert.ToDouble(txtChina.Text);

s.math = Convert.ToDouble(txtMath.Text);

s.english = Convert.ToDouble(txtEng.Text);

sl.addstu(s);

MessageBox.Show(“添加成功”);

}

private void btnSearch_Click(object sender, EventArgs e)

{

int pos = sl.searchstu(this.textBox1.Text);

if(pos!=-1)

{

label7.Text = this.textBox1.Text + “的总成绩:” + sl.stu[pos].sumScore;

}

else { MessageBox.Show(“不存在这个人!”);}

}

private void btnFinish_Click(object sender, EventArgs e)

{

label7.Text = “前 3 名:”+“n”;

for(int i = 0;i < 3;i++)

{

sl.ProThree();

label7.Text+= sl.stu[i].name+“n”;

}

label7.Text += sl.getHL()+“n”;

label7.Text += Convert.ToString(sl.SumScore())+“n”;

label7.Text += sl.PerC()+“n”;

label7.Text += sl.PerM()+“n”;

label7.Text += sl.PerE()+“n”;

}

} }

六、实验体会(遇到问题及解决办法,编程后的心得体会)

通过本次实验,我掌握了类的定义与使用;掌握了类的数据成员,属性的定义和使用;掌握了方法的定义,调用和重载以及方法参数的传递以及构造函数的定义和使用。值得注意的是:本次实验中 return的使用以及所在的位置,类型转换时也经常用到

实验项目名称:

继承与多态

实验学时:

同组学生姓名:

实验地点:

1318

实验日期:月 16 日-11 月 30 日 实验成绩:

批改教师:

批改时间:

实验 3

继承与多态

一、实验目的、要求

(1)掌握类的继承性与多态性;(2)掌握虚方法的定义以及如何使用虚方法实现多态;(3)掌握抽象类的定义以及如何使用抽象方法实现多态; 二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有 Visual Studio.NET 软件。

四、实验步骤

1、分析题意; 2、根据题目要求,新建项目; 3、编写并输入相关的程序代码; 5、运行与调试项目; 6、保存项目。

五、实验内容

1、设计一个 Windows 应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。

Student 类:

using System;using System.Collections.Generic;using System.Text;namespace Test3_1 {

public abstract class Student

{

protected string name;

protected int age;

public static int number;

public Student(string name, int age)

{

this.name = name;

this.age = age;

number++;

}

public string Name

{

get { return name;}

}

public abstract double Average();

}

public class Pupil : Student

{

protected double chinese;

protected double math;

public Pupil(string name, int age, double chinese, double math)

: base(name, age)

{

this.chinese = chinese;

this.math = math;

}

public override double Average()

{

return(chinese + math)/ 2;

}

}

public class Middle : Student

{

protected double chinese;

protected double math;

protected double english;

public Middle(string name, int age, double

chinese, double math, double english)

: base(name, age)

{

this.chinese = chinese;

this.math = math;

this.english = english;

}

public override double Average()

{

return(chinese + math + english)/ 3;

}

}

public class College : Student

{

protected double required;

protected double elective;

public College(string name, int age, double required, double elective)

: base(name, age)

{

this.required = required;

this.elective = elective;

}

public override double Average()

{

return(required + elective)/ 2;

}

} } Form 窗体内的代码:

using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_1 {

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnSmall_Click(object sender, EventArgs e)

{

Pupil p = new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text));

lblShow.Text += “ 总 人 数 :” +Convert.ToString(Student.number)+ “,” + “姓名:” + p.Name + “,” + “小学生” + “,” + “平均成绩为:” + p.Average()+“n”;

}

private void btnMiddle_Click(object sender, EventArgs e)

{

Middle m = new Middle(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text));

lblShow.Text += “ 总 人 数 :” + Convert.ToString(Student.number)+ “,” + “姓名:” + m.Name +

“,” + “中学生” + “,” + “平均成绩为:” + m.Average()+ “n”;

}

private void btnBig_Click(object sender, EventArgs e)

{

College c = new College(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text));

lblShow.Text += “ 总 人 数 :” + Convert.ToString(Student.number)+ “,” + “姓名:” + c.Name + “,” + “大学生” + “,” + “平均成绩为:” + c.Average()+ “n”;

}

} } 2、设计一个 Windows 应用程序,在该程序中定义平面图形抽象类和派生类圆,矩形和三角形。

Figure 类代码:

using System;using System.Collections.Generic;using System.Text;namespace Test3_2

{

public abstract class Figure

{

public abstract double Area();

}

public class Circle:Figure

{

double radius;

public Circle(double r)

{

radius = r;

}

public override double Area()

{

return radius * radius * 3.14;

}

}

public class JUxing:Figure

{

double chang;

double kuan;

public JUxing(double c, double k)

{

this.chang = c;

this.kuan = k;

}

public override double Area()

{

return chang * kuan;

}

}

public class San:Figure

{

double bian;

double heigth;

public San(double b, double h)

{

this.bian = b;

this.heigth = h;

}

public override double Area()

{

return bian * heigth / 2;

}

} } Form 窗体代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Test3_2 {

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnCircle_Click(object sender, EventArgs e)

{

Circle c=new

Circle(Convert.ToInt32(TxtChang.Text));

lblShow.Text = “圆的面积为:” + c.Area();

}

private void btnJu_Click(object sender, EventArgs e)

{

JUxing j = new JUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text));

lblShow.Text = “矩形的面积为:” + j.Area();

}

private void btnSan_Click(object sender, EventArgs e)

{

San s = new San(Convert.ToInt32(TxtChang.Text), Convert.ToInt32(TxtHigh.Text));

lblShow.Text = “三角形的面积为:” + s.Area();

}

} }

3、定义一个 Person 类,包含姓名字段和一个方法,早上 8:30学生开始上课,教师开始讲课。分别用 new 关键字,虚方法,抽象类实现多态性。

New 关键字:

using System;using System.Collections.Generic;using System.Text;

namespace third.three {

class Program

{

static void Main(string[] args)

{

Student s=new Student(“学生”);

Teacher t=new Teacher(“教师”);

Console.WriteLine(s.name+s.work());

Console.WriteLine(t.name+t.work());

Console.ReadLine();

}

}

public class Person

{

public string name;

public interface method

{ string work();}

}

public class Student:Person

{

public Student(string name)

{ this.name = name;}

public string work()

{ return “早上 8:30 开始上课”;}

}

public class Teacher:Person

{

public Teacher(string name)

{ this.name = name;}

public string work()

{ return “开始讲课”;}

} } 虚方法:

using System;

using System.Collections.Generic;using System.Text;

namespace third.three.two {

class Program

{

static void Main(string[] args)

{

Student s = new Student(“张三”,“学生”);

PersonWork(s);

Teacher t=new Teacher(“李斯”,“教师”);

PersonWork(t);

}

private static void PersonWork(Person Person)

{ Console.WriteLine(Person.Work());}

}

public class Person

{

public string name;

public Person(string name)

{ this.name = name;}

public virtual string Work()

{ return string.Format(“Person{0}:早上 8:30 开始”,name);}

}

public class Student : Person

{

private string type;

public Student(string name, string type)

: base(name)

{ this.type = type;}

public override string Work()

{

return string.Format(“Person{0}:早上 8:30 开始上课”, name);

}

}

public class Teacher : Person

{

private string type;

public Teacher(string name, string type)

: base(name)

{ this.type = type;}

public override string Work()

{

return string.Format(“Person{0}:开始讲课”, name);

}

} }

抽象类:

using System;using System.Collections.Generic;using System.Text;

namespace third.three.three {

class Program

{

static void Main(string[] args)

{

Student s = new Student(“张三”, “学生”);

PersonWork(s);

Teacher t = new Teacher(“李斯”, “教师”);

PersonWork(t);

}

private static void PersonWork(Person person)

{

Console.WriteLine(person.Work());

}

}

public abstract class Person

{

public string name;

public Person(string name)

{ this.name = name;}

public abstract string Work();

}

public class Student : Person

{

private string type;

public Student(string name, string type)

: base(name)

{

this.type = type;

}

public override string Work()

{

return string.Format(“Person{0}:早上 8:30 开始上课”, name);

}

}

public class Teacher : Person

{

private string type;

public Teacher(string name, string type)

: base(name)

{

this.type = type;

}

public override string Work()

{

return string.Format(“Person{0}:开始讲课”, name);

}

}

}

六、实验体会(遇到问题及解决办法,编程后的心得体会)

通过本次实验,我理解了类的继承性与多态性;掌握了虚方法的定义以及如何用虚方法来实现多态;掌握了抽象类的定义以及如何用抽象方法来实现多态。

这次实验与前两次不同,采用 Windows 应用程序,既涉及到代码段也涉及到界面的设计。所以,勉强通过实验。

实验项目名称:

接口、文件和流

实验学时:

同组学生姓名:

实验地点:

A205

实验日期:月 7 日-12 月 21 日 实验成绩:

批改教师:

批改时间:

实验 4

接口、文件和流

一、实验目的

(1)掌握接口的定义及使用方法;(2)掌握流,序列化和反序列化的概念和使用方法;(3)掌握流文件的读写操作类及其使用方法;(4)掌握 OpenFileDialog,SaveFileDialog 等控件的使用。

二、实验要求

(1)编写程序要规范、正确,上机调试过程和结果要有记录;(2)做完实验后给出本实验的实验报告。

三、实验设备、环境

安装有 Visual Studio.NET 软件。

四、实验步骤

1、分析题意; 2、根据题目要求,新建项目; 3、编写并输入相关的程序代码; 5、运行与调试项目; 6、保存项目。

五、实验内容

1、定义一个 Person 类,包含姓名字段和一个方法,早上 8:30学生开始上课,教师开始讲课。用接口来实现。

using System;using System.Collections.Generic;

using System.Text;namespace Test4_1 {

class Program

{

static void Main(string[] args)

{

Student s = new Student(“张三”,“学生”);

Console.WriteLine(s.Work());

Teacher t = new Teacher(“李四”,“老师”);

Console.WriteLine(t.Work());

}

public abstract class Person

{

public string name;

public Person(string name)

{

this.name = name;

}

}

interface IPerson

{

string type { get;}

string Work();

}

public class Student :Person, IPerson

{

public string type

{

get { return string.Format(“老师”);}

}

public Student(string name, string type)

: base(name)

{

this.name=name;

}

public string Work()

{

return string.Format(“Person{0}:早上 8:30 开始上课”, name);

}

}

...

第五篇:如何面对公司里的太子党

如何面对公司里的“太子党”

11人力1班Augus

1.什么是“太子党”

所谓“太子党”是指熟人推荐的员工或老板的亲戚、朋友、同学、某种利益的联盟。熟人推荐招聘成本低、可靠性高,同时也带来拉帮结派、难于管理的问题。

老板的亲戚朋友同学等同样也造成管理困难,特别是在与他们进行沟通时,通常存在各种各样的困难和压力,导致管理失效。中国人的人情观念又比较重,人情叠人情,一旦发现问题,你又不好下重手。

2.问题分析

①中国人的人情观念太重,而这些小团体中尤以搭上人情的渠道才进入了公司,人情债还都还不清。而一旦出问题你重罚他们,你可能就卷入了一场人情战中。②中国人的等级观念也很重,如果是领导推荐的人来了,你就更不好动了,你会当心:如果你动了领导的人,你会不会被动?

③中国人做生意讲究礼尚往来,人情在生意中比重不小,关系搞不好,生意是做不成的。所以他们做的不好,你打不得骂不得,更加开不得,所以只能忍着,可忍久了,还是要出毛病的。

④熟人推荐可能并不是符合这个岗位的,熟人推荐的员工容易和推荐他的熟人形成利益集团,在沟通、管理上带来一定困难。

3.沟通对策

对推荐人员进行筛选,只选用以下几类员工:

①有真才实学、符合岗位要求、能够完成工作要求的。

②工作态度积极正面、踏实肯干、有培养潜力的③有良好的合作精神或有优秀丰富的工作经历、能够站在他人角度想问题的 ④重要人物推荐的非接受不可的和推荐人员进行入职前沟通,提出以下几点要求:

①不准提及你的关系网是谁,如果以此为傲并带来不良影响,一律开除!

②注意工作态度,尊重老员工,和同事和谐相处,不要有“优越于他人”的想法。③推荐人或领导并不会一味支撑你,如果你没有好的表现同样会对他造成不良影响。

④试用期内被证明不合格的,一样会被辞职。

4.总结

在工作时,领导公正地对待:

①表现优秀的员工,要给以及时的鼓励,但是要注意分寸,不要过头。

②如果有做的不够好的地方,部门领导要及时找他谈话,及时纠正问题点,如果表现不听从的,人力资源部出面单独谈话,并给以一定的压力。

总的来说,公司里的“太子党”既有优点也有缺点,如果不善于和他们沟通解决问题的话,则无法利用好这一资源。人力源管理部门在与领导、员工的沟通上起着不可或缺的作用,所以掌握一些沟通技巧是很有必要的。

相关内容

热门阅读

最新更新

随机推荐