C# 将接口或类属性继承到派生类中
我想将接口或类属性作为属性继承到派生类中这是我的代码:C# 将接口或类属性继承到派生类中,c#,inheritance,C#,Inheritance,我想将接口或类属性作为属性继承到派生类中这是我的代码: public class Login : Customer { ?? } interface Customer { string Name { get; set; } string pdw { get; set; } } 但我无法做到这一点,请建议我实现我的要求的最佳方法。看看这个例子,我认为你走错了路 继承不同于实现接口 接口更像合同,在合同中,实现接口的类也必须遵守:- public interface I
public class Login : Customer
{
??
}
interface Customer
{
string Name { get; set; }
string pdw { get; set; }
}
但我无法做到这一点,请建议我实现我的要求的最佳方法。看看这个例子,我认为你走错了路 继承不同于实现接口 接口更像合同,在合同中,实现接口的类也必须遵守:-
public interface ISendMessage
{
void Send();
}
public class SmsSender : ISendMessage
{
public void Send()
{
// Code to send SMS
}
}
public class EmailSender : ISendMessage
{
public void Send()
{
// Code to send Email
}
}
继承,通常用于有一个类作为基的情况,其他类似的类可以继承它的基属性
一个示例基类是车辆:-
public class Vehicle
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public int ManufactureYear { get; set; }
public string RegNumber { get; set; }
public decimal Value { get; set; }
}
车辆类别的所有属性都适用于卡车、摩托车、汽车或公共汽车
但是摩托车和汽车有着不同的特性,不能像这样描述:-
public class MotorBike : Vehicle
{
}
public class Car : Vehicle
{
public int NumberOfDoors { get; set; }
public int NumberofSeatBelts { get; set; }
}
实现接口
interface Customer
{
// Property signatures:
string Name
{
get;
set;
}
string pdw
{
get;
set;
}
}
class Login : Customer
{
// Fields:
private string _name;
private string _pdw;
// Constructor:
public Login ()
{
}
// Property implementation:
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string pdw
{
get
{
return _pdw;
}
set
{
_pdw = value;
}
}
}
始终对属性使用上限方法。Pdw不是Pdw
并且始终使用ICustomer之类的东西来声明接口,而不是Customer
基本上,为模型提供接口是有害的(长期项目),或者至少这是我的观点
为架构、存储库、总线等保留接口
并保持模型幂等且不依赖。(或者至少没有那么多依赖关系)
^依赖项等于接口或其他类。另一种方法如何?而不是
接口
:
public class Customer
{
public string Name { get; set; }
public string pdw { get; set; }
}
public class Login : Customer
{
}
Login
类现在可以访问基类属性,而不必自己实现它们
当然,如果您无法控制原始接口的使用,那么请忽略此答案,因为它只是显示另一种方法的一种方式。您没有在接口中指定代码,因此没有可继承的内容。类实现接口,而不是继承接口 但是,如果您想为派生类提供代码,您可以只编写一个基类来包含它 通常,您希望提供一个基类来实现接口的部分(但不是全部)。您可以通过编写一个抽象基类来实现接口的一部分,并将未实现的方法声明为抽象 举例说明:
interface ICustomer
{
string Name { get; set; }
string Password { get; set; }
string SomeOtherMethod();
}
public abstract class CustomerBase : ICustomer
{
public string Name { get; set; }
public string Password { get; set; }
// We do not implement SomeOtherMethod() here.
// Instead, make it abstract to force a non-abstract derived class to implement it:
public abstract string SomeOtherMethod();
}
class Login: CustomerBase
{
// This class does not need to implement Name or Password,
// because it can inherit them from CustomerBase.
// However it must still implement SomeOtherMethod()
// because that method is declared abstract in the base class.
public override string SomeOtherMethod()
{
return "Implemented by me";
}
}
请看一看:您不是从接口继承,而是实现它们。由于接口没有自己的实现,因此由您在自己的类中实现属性。您从其他类继承,而不是从接口继承。
interface ICustomer
{
string Name { get; set; }
string Password { get; set; }
string SomeOtherMethod();
}
public abstract class CustomerBase : ICustomer
{
public string Name { get; set; }
public string Password { get; set; }
// We do not implement SomeOtherMethod() here.
// Instead, make it abstract to force a non-abstract derived class to implement it:
public abstract string SomeOtherMethod();
}
class Login: CustomerBase
{
// This class does not need to implement Name or Password,
// because it can inherit them from CustomerBase.
// However it must still implement SomeOtherMethod()
// because that method is declared abstract in the base class.
public override string SomeOtherMethod()
{
return "Implemented by me";
}
}