我有下面的类:
public class Third
{
public Third()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public virtual double AreaTest(double x,double y)
{
return x * y;
}
}
类里面仅仅有个虚拟方法,它有两个子类
public class Fourthly:Third
{
public Fourthly()
{ }
public override double AreaTest(double x, double y)
{
return 2 * x * y;
}
}
子类Fourthly重写了父类的AreaTest方法,同样另外一个子类
public class Fifthly:Third
{
public Fifthly()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public override double AreaTest(double x, double y)
{
return 4 * x * y;
}}
也是重写了父类的AreaTest方法。下面看看我怎么样使用这些类以及他们的方法
public double ClassTypeTest(Third ThClass,double x,double y)
{
return ThClass.AreaTest(x,y);
}
我定义了上面一个方法,其中参数就有Third类对象,再看下面的
private void Button2_Click(object sender, System.EventArgs e)
{
Fourthly FourthlyClassObj = new Fourthly();
Fifthly FiftylyClassObj = new Fifthly();
this.TextBox3.Text = this.ClassTypeTest(FourthlyClassObj,1,1).ToString();
this.TextBox4.Text = this.ClassTypeTest(FiftylyClassObj,1,1).ToString();
}
我运行TextBox3文本框的值为2,TextBox4的值为4,运行正常
说到这里,我想我如果把类Third换成一个接口,也有个AreaTest方法,只是没有实现。
对应的我修改其子类,去掉子类的AreaTest方法前的override关键字,同时修改ClassTypeTest方法,将参数中的Third对象改为对应接口,最后发现,用接口和我用类实现的效果是一样的。
通过上面的例子,是不是反应我用特殊点了类(我这里的意思是:父类只声明虚拟方法,由其子类根据不同情况来实现做不同的处理)也可以实现接口相同的功能了?或者说是,我假设将Third类改成抽象的,也可以实现接口一样的功能,那么对于设计模式来说,也可以用我所说的特殊的类来实现我想问的是,我如果把类写得特殊点,那么是不是接口或抽象类就没有存在的必要了了?
可能有人会说我在转牛角尖,不过这些确实也是我比较糊涂的地方,还请各位见谅,帮帮我这个新手!
不过大家最好是把你们的理解写出来,我不需要书中理论,也正由于我没有理解好基本理论才会有这样的问题在问!
public class Third
{
public Third()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public virtual double AreaTest(double x,double y)
{
return x * y;
}
}
类里面仅仅有个虚拟方法,它有两个子类
public class Fourthly:Third
{
public Fourthly()
{ }
public override double AreaTest(double x, double y)
{
return 2 * x * y;
}
}
子类Fourthly重写了父类的AreaTest方法,同样另外一个子类
public class Fifthly:Third
{
public Fifthly()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public override double AreaTest(double x, double y)
{
return 4 * x * y;
}}
也是重写了父类的AreaTest方法。下面看看我怎么样使用这些类以及他们的方法
public double ClassTypeTest(Third ThClass,double x,double y)
{
return ThClass.AreaTest(x,y);
}
我定义了上面一个方法,其中参数就有Third类对象,再看下面的
private void Button2_Click(object sender, System.EventArgs e)
{
Fourthly FourthlyClassObj = new Fourthly();
Fifthly FiftylyClassObj = new Fifthly();
this.TextBox3.Text = this.ClassTypeTest(FourthlyClassObj,1,1).ToString();
this.TextBox4.Text = this.ClassTypeTest(FiftylyClassObj,1,1).ToString();
}
我运行TextBox3文本框的值为2,TextBox4的值为4,运行正常
说到这里,我想我如果把类Third换成一个接口,也有个AreaTest方法,只是没有实现。
对应的我修改其子类,去掉子类的AreaTest方法前的override关键字,同时修改ClassTypeTest方法,将参数中的Third对象改为对应接口,最后发现,用接口和我用类实现的效果是一样的。
通过上面的例子,是不是反应我用特殊点了类(我这里的意思是:父类只声明虚拟方法,由其子类根据不同情况来实现做不同的处理)也可以实现接口相同的功能了?或者说是,我假设将Third类改成抽象的,也可以实现接口一样的功能,那么对于设计模式来说,也可以用我所说的特殊的类来实现我想问的是,我如果把类写得特殊点,那么是不是接口或抽象类就没有存在的必要了了?
可能有人会说我在转牛角尖,不过这些确实也是我比较糊涂的地方,还请各位见谅,帮帮我这个新手!
不过大家最好是把你们的理解写出来,我不需要书中理论,也正由于我没有理解好基本理论才会有这样的问题在问!
private void Button2_Click(object sender, System.EventArgs e)
{
Fourthly FourthlyClassObj = new Fourthly();
Fifthly FiftylyClassObj = new Fifthly();
this.TextBox3.Text = this.ClassTypeTest(FourthlyClassObj,1,1).ToString();
this.TextBox4.Text = this.ClassTypeTest(FiftylyClassObj,1,1).ToString();
}也可以写成下面形式:
private void Button2_Click(object sender, System.EventArgs e)
{
Third FourthlyClassObj = new Fourthly();
Third FiftylyClassObj = new Fifthly();
this.TextBox3.Text = this.ClassTypeTest(FourthlyClassObj,1,1).ToString();
this.TextBox4.Text = this.ClassTypeTest(FiftylyClassObj,1,1).ToString();
}
Fourthly FourthlyClassObj = new Fourthly();
Third FourthlyClassObj = new Fourthly();
上面两种声明本质上有什么区别?
在同一层的继承中,接口可以继承多个,但抽象类只能继承一个。
你将上面的Fourthly 类和Third 类多扩展几个不同的方法然后使用一下就知道区别了。
对了,我还想问的是
Fourthly FourthlyClassObj = new Fourthly();
Third FourthlyClassObj = new Fourthly();
上面两种声明本质上有什么区别?
---------------------------------
我觉得就这样实现了类的多态性了,就是根据使用基类调用子类的方法
偶也不是很清楚,说错了请多包涵。
public interface MyInterface
{
double AreaTest(double x,double y);
} public class Third:MyInterface /////////////继承接口
{
public Third()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public virtual double AreaTest(double x,double y)
{
return x * y;
}
} public class Fourthly:Third
{
public Fourthly()
{ }
public override double AreaTest(double x, double y)
{
return 2 * x * y;
}
}
测试代码:
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyInterface Myintf = new Third();
Console.WriteLine(Myintf.AreaTest(1,2));
Myintf = new Fourthly();
Console.WriteLine(Myintf.AreaTest(1,2));
Console.ReadLine();
}
}输出:
2
4
/// <summary>
/// 请求环境类
/// </summary>
public class RequestUnit
{
private string _Uri;
/// <summary>
/// 构造业务接口命令招待类
/// </summary>
/// <param name="uri">连接的URI</param>
public RequestUnit(string uri)
{
//
// TODO: 在此处添加构造函数逻辑
//
_Uri = uri;
}
/// <summary>
/// 执行命令操作
/// </summary>
/// <param name="cmd">命令对象</param>
public void Execute(IRequestCmd cmd)
{
System.Net.HttpWebRequest request =(System.Net.HttpWebRequest)System.Net.WebRequest.Create(_Uri + cmd.GetUri());
request.Method="Post";
request.ContentType="application/x-www-form-urlencoded";
System.IO.Stream stream= request.GetRequestStream();
byte[] bytes = System.Text.Encoding.Default.GetBytes(cmd.GetPostStream());
stream.Write(bytes,0,bytes.Length);
stream.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream rstream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(rstream,System.Text.Encoding.Default);
cmd.Value = reader.ReadToEnd();
reader.Close();
} }
/// <summary>
///请求命令描述接口
/// </summary>
public interface IRequestCmd
{
/// <summary>
/// 获取命令的URI
/// </summary>
/// <returns>string</returns>
string GetUri();
string GetPostStream();
/// <summary>
/// 获取操作返回的值
/// </summary>
string Value
{
get;
set;
}
}
以上代码按楼主的想法是可以把IRequestCmd接口用一个的类代理不是更简单.实际上不是这样,因为这样不方便扩展,你很难用一个类去完成所有东西,看下基于IRequestCmd的简单实现就明了
public abstract class SpaceCmd:IRequestCmd
{
#region IRequestCmd 成员
/// <summary>
/// 获取操作的URI指命
/// </summary>
/// <returns></returns>
public abstract string GetUri();
public abstract string GetPostStream();
private string mValue;
/// <summary>
/// 获取操作返回值
/// </summary>
public string Value
{
get
{
return mValue;
}
set
{
mValue = value;
}
} #endregion
/// <summary>
/// 系统登陆的用户
/// </summary>
public string user="";
/// <summary>
/// 用户登陆密码
/// </summary>
public string userpwd=""; }public class Getxxx:SpaceCmd
{
#region IRequestCmd 成员 /// <summary>
/// 获取操作的URI指命
/// </summary>
/// <returns></returns>
public override string GetUri()
{
// TODO: 添加 GetDiskServer.GetUri 实现
return "getdiskserver.asp";
}
public override string GetPostStream()
{
return string.Format("user={0}&userpwd={1}",user,userpwd);
}
public System.Collections.IList Servers
{
get
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
Byte[] bytes = System.Text.Encoding.Default.GetBytes(Value);
stream.Write(bytes,0,bytes.Length);
stream.Position = 0;
System.IO.StreamReader reader = new System.IO.StreamReader(stream,System.Text.Encoding.Default);
string text = reader.ReadToEnd().Replace("<br>","|");
reader.Close();
string[] line = text.Split(new char[]{'|'});
foreach(string item in line)
{
if(item !="")
{
string[] infos = item.Split(new char[]{','});
Server server = new Server();
server.State = Server.States[int.Parse(infos[0])-1];
server.ServerNumber = infos[1];
server.ServerType = Server.Types[int.Parse(infos[2])];
server.IPAddress = infos[3];
server.Error = infos[4];
list.Add(server);
}
}
return list;
}
}
#endregion
public class Server
{
public static string[] States = new string[]{"调用成功","调用失败"};
private string mState;
/// <summary>
/// 状态值1调用成功;2调用失败
/// </summary>
public string State
{
get
{
return mState;
}
set
{
mState = value;
}
}
private string mServerNumber;
/// <summary>
///服务器编号
/// </summary>
public string ServerNumber
{
get
{
return mServerNumber;
}
set
{
mServerNumber = value;
}
}
private string mServerType;
public static string[] Types = new string[]{"其它特殊产品","站点空间","邮箱空间","数据库空间","邮件服务器"};
/// <summary>
/// 服务器类型
/// </summary>
public string ServerType
{
get
{
return mServerType;
}
set
{
mServerType = value;
}
}
private string mIPAddress;
/// <summary>
/// 服务器IP地址
/// </summary>
public string IPAddress
{
get
{
return mIPAddress;
}
set
{
mIPAddress = value;
}
}
private string mError;
/// <summary>
/// 调用错误描述
/// </summary>
public string Error
{
get
{
return mError;
}
set
{
mError = value;
}
}
}
}
Third FourthlyClassObj = new Fourthly();这个是多态.其实当你看过设计模式或者深入了解了oo思想以后,你就会觉得接口是多么重要了.和你一样,以前总觉得接口是多此一举,现在研究过设计模式就不一样了.
去看看《java与模式》和《C#设计模式》《敏捷软件开发》,你的问题就不再是问题了。
看了这些书,oo思想也会有提高的。