作业帮 > ASP.NET > 教育资讯

asp.net教程:ASP.NET基础教程(一):面向对象(OO)思想

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/10 20:17:34 ASP.NET
asp.net教程:ASP.NET基础教程(一):面向对象(OO)思想
asp.net教程:ASP.NET基础教程(一):面向对象(OO)思想ASP.NET
【51Test.NET-asp教程:ASP.NET基础教程(一):面向对象(OO)思想】:

刚开始接触面向对象编程的朋友很多人都搞不懂什么是面向对象,以及类和对象的区别,我们本节先来看一下什么是面向对象,在下节我们将学习类和对象的区别。

其实学过一点C++的人都知道,面向对象就是程序并发的一种机制,他有几个重要的特征,分别是:封装,继承,多态 要实现以面向对象编写程序,那你要记得把复杂的项目抽象分成几个对象的模型,然后就可以开始写类的结构,声明变量以及实现成员类型(包括接口和结构体),最后通过类的实例处理完成一定的问题的解决。相对于面向过程,面向对象注重人的思维,可以实现程序快速开发,当你把程序分成多个模块时,更加体现了分工负责的思想,有更低的耦合度,类是面向对象的重要概念,而对象的作用也不能忽视,打个比方,动物是个大说法,可以有鸟类,而鸟类又有分有燕子,布谷鸟,乌鸦等等,燕子,布谷鸟,乌鸦是从鸟类中派生出来的,他们具有鸟类特有飞的特征,但另外他们也具有自己特有的特点,如图:


  
  
  类和对象图示


下面代码显示这个思想
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oop
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(
"---------------请输入猫头鹰对象的体重和身高(以回车隔开)------------------");
string w = Console.ReadLine();
string l = Console.ReadLine();
Owl a_Owl
= new Owl(w,l);
Console.WriteLine(
"猫头鹰对象创建成功!!");
Console.WriteLine(
"猫头鹰的体重是"+a_Owl.weight +"kg");
Console.WriteLine(
"猫头鹰的身高是"+a_Owl.length +"cm");
Console.WriteLine(
"猫头鹰的特征是"+ Owl.message);
Console.WriteLine(
"猫头鹰的生存在" + Owl.habitat);
Console.WriteLine(
"猫头鹰跟猫很像吗?" + Owl.cat());
Console.WriteLine(
"---------------程序结束!!------------------");
Console.ReadLine();

}

}
class Mammal //动物全称
{
protected static bool Feeding = true;//有哺乳的特点
}
class Bird:Mammal//鸟类
{
protected static bool catlike = true;//类似猫
protected static bool Fly=true;//可以飞行
}
class Owl : Bird
{
//有自己的特征
internal static string message="猫头鹰吃老鼠";
internal static string habitat = "丛林高树上";
internal string weight; //体重
internal string length; //身高
internal Owl(string w, string l)//构造函数直接赋值
{
this.weight = w;
this.length = l;
}
internal static bool cat()//通过静态方法获取继承的信息
{
return Owl.catlike;
}

}
}
程序创建了一个对象,并访问部分数据和方法,编译后结果显示如下: 

ASP.NET