假设如下:我们有B类,它是嵌套在A类中的私有类.没有任何继承自B类的类.问题是:编译器会自动将B类标记为Sealed吗?(在VB中不可继承).编译器是否有任何理由不将B类标记为密封?
我的想法是这样的:因为B类嵌套在A类中并且是私有的,并且没有其他类继承自B类,所以密封它应该是安全的,因为它不能在A类之外继承(不是甚至通过A)的子类.
Class A
Private Class B
End Class
End Class
Run Code Online (Sandbox Code Playgroud) 例如,我需要查看字符串是否包含子字符串,所以我只是这样做:
String helloworld = "Hello World";
if(helloworld.Contains("ello"){
//do something
}
Run Code Online (Sandbox Code Playgroud)
但如果我有一系列的项目
String helloworld = "Hello World";
String items = { "He", "el", "lo" };
Run Code Online (Sandbox Code Playgroud)
我需要在String类中创建一个函数,如果数组中的任何一个项包含在字符串中,它将返回true.
我想为这个场景覆盖包含(IEnumerable)函数Contains(string),而不是在另一个类中创建一个函数.是否可以这样做,如果是这样,我们如何覆盖这个功能?非常感谢你.
所以这里是完整的解决方案(谢谢你们):
public static bool ContainsAny(this string thisString, params string[] str) {
return str.Any(a => thisString.Contains(a));
}
Run Code Online (Sandbox Code Playgroud) 我目前正在使用XMLSerializer序列化我自己的类列表.类的一个属性是一个没有无参数构造函数的密封类的实例,因此XML Serializer拒绝序列化该类.我怎么能绕过这个?我需要将该属性序列化.
有什么方法可以指定该类应该如何序列化?
我们想继续使用XML; 是否有另一个我可以使用的XML序列化器,不会有这个问题?
我再次道歉,如果这是一个骗局,但我不知道该搜索什么.
[编辑]为了澄清,我无法访问密封类的来源.
我有一个带有受保护方法的密封类,我想测试它的行为.这使得很难直接测试,很难模拟.
它位于不是以TDD方式开发的代码库中,我现在正在为特定功能添加单元测试.
在这种情况下可能的一般方法是什么?目前我有:
还有吗?
关于方法重写和OOP原则的有效性,我有一点混乱.我知道关于密封,阴影,覆盖,虚拟等的一切,但我遇到了一个场景,这让我很困惑.假设我有:
class classA
{
public virtual void sayhello()
{
Console.WriteLine("hello I'm A");
}
};
class classB :classA
{
public override void sayhello()
{
Console.WriteLine("hello I'm B");
}
};
class Program
{
static void Main(string[] args)
{
classB a = new classB();
a.sayhello();
}
}
Run Code Online (Sandbox Code Playgroud)
根据我到目前为止研究的所有内容,可以使用子类中的override关键字覆盖声明为virtual或abstract(在抽象类中)的方法.根据这个,上面的代码工作完美.当我删除virtual关键字,然后尝试使用override关键字覆盖该方法时,编译器会给出错误:
不能覆盖继承的成员'inheritence.classA.sayhello()',因为它没有标记为虚拟,抽象或覆盖
然后我从子类中删除了覆盖关键字,并将实现提供为:
class classB :classA
{
public void sayhello()
{
Console.WriteLine("hello I'm B");
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该方法可以被覆盖.我能够覆盖不是虚拟或抽象的方法.所以,我的问题是:
1. …
为什么抽象类不能密封或静态?我也对此问题感到困惑为什么在C#中将静态类声明为密封和抽象?
我之所以这么问是因为我想创建一个具有 FileInfo 类(源自 FileInfo)的所有功能的类,并允许我向其中添加自己的属性。
我认为一个例子会更多地合作。我想要的是:
BindingList<FileInformation> files = new BindingList<FileInformation>();
public void GatherFileInfo(string path)
{
files.Add(new FileInformation(path));
listboxFiles.DataContext = files;
}
class FileInformation : FileInfo
{
public bool selected = false;
}
Run Code Online (Sandbox Code Playgroud)
对比我害怕我必须做的事情:
BindingList<FileInformation> files = new BindingList<FileInformation>();
public void GatherFileInfo(string path)
{
files.Add(new FileInformation(path));
listboxFiles.DataContext = files;
}
class FileInformation : FileInfo
{
string path = "<whatever>"
FileInfo fileInfo = new FileInfo(path);
public bool selected = false;
public string Name
{
get { return fileInfo.Name }
}
//Manually inherit …Run Code Online (Sandbox Code Playgroud) 我有类似下面的内容,我想将它们作为意图参数传递;
sealed class BasketType : Parcelable {
class BasketOne(val basketId: String): BasketType() {
constructor(parcel: Parcel) : this(parcel.readString()) {
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
super.writeToParcel(parcel, flags)
parcel.writeString(basketId)
}
override fun describeContents(): Int {
return 0
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误;
不能直接访问抽象成员
就在super.writeToParcel(parcel, flags)预料之中。
我四处寻找解决方法,但找不到。有任何想法吗?
sealed ×10
c# ×6
.net ×2
c#-4.0 ×1
c++-cli ×1
c++11 ×1
cil ×1
compilation ×1
inheritance ×1
kotlin ×1
oop ×1
optimization ×1
overloading ×1
overriding ×1
parcelable ×1
protected ×1
string ×1
unit-testing ×1
wpf ×1