会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
廖志的专栏
人无近忧,必有远虑 [天道酬勤]
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
我了解 类继承和委托的代码例
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApp
{
class
Program
{
static
void
Main(
string
[] args)
{
testClass2 mytest
=
new
testClass2();
mytest.setValue2(
100
);
mytest.PrintTxt();
mytest.outTxt();
Console.ReadLine();
}
}
}
class1: base class
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApp
{
public
delegate
void
mydelegate();
class
testClass1
{
private
int
int1, int2;
public
int
_int1
{
set
{ int1
=
value; }
get
{
return
int1; }
}
public
int
_int2
{
set
{ int2
=
value; }
get
{
return
int2; }
}
private
void
outTxt()
{
Console.WriteLine(
"
int1's value is {0} ; int2's value is {1}
"
, int1, int2);
}
public
void
PrintTxt()
{
outTxt();
}
public
void
setValue(
int
d1)
{
this
.int1
=
this
.int2
=
d1;
}
public
testClass1()
{ }
public
testClass1(
int
d)
{
this
.int1
=
d;
this
.int2
=
d;
}
public
testClass1(
int
d1,
int
d2)
{
this
.int1
=
d1;
this
.int2
=
d2;
}
}
}
class2: 继承类
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApp
{
class
testClass2:testClass1
{
mydelegate thisdelegate
=
null
;
public
testClass2()
:
base
(
12
,
13
)
{
thisdelegate
=
new
mydelegate(outTxt);
thisdelegate();
thisdelegate
=
new
mydelegate(outCustomTxt);
thisdelegate();
}
public
void
setValue2(
int
d1)
{
this
.setValue(
123
);
}
public
void
outTxt()
{
this
.PrintTxt();
}
public
void
outCustomTxt()
{
Console.WriteLine(
"
测试委托!
"
);
}
}
}
打印结果:
int1's value is 12 ; int2's value is 13
测试委托!
int1's value is 123 ; int2's value is 123
int1's value is 123 ; int2's value is 123
posted on
2005-05-12 14:35
dotnet学习者
阅读(
155
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
博客园
© 2004-2026
浙公网安备 33010602011771号
浙ICP备2021040463号-3