[英]How To Make Abstract Class And Inherit In Other Classes
I have two clsses with similar code in both but having different variables 我有两个具有相似代码但却具有不同变量的类
ISource.cs ISource.cs
public interface ISource
{
string AvailConfigPath { get; }
string AvailVersion { get; }
IDictionary<string, string> AvailFiles { get; }
}
public class Source : ISource
{
public string AvailConfigPath
{
get
{
return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config";
}
}
private XDocument document = XDocument.Load(@"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config");
public string AvailVersion
{
get
{
return document.Root
.Element("InfoConfigFile")
.Attribute("version").Value.ToString();
}
}
public IDictionary<string, string> AvailFiles
{
get
{
return document.Root
.Element("files")
.Elements("file")
.ToDictionary(x => x.Attribute("name").Value,
x => x.Attribute("version").Value);
}
}
}
ITarget.cs ITarget.cs
public interface ITarget
{
string LocalConfigPath { get; }
string LocalVersion { get; }
IDictionary<string, string> LocalFiles { get; }
}
internal class Target : ITarget
{
public string LocalConfigPath
{
get
{
return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
}
}
private XDocument document = XDocument.Load(@"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config");
public string LocalVersion
{
get
{
return document.Root
.Element("InfoConfigFile")
.Attribute("version").Value;
}
}
public IDictionary<string, string> LocalFiles
{
get
{
return document.Root
.Element("files")
.Elements("file")
.ToDictionary(x => x.Attribute("name").Value,
x => x.Attribute("version").Value);
}
}
}
Now i want to make an abstract class and put my common code in that class file and then inherite that code in these both class files 现在,我想创建一个抽象类,并将我的通用代码放入该类文件中,然后在这两个类文件中继承该代码
Looks like you should be able to combine them this way: 看起来您应该可以通过以下方式将它们组合:
public interface ITarget
{
string LocalConfigPath { get; }
string LocalVersion { get; }
IDictionary<string, string> LocalFiles { get; }
}
public interface ISource
{
string AvailConfigPath { get; }
string AvailVersion { get; }
IDictionary<string, string> AvailFiles { get; }
}
internal abstract class BaseClass
{
public virtual string ConfigPath { get; }
private XDocument document = XDocument.Load(ConfigPath);
public string Version
{
get
{
return document.Root
.Element("InfoConfigFile")
.Attribute("version").Value;
}
}
public IDictionary<string, string> Files
{
get
{
return document.Root
.Element("files")
.Elements("file")
.ToDictionary(x => x.Attribute("name").Value,
x => x.Attribute("version").Value);
}
}
}
internal class Target : BaseClass, ITarget
{
public override string LocalConfigPath
{
get
{
return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
}
}
public string LocalVersion
{
get { return Version; }
}
public IDictionary<string, string> LocalFiles
{
get { return Files; }
}
}
public class Source : BaseClass, ISource
{
public override string AvailConfigPath
{
get
{ return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config"; }
}
public string AvailVersion
{
get { return Version; }
}
public IDictionary<string, string> AvailFiles
{
get { return Files; }
}
}
Note: Do not use the class name BaseClass
- make it something more domain-appropriate. 注意:不要使用类名BaseClass
使它更适合于域。 I was just using it to illustrate that is was the "base class" 我只是用它来说明那是“基类”
Also note that if Source
is public then BaseClass
has to be public as well. 还要注意,如果Source
是公共的,则BaseClass
也必须是公共的。 If you really want to bake BaseClass internal then you could use encapsulation instead of inheritance - which is still re-using code but the plumbing is different. 如果您真的想在内部烘焙BaseClass,则可以使用封装而不是继承-仍在重用代码,但方法有所不同。
Thanks To D Stanley__ your answer helped me a lot to solve this problem 感谢D Stanley__,您的回答对我解决了很多问题
AbstractClass.cs AbstractClass.cs
public abstract class BaseClass
{
private string _ConfigPath;
public string ConfigPath
{
get { return _ConfigPath; }
set { _ConfigPath = value; }
}
private XDocument _Document = null;
private XDocument document
{
get
{
if (_Document == null)
_Document = XDocument.Load(ConfigPath);
return _Document;
}
}
public string Version
{
get
{
return document.Root
.Element("InfoConfigFile")
.Attribute("version").Value;
}
}
public IDictionary<string, string> Files
{
get
{
return document.Root
.Element("files")
.Elements("file")
.ToDictionary(x => x.Attribute("name").Value,
x => x.Attribute("version").Value);
}
}
}
ISource.cs ISource.cs
public interface ISource
{
string ConfigPath { get; }
string AvailVersion { get; }
IDictionary<string, string> AvailFiles { get; }
}
public class Source : BaseClass, ISource
{
public Source()
{
ConfigPath = @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config";
}
public string AvailVersion
{
get
{
return Version;
}
}
public IDictionary<string, string> AvailFiles
{
get
{
return Files;
}
}
}
ITarget.cs ITarget.cs
public interface ITarget
{
string ConfigPath { get; }
string LocalVersion { get; }
IDictionary<string, string> LocalFiles { get; }
}
internal class Target : BaseClass, ITarget
{
public Target()
{
ConfigPath = @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
}
public string LocalVersion
{
get
{
return Version;
}
}
public IDictionary<string, string> LocalFiles
{
get
{
return Files;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.