C#语法与面向对象基础.docx
《C#语法与面向对象基础.docx》由会员分享,可在线阅读,更多相关《C#语法与面向对象基础.docx(23页珍藏版)》请在冰豆网上搜索。
C#语法与面向对象基础
C#语法和面向对象基础学习笔记
2012/12/29—2013/01/5
C#语法与面向对象基础
第一节C#基础
1.控制台程序简介:
类似于C语言的显示界面,一般需要下列三个常见的命令:
(1)控制台界面打印:
Console.WriteLine("请输入您的姓名")
(2)控制台后台读取:
stringname=Console.ReadLine(),返回的永远都是字符串。
(3)控制台界面暂停:
Console.ReadKey()
2.特殊符号:
(1)占位符:
占位符类似于C语言中的占位符,从0开始,依次类推。
staticvoidMain(string[]args)
{
Console.WriteLine("请输入您的姓名");
stringboy=Console.ReadLine();
Console.WriteLine("请输入您女朋友的名字");
stringgirl=Console.ReadLine();
Console.WriteLine("{0}+{1}={2}",boy,girl,boy+"爱"+girl);
Console.ReadKey();
}
(2)转义符:
转义符“\”,因此若想在控制台输出“\”必须经过转义之后才能正确输出。
staticvoidMain(string[]args)
{
inta=45;
intb=5;
Console.WriteLine("{0}\\{1}={2}",a,b,a/b);
Console.ReadKey();
}
若想将转义符释放作用,只需要在\前加上@符号。
如:
strings=@"123\4";
3.类型转换:
类型转换一般选用convert表达式。
(1)Convert.ToInt32():
转换成正整数
(2)Convert.ToString():
转换成字符串
staticvoidMain(string[]args)
{
Console.WriteLine("请输入第一个数字");
stringa=Console.ReadLine();
intnum1=Convert.ToInt32(a);
Console.WriteLine("请输入第二个数字");
stringb=Console.ReadLine();
intnum2=Convert.ToInt32(b);
Console.WriteLine("{0}+{1}={2}",num1,num2,num1+num2);
Console.ReadKey();
}
第二节面向对象-类
1.面向对象的三个特征:
(1)封装
(2)继承
(3)多态
2.类的简介:
(1)类的定义:
类是抽象的,对象是具体的。
类可以从其上一级或者同级的类中继承本身不具有的特性。
类中包含字段、方法和属性。
classPerson
{
publicstringName;/*Name即是Person类的字段*/
publicintAge;
publicvoidSay()/*Say()即是Person类的方法*/
{
Console.WriteLine("{0}您好",Name);/*使用了占位符,打印*/
}
}
(2)类的使用:
类的使用以new字段开始,在使用时要注意类中的字段或者方法是公共部分还是私有部分。
classProgram
{
staticvoidMain(string[]args)
{
Personzhenghao=newPerson();/*新建一个zhenghao的类*/
zhenghao.Name="郑昊";/*Person类中的Name字段是公共的,因此可以使用*/
zhenghao.Age=26;
Console.WriteLine(zhenghao.Name);
Console.WriteLine(zhenghao.Age);
zhenghao.Say();
Console.ReadKey();
}
}
(3)类中成员的访问级别:
A、public:
任何地方都可以访问。
B、private:
只有所在的类自己能访问,字段一般都定义成私有private。
C、internal:
内部的文件可以使用。
(4)类中的属性:
类中的字段一般设立为私有(private),在需要对私有的字段进行修改时,则需要通过类中的属性来实现。
属性中通过set和get来进行操作,其中set是赋值,get为取值。
这样的好处主要是控制非法值。
classProgram
{
staticvoidMain(string[]args)
{
Personzhenghao=newPerson();
zhenghao.Name="郑昊";
Console.WriteLine(zhenghao.Name);
Console.ReadKey();
}
}
classPerson
{
privateintage;
privatestringname;
publicstringName
{
set
{
if(value=="zhenghao")
{
Console.WriteLine("非法值!
");
return;
}
this.name=value;
}
get
{
returnthis.name;
}
}
}
判断
主调函数staticvoidMain(string[]args)在新建一个Person类后使用其中的Name属性,Name属性中则采用所在类的私有字段name。
name字段在主调函数中无法使用,但是主调函数可以使用Name属性。
3.构造函数:
构造函数用于创建对象,并对对象进行初始化。
类似于类,是类中的一种没有返回值的函数,函数名必须与类名一致。
如果在类中不写构造函数,则.framework默认为其已经有一个不需要参数的构造函数。
这就是我们在定义完类后,在新建对象是使用new类名()。
classProgram
{
staticvoidMain(string[]args)
{
RobotR1=newRobot();/*在新建Robot类时系统已经默认Robot中含有构造函数,该函数不需要任何参数*/
}
}
classRobot/*在Robot类中并没有构造函数*/
{
……………………………
}
classProgram
{
staticvoidMain(string[]args)
{
RobotR1=newRobot();
RobotR2=newRobot("郑昊");
RobotR3=newRobot("郑昊",26);
Console.WriteLine("{0}{1}",R1.Name,R1.Age);
Console.WriteLine("{0}{1}",R2.Name,R2.Age);
Console.WriteLine("{0}{1}",R3.Name,R3.Age);
Console.ReadKey();
}
}
classRobot
{
publicstringName{get;set;}
publicintAge{set;get;}
publicRobot()
{
this.Name="未命名";
this.Age=0;
}
publicRobot(stringname)
{
this.Name=name;
}
publicRobot(stringname,intage)
{
this.Name=name;
this.Age=age;
}
}
4.值引用和对象引用:
值类型只是简单的赋值,赋值的时候是类似于传递“拷贝”;对象的引用则类似于C语言中的指针,他们传递的是引用。
下面的R1和R2两个对象其实指的都是age=10。
staticvoidMain(string[]args)
{
--------------------------------------数值引用-----------------------------------------
inta=10;
intb=a;
a++;/*a自增1后,b不会改变*/
Console.WriteLine("a={0},b={1}",a,b);
-------------------------------------对象引用------------------------------------------
RobotR1=newRobot(10);
RobotR2=R1;
R1.Age++;/*R1.Age自增1后,R2.Age也会相应的自增1*/
Console.WriteLine(R2.Age);
Console.ReadKey();
}
5.继承:
子类可以继承其他类作为其父类,子类后面使用冒号连接父类。
子类能集成父类所有的字段和属性以及方法。
.NET平台中所有的类都继承于object类
classProgram
{
staticvoidMain(string[]args)
{
ChineseC1=newChinese();
C1.Name="郑昊";
C1.Age=26;
C1.Height=20.4M;
C1.Destination="河南省新蔡县";
}
}
classPerson
{
privatestringname;
privateintage;
privatedecimalheight;
publicstringName{get;set;}
publicintAge{get;set;}
publicdecimalHeight{get;set;}
}
classChinese:
Person
{
privatestringdestination;
publicstringDestination{get;set;}
}
6.异常:
使用try…catch语句来测试异常。
若想让系统显示自己定义的异常提示,只需要使用throw语句即可。
classProgram
{
staticvoidMain(string[]args)
{
try
{
PersonP1=newPerson();
intC=P1.Sum(1,8);
Console.WriteLine(C);
Console.ReadKey();
}
catch(ExceptionErro)
{
Console.WriteLine("数据错误"+Erro.Message);
}
Console.ReadKey();
}
}
classPerson
{
publicintSum(inta,intb)
{
intsum;
sum=a+b;
if(sum>10)
{
thrownewException("数字太小");
}
returnsum;
}
}
第三节变量和命名空间
1.常量:
常量以const开头,表示不会发生改变的量。
2.变量:
(1)全局变量:
使用关键字static,该变量无需新建对象,直接使用。
也可以在不同的命名空间中使用。
在static成员中只能使用static成员(包括字段、属性和方法),不能使用非static成员。
类也可以使用static修饰,该种类为静态类,他们不能被实例化(new方法),直接使用即可。
(2)局部变量:
public和private修饰。
3.命名空间:
在.NET中若要使用不在同一命名空间下的类则需要引用该命名空间,使用using关键词。
namespace第节__命名空间.Person_class
{
classPerson
{
privatestringname;
privateintage;
publicstringName{get;set;}
publicintAge{get;set;}
publicvoidSay()
{
Console.WriteLine("欢迎您"+Name);
}
}
}
---------------------------------------------------------------------------------------
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using第八节__命名空间.Person_class;
namespace第八节__命名空间
{
classProgram
{
staticvoidMain(string[]args)
{
PersonP1=newPerson();
P1.Name="郑昊";
P1.Say();
Console.ReadKey();
}
}
}
4.索引器:
在.NET中可以使用索引来进行相关的操作。
classProgram
{
staticvoidMain(string[]args)
{
int[]num={1,2,3,4};
inti=num[2];
Console.WriteLine(i);
Console.ReadKey();
PersonP1=newPerson();
P1[1]="郑昊";
Console.WriteLine(P1[1]+P1[2]);
Console.ReadKey();
}
}
classPerson
{
privatestringFirstName="杨静";
privatestringSecondName="刘亦菲";
publicstringthis[intindex]
{
set
{
if(index==1)
{
FirstName=value;
}
elseif(index==2)
{
SecondName=value;
}
else
{
thrownewException("传入的数据有误");
}
}
get
{
if(index==1)
{
returnFirstName;
}
elseif(index==2)
{
returnSecondName;
}
else
{
thrownewException("传入的数据有误");
}
}
}
}
第四节类中属性和函数的比较
类中的属性和函数有所类似,由于属性中也有set和get,故也需要指定属性的类型。
函数指定的类型即为函数参数类型。
例如publicintAge{get;set;}中属性Age后面直接为{},括号中则是取值和赋值的get和set,在使用时直接P1.Age=26。
例如publicvoidSay(stringNAME)中函数Say()后面也为{},括号中即是对参数NAME的操作过程,在使用时需要给Say()提供正确类型的参数。
P1.Say("杨静")。
classProgram
{
staticvoidMain(string[]args)
{
PersonP1=newPerson();
P1.Name="郑昊";
P1.Age=26;
P1.SayHello();
P1.Say("杨静");
Console.ReadKey();
}
}
classPerson
{
privatestringname;
privateintage;
publicstringName{get;set;}
publicintAge{get;set;}
publicvoidSayHello()
{
Console.WriteLine("您好,{0}",Name);
}
publicvoidSay(stringNAME)
{
Console.WriteLine("您好,{0}",NAME);
}
}
第五节windowform开发实例
1.加法测试器:
简介:
简单的求取两个数的和,当其中一个数的格式错误时,程序自动跳出。
privatevoidbutton1_Click(objectsender,EventArgse)
{
strings1=textBox1.Text;
strings2=textBox2.Text;
inti1,i2,sum;
if(!
int.TryParse(s1,outi1))
{
MessageBox.Show("第一个数格式有错误");
textBox1.Text="";
textBox2.Text="";
return;
}
if(!
int.TryParse(s2,outi2))
{
MessageBox.Show("第二个数格式有错误");
textBox1.Text="";
textBox2.Text="";
return;
}
i1=Convert.ToInt32(s1);
i2=Convert.ToInt32(s2);
sum=i1+i2;
textBox1.Text="";
textBox2.Text="";
this.label4.Text=sum.ToString();
}
2.累加测试器:
简介:
分别输入两个大小不同的数字,然后从小数开始累加到大数。
privatevoidbutton1_Click(objectsender,EventArgse)
{
strings1=textBox1.Text;
strings2=textBox2.Text;
inti1,i2,i,sum=0;
if(!
int.TryParse(s1,outi1))
{
MessageBox.Show("第一个数格式有误");
textBox1.Text="";
textBox2.Text="";
return;
}
if(!
int.TryParse(s2,outi2))
{
MessageBox.Show("第二个数格式有误");
textBox1.Text="";
textBox2.Text="";
return;
}
i1=Convert.ToInt32(s1);
i2=Convert.ToInt32(s2);
if(i1>i2)
{
MessageBox.Show("第一个数不能比第二个数大");
textBox1.Text="";
textBox2.Text="";
return;
}
for(i=i1;i<=i2;i++)
{
sum=sum+i;
}
textBox1.Text="";
textBox2.Text="";
label4.Text=sum.ToString();
}
3.累加测试器:
简介:
输入邮箱地址后,自动分离用户名和主机域名。
privatevoidbutton1_Click(objectsender,EventArgse)
{
stringEmail=textBox1.Text;
string[]str=Email.Split('@');
if(str.Length!
=2)
{
MessageBox.Show("邮箱地址有误");
textBox1.Text="";
return;
}
label3.Text=str[0];
label4.Text=str[1];
}
4.登陆器模拟:
简介:
新增错误次数提示,当错误次数达到3次以上时,程序自动退出。
publicpartialclassForm1:
Form
{
intErrotimes=0;
publicForm1()
{
InitializeComponent();
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
txt_password.Text="";
txt_username.Text="";
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
stringusername=txt_username.Text.Trim();
stringpassword=txt_password.Text.Trim();
if(username=="郑昊"&&password=="zhenghao1987")
{
MessageBox.Show("登录成功");
}
else
{
Errotimes++;
if(Errotimes>=4)
{
MessageBox.Show("错误次数已达上限");
this.Close();
}
MessageBox.Show("用户名或密码错误");
txt_password.Text="";
txt_username.Text="";
}
}
}
基于C#的窗体播放器开发