零基础学会C#三——在C#中实现面向对象的概念 目标
1、目标
- 编写一个队列类的C#程序。
- this关键字的使用。
- 构造函数的重载和普通函数的重载。
2、队列类的应用程序
2.1 问题
在数据结构中已经学习了队列,请用C#编写一个类实现队列的功能,并编写一个测试类测试该队列的功能。(参考.Net的类库中已经封装好了的队列类:System.Collections.Queue,可以将自己做好的类,和它进行比较)
2.2 说明
队列是限定所有的插入操作在表的一端进行,而删除操作在表的另一端进行的线性表,具有先进先出的特性。确定该队列中存储的值类型为整数类型。
2.3 分析
一个队列有入排和出排动作,可以编写两个函数分别命名为EnQueue和DeQueue;还应该有一个属性Length:判断队列中的元素个数;一个打印的函数Print:将队列中所有的值进行打印输出。
2.4 推荐步骤:
(1)新建一个名为“QueueWithCSharp”的基于控制台应用程序的项目。
(2)添加一个类,名为:Queue,添加以下代码。
2.5 代码
using System;
using System.Collections.Generic;
using System.Text;
namespace QueueWithCSharp
{
public class Node
{
public int data;
public Node prior, next;
public Node()
{
prior = null;
next = null;
data = 0;
}
}
public class Queue
{
Node head, rear;
int length;
public int Length
{
get
{
return length;
}
}
public Queue()
{
//
// TODO: 在此处添加构造函数逻辑
//
head = rear = null;
length = 0;
}
public void EnQueue(int data) // 追加
{
if (rear == null)
{
rear = new Node();
head = rear;
rear.data = data;
length++;
}
else
{
rear.next = new Node();
rear.next.data = data;
length++;
rear = rear.next;
}
}
public int DeQueue() //删除
{
if (length <= 0)
{
rear = head = null;
Console.WriteLine("队列中没有元素");
return 0;
}
int data = head.data;
head = head.next;
length--;
return data;
}
public void Print() //打印
{
string str = "";
Node current = head;
while (current != null)
{
if (current == head)
{
str += current.data.ToString();
}
else
{
str += " <- " + current.data.ToString();
}
current = current.next;
}// end while current
Console.WriteLine(str);
}
}
}
(3)在Program.cs类中调用Queue类,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace QueueWithCSharp
{
class Program
{
static void Main(string[] args)
{
//定义一个 队列类
Queue demQueue = new Queue();
//数据入排
demQueue.EnQueue(10);
demQueue.EnQueue(19);
demQueue.EnQueue(50);
demQueue.EnQueue(99);
//数据出排
demQueue.DeQueue();
//打印队列中的数据
demQueue.Print();
}
}
}
2.6 输出结果
3、this关键字的使用
3.1 问题
this关键字的使用
3.2 说明
this引用有三种典型的使用方式。第一种方式是限定被参数掩藏的实例变量;
public void Function1(int i)
{
This.i=i;
}
第二种用法是把当前对象作为参数传给另一个方法;
public void Method1()
{
OtherClass obj= new OtherClass();
obj.Method2(this);
}
第三种方法与索引器有关。
public Class this[string title]
{
get
{
foreach(…)
…
return class;
}
}
3.3 分析
定义一个汽车类,这个类有多个构造函数,因为这些构造函数之间有部分功能是重叠的,所以可以进行相互调用。在进行构造函数的互相调用时,this关键字调用参数最匹配的那个构造函数。
3.4 推荐步骤:
(1)使用Visual Studio.NET 2010新建一个基于控制台的项目“CallConstructor”。
(2)将“Program.cs”类文件重命名为“Car.cs”。
(3)将以下代码添加到“Car.cs”中。
3.5 代码
using System;
using System.Collections.Generic;
using System.Text;
namespace CallConstructor
{
public class Car
{
int petalCount = 0;
string s = "null";
Car(int petalCount)
{
this.petalCount = petalCount; //this的第一种用法
Console.WriteLine("Constructor w/int arg only,petalCount = " + petalCount);
}
Car(String s, int petals)
: this(petals) //this关键字调用参数最匹配的那个构造函数
{
this.s = s;
Console.WriteLine("String & int args");
}
Car(): this("hi",47)
{
Console.WriteLine("default Constructor");
}
static void Main(string[] args)
{
Car x = new Car();
}
}
}
3.6 输出结果
4、构造函数的重载和普通函数的重载
4.1 问题
构造函数的重载和普通函数的重载
4.2 说明
方法的重载仅仅是根据方法的参数列表来决定是否两个同名的函数是不同的,而不包括方法的返回值。以下声明两个不同的函数是非法的:
Void f() {}
int f() {}
4.3 分析
定义一个Tree类,该类对构造函数和info函数进行重载。最后在main函数中,采用不同的参数去实例Tree类对象和调用info方法。
4.4 推荐步骤:
(1)创建一个名为“Overloading”的控制台应用程序。
(2)将以下代码添加到“Program.cs”中。
using System;
using System.Collections.Generic;
using System.Text;
namespace Overloading
{
class Program
{
class Tree
{
int height;
public Tree()
{
Console.WriteLine("Planting a seedling");
height = 0;
}
public Tree(int i)
{
Console.WriteLine("Creating new Tree that is "+i+" feet tall");
height = i;
}
internal void info()
{
Console.WriteLine("Tree is "+height+" feet tall");
}
internal void info(string s)
{
Console.WriteLine(s+":Tree is " + height + " feet tall");
}
}
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Tree t = new Tree(i);
t.info();
t.info("overloaded method");
}
new Tree();
}
}
}
4.5 输出结果
5、练习
5.1 问题一
5.1.1 题目
设计项目s3-1。构建一个类Point,它提供两个公有的构造函数,一个没有参数的Point构造函数和一个有两个double参数的构造函数。另外在该类中提供一个静态方法计算两个点的直线距离,传入参数为两个Point类实例。然后设计一个测试类来对Point类进行使用。
提示:先定义两个变量来存储Point点的X,Y坐标;无参的构造函数将X,Y坐标赋为0;有参的构造函数将传入的参数分别赋给X,Y坐标。
5.2 问题二
5.2.1 题目
设计项目s3-2。编写一个程序,输入自己的生日,判断这一天是这一年的第几天。
5.3 问题三
5.3.1 题目
设计项目s3-3。编写一个程序,用来模拟银行帐户的基本操作,如帐户开户的话,则最低存款额为100、存取现金操作以及在使用任意修改余额后都可以随时查看帐户余额。请使用重载的带参数的构造函数。
提示:要实现此问题的解决方案,请创建一个名为Account类。该类包含一个名为saving的成员变量。编写一个默认的构造函数,使用值100 初始化该成员变量(如果用户开户时的钱少于100,则不能开户;也就是不能实例化该类。)。创建一个参数化构造函数,它可以传递任何大于100的整数以初始化saving变量。
5.4 问题
5.4.1 题目
设计项目s3-4。请采用面向对象的程序设计方法建立一个汽车Auto类 包括轮胎个数、汽车颜色、 车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求 汽车能够加速、 减速、 停车。 再定义一个小汽车类Car 继承Auto 并添加空调、CD等成员变量 覆盖加速 、减速的方法。