请问C#中 抽象类有没有构造方法 构造方法中能不能带参数
4个回答
展开全部
抽象类是不可以被直接构造的,所以他的构造函数没有意义。很多语言中都不允许为抽象类添加构造函数,但是C#允许为抽象类添加构造函数。不过这是不推荐的做法,因为这样做会破坏设计原则!
以下为FXCop设计规则中对抽象类含有构造函数的警告:
Abstract types should not have constructors
TypeName:
AbstractTypesShouldNotHaveConstructors
CheckId:
CA1012
Category:
Microsoft.Design
Message Level:
CriticalWarning
Certainty:
95%
Breaking Change:
NonBreaking
Cause: A public type is abstract and has a public constructor.
Rule Description
Constructors on abstract types can only be called by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type with a public constructor is incorrectly designed.
How to Fix Violations
To fix a violation of this rule, either make the constructor protected, or do not declare the type as abstract.
When to Exclude Messages
Do not exclude a message from this rule.
Example Code
The following example contains an abstract type that violates this rule, and an abstract type that is correctly implemented.
[C#]
using System;
namespace DesignLibrary
{
public abstract class BadAbstractClassWithConstructor
{
// Violates rule: AbstractTypesShouldNotHaveConstructors.
public BadAbstractClassWithConstructor()
{
// Add constructor logic here.
}
}
public abstract class GoodAbstractClassWithConstructor
{
protected GoodAbstractClassWithConstructor()
{
// Add constructor logic here.
}
}
}
以下为FXCop设计规则中对抽象类含有构造函数的警告:
Abstract types should not have constructors
TypeName:
AbstractTypesShouldNotHaveConstructors
CheckId:
CA1012
Category:
Microsoft.Design
Message Level:
CriticalWarning
Certainty:
95%
Breaking Change:
NonBreaking
Cause: A public type is abstract and has a public constructor.
Rule Description
Constructors on abstract types can only be called by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type with a public constructor is incorrectly designed.
How to Fix Violations
To fix a violation of this rule, either make the constructor protected, or do not declare the type as abstract.
When to Exclude Messages
Do not exclude a message from this rule.
Example Code
The following example contains an abstract type that violates this rule, and an abstract type that is correctly implemented.
[C#]
using System;
namespace DesignLibrary
{
public abstract class BadAbstractClassWithConstructor
{
// Violates rule: AbstractTypesShouldNotHaveConstructors.
public BadAbstractClassWithConstructor()
{
// Add constructor logic here.
}
}
public abstract class GoodAbstractClassWithConstructor
{
protected GoodAbstractClassWithConstructor()
{
// Add constructor logic here.
}
}
}
展开全部
abstract (C# 参考)
abstract 修饰词可用於类别、方法、属性、索引子 (Indexer) 和事件。在类别宣告里使用 abstract 修饰词,表示该类别只是当做其他类别的基底类别而已。成员如果标记为抽象,或是包含在抽象类别 (Abstract Class) 内,则必须由衍生自此抽象类别的类别实作这个成员。
抽象类根本没有实作,又要衍生类来实作,构造函数又不可以继承,在抽象类中加构造函数和参数都是没有用的.我试过加上也不会报错.
abstract 修饰词可用於类别、方法、属性、索引子 (Indexer) 和事件。在类别宣告里使用 abstract 修饰词,表示该类别只是当做其他类别的基底类别而已。成员如果标记为抽象,或是包含在抽象类别 (Abstract Class) 内,则必须由衍生自此抽象类别的类别实作这个成员。
抽象类根本没有实作,又要衍生类来实作,构造函数又不可以继承,在抽象类中加构造函数和参数都是没有用的.我试过加上也不会报错.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
完全可以。虽然抽象类本身不能被实例化。
但是不能不让他的子类去调用基类的构造函数吧
但是不能不让他的子类去调用基类的构造函数吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
抽象当然也有构造方法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询