小例子大智慧1---C#继承,构造函数
来源:互联网 发布:win10如何开启23端口 编辑:程序博客网 时间:2024/05/01 02:32
1、建立两个实体,学生Student和人Person
这两个角色都有自己共同的属性,那就是姓名,性别,年龄;而学生有学生成绩
所以:
public class Person
{
public string Name;
public string Sex;
public int Age;
//构造函数,初始化
public Person()
{
this.Name = "张三";
this.Sex = "男";
this.Age = 30;
}
public string SayHi()
{
return "Hell!How are you?";
}
}
而学生直接继承人的实体:
public class Student : Person
{
public int CJ;
//构造函数,初始化
public Student()
{
this.CJ = 90;
}
public new string SayHi()//使用new有意隐藏基类方法
{
return "你好啊!....";
}
}
然后我在aspx页面放了两个lable
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
.cs文件中的代码
protected void Page_Load(object sender, EventArgs e)
{
Student s = new Student();
Label1.Text = "姓名:" + s.Name + ",成绩:" + s.CJ;
Label2.Text = s.SayHi();
}
页面显示:
姓名:张三,成绩:90
你好啊!....
有意隐藏了基类中的方法,如果去掉Student中new的修饰的方法,会提示
- 小例子大智慧1---C#继承,构造函数
- 小例子大智慧2--C#中的多态Polymorphism
- C#几个例子[静态构造函数,继承,虚方法]
- 小例子大智慧4--委托Delegate
- 小例子大智慧5--事件Event
- C#构造函数继承
- C#构造函数继承
- C# 构造函数 例子
- 小故事大智慧1
- 小例子分析C#继承机制
- 小例子分析C#继承机制
- 小动物大智慧
- 小故事大智慧
- 小问题,大智慧
- 小故事大智慧
- 小幽默大智慧
- 小故事大智慧
- 小常识,大智慧
- c++拷贝构造函数
- viewPager控制滑动速度和时间
- [Django实战] 第4篇 - 用户认证(用户登录与注销)
- linux常见问题之vim 查找替换命令
- epoll
- 小例子大智慧1---C#继承,构造函数
- UltraEdit 中用正则表达式替换文本中的空行
- CSS中z-index的用法
- windows环境下配置Visual Studio的cl编译器
- linux常见问题之系统移植的概念区分
- windows下进程间通信方式介绍
- 完全退出android程序的方法
- IOS 用dispatch_once创建单利
- 网页设计与网站建设(CS3中文版)从新手到高手