黑马程序员——C#结合winForm学习笔记

来源:互联网 发布:ppt模版打开软件 编辑:程序博客网 时间:2024/04/29 21:21

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------   

在学习中,最重要的还是多练,多写,例子结合理论事半功倍!

方法的重载

编译器可以自动选择调用哪个方法
重载“不是面向对象特征”,“不是多态特征”,而是一种“方便” 
的特征
代码

{
Test theTest=new Test();
//编译器自动判断使用哪个函数
int intMax=theTest.max(4,5);
double doubleMax=theTest.max 
(4.444,5.555);
string stringMax=theTest.max("无天听 
过吗?","无天是我");
//显示出来
MessageBox.Show(intMax.ToString());
MessageBox.Show(doubleMax.ToString 
());
MessageBox.Show(stringMax.ToString 
());
}


public class Test {
//成员方法
public int max(int x, int y) {
if (x >= y)
return x;
else
return y;
}
public double max(double x, double y) {
if (x >= y)
return x;
else
return y;
}
public string max(string str1, string str2) 
{
if (str1.Length >= str2.Length) { 
return str1; }
else { return str2; }
}
}

 方法的参数 (ref,out,params)

参数:类的成员方法中的参数

当我们定义方法时,使用一些关键字修饰形参,将有不同的效果。
定义方法时:形参
调用方法时:实参

参数不确定 用params 虽然不合理 但是支持
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

private void Form1_Load(object sender, 
EventArgs e) {
/*
int x = 9, y = 10;
MessageBox.Show("调用前:" + x + "和 
" + y);
Function1(x, y);
MessageBox.Show("调用后:" + x + "和 
" + y);
//*/
/*
string s1 = "第一个字符串";
string s2 = "第二个字符串";
MessageBox.Show("调用前:“" + s1 + 
"”和“" + s2 + "“");
Funcion2(ref s1, ref s2);
MessageBox.Show("调用后:“" + s1 + 
"”和“" + s2 + "“");
//*/
/*
int ans;
Add(90, 90, out ans);
MessageBox.Show("ans:"+ans);
//*/

double average;
average = CalculateAverage(4.0, 3.2, 
5.7);
MessageBox.Show("4.0,3.2,5.7的平均数 
是:" + average);
//*/
double[] data = { 4.0, 3.2, 5.7, 33 
};
average = CalculateAverage(data);
MessageBox.Show("4.0,3.2,5.7,33的平 
均数是:" + average);
}
//值参数
public void Function1(int x, int y) {
x = 10000;
y = 88888;
}
public void Funcion2(ref string s1, ref 
string s2) {
s1 = "踩踩我是谁";
s2 = "就不告诉你";
}
//输出参数
public void Add(int x, int y, out int ans) {
ans = x + y;
}
//多个参数的情况
public double CalculateAverage(params 
double[] values) {
double sum = 0;
for (int i = 0; i < values.Length; 
i++)
sum += values[i];
return (sum / values.Length);
}
}

 继承和多态

:
virtual
new
override

public class Animal
{
public string word="";
public virtual void Introduce()
{word="I am an animal.";}
}
public class Dog:Animal
{
public override void Introduce()
{word="I am an Dog.";}
}
//没有成员方法 但是word可以被赋值
//将其值覆盖
public class Cat:Animal
{
public override void Introduce()
{word="I am an Cat.";}
}
virtual:可以被继承
override: 已经被继承
这就是多态 子类的表现形式是多种多样的

派生类从它的直接基类中继承成员:方法.域、属性、事件、索引指 
示器。除了构造函数和析构函数,派生类隐式地继承了直接基类的所 
有成员

 异常和异常处理

异常:
1.除数为0
2.访问的文件不存在
3.溢出
4.错误的强制类型转化
5.网络原因
6.等等

try-catch-finally

DivideByZeroException

throw new ArgumentNullException();

/*
try {
int nTheZero = 0;
int nResult = 10 / nTheZero;
}
catch (Exception Ex) {
MessageBox.Show("除数不能为 
零");
}
finally {
MessageBox.Show("finally");
}
MessageBox.Show("执行try语句后面的语 
句");
//*/
/*
try {
int nTheZero = 0;
int nResult = 10 / nTheZero;
}
catch (DivideByZeroException divEx) 
{
MessageBox.Show 
(divEx.ToString());
}
catch (Exception Ex) {
MessageBox.Show(Ex.ToString 
());
}
finally {
MessageBox.Show("finally");
}
MessageBox.Show("执行try语句后面的语 
句");
//*/
//前面匹配了 后面就没有了 异常的子 
类型应该放在前面 程序更精准
/*
try { //会把所有的异常捕获 

int nTheZero = 0;
int nResult = 10 / nTheZero;
}
catch {
MessageBox.Show("Catch!!");
}
//*/
try {
string s = null;
if (s == null) {
throw new 
ArgumentNullException();
} //自己创造一个异常 抛出 
异常
}
catch { MessageBox.Show("接受抛出的 
异常"); }
MessageBox.Show("执行try语句后面的语 
句");

命名空间

命名空间:一种组织类的机制,例如:我们可以把所有的文件操作的 
类放在一个命名空间。

如果两个命名空间同时命名 那么无法区分 因此需要写全称!

空间是可以嵌套的

微软命名空间的类库

using System.Windows.Forms;
//using Ceng1;
//using Ceng2;
//using Ceng3;
//using Ceng3.Ceng33;

namespace _234 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

private void Form1_Load(object sender, 
EventArgs e) {
Person thePerson = new Person();
//Ceng1.Person thePerson1 = new 
Ceng1.Person();
//Ceng2.Person thePerson2 = new 
Ceng2.Person();
//Ceng3.Ceng33.Person thePerson3 = 
new Ceng3.Ceng33.Person();
}
}
}
namespace Ceng1 {
public class Person { }
}
namespace Ceng2 {
public class Person { }
}
namespace Ceng3 {
public class Person { }
//子空间
namespace Ceng33 {
public class Person { }
}
}

 接口

接口看似是:声明了一个类,而且类的成员都具有“行为”
public interface IShape
{
void Draw();
}
没有成员变量,不能使用访问限制关键字

接口

接口interface从两个基接口IBase1和IBase2继承:
interface IChild:IBase1,IBase2
{
void Method1();
void Method2();
}

1.一个接口定义一个协定。使用者和实现者之间的协议。
2.接口在更多的时候,表现出对外提供的服务。所以没有成员变量
3.形式上可以说,接口是没有成员变量的抽象类,但是语义上讲不通
4.但在实际中,解决多重继承的问题
namespace _234 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
IFace PengGe = new Person();
PengGe.ShowFace();
}
}
interface IFace {
void ShowFace();
}
//实现接口的类
class Person : IFace {
public void ShowFace() { MessageBox.Show("吓死人, 哈哈哈!!!"); }
}
}

 泛型

泛型类:带有“参数”的类。这里的参数指的是类型。

注意事项:
1.这里的T、S不知道是什么类型的,有些语句不能用t>0,t=null
2.你就当T、S是object类型的。object是所有类型的最高父类,包括基本数据类型。
3.好处:高效,简洁。避免使用object的一些基本开销。
4.很多东西都可以是泛型的,方法接口委托等等

private void Form1_Load(object sender, EventArgs e) {
//使用string,int来实例化Test<T,S>类
Test<string, int> t = new Test<string, int>("PengGe", 26);
//调用泛型类中的方法
t.SetValue();
}

}
//定义一个泛型类,该类有两个类型参数,分别是T,S
public class Test<T, S> {
//泛型类的类型参数可用于类成员
private T name;
private S age;

public Test(T Name, S Age) {
this.name = Name;
this.age = Age;
}
public void SetValue() {
MessageBox.Show("姓名是:" + name.ToString());
MessageBox.Show("年龄是:" + age.ToString());
}

   ---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------详细请查看:http://net.itheima.com/

原创粉丝点击