简体   繁体   English

如果基类不在VB.Net中,则实例化派生对象时,基类的共享成员将如何反应?

[英]How do shared members from a base class react when a derived object is instantiated when the base class is not in VB.Net?

The questions is pretty straight forward, and can't find the answer in any of my readying. 这些问题很简单,在我准备好的任何时候都找不到答案。 How do shared members from a base class react when a derived object is instantiated when the base class is not instantiated in VB.Net? 在VB.Net中未实例化基类时,实例化派生对象时,基类的共享成员如何反应? Does the shared constructor get called at the base level? 共享构造函数是否在基类调用? I am trying to figure out if a base class initializes a set of shared members in the shared new() method defined at the base class will be accesible by the instantiated derired class, given the proper accessors, if the base class is not instantiated? 我试图弄清楚基类是否初始化了在基类定义的shared new()方法中的一组共享成员,如果基类未实例化,给定适当的访问器,实例化的derired类可以访问该共享成员吗?

Public Class Car
   Protected Shared _carCount as Integer

   Shared Sub New()
       _carCount = 0
   End Sub

   Shared Public Function GetCount() as integer
       return _carCount
   End Sub
End Class

Public Class Sedan
    Inherits Car

    Shared Sub New()
        _carCount = _carCount + 1
    End Sub
End Sub


Sub Main()
    Dim someSedan as New Sedan

    System.Console.Writeline(someSedan.GetCount())

End Sub

This code is not checked, but it should give enough of an idea what I am talking about. 这个代码没有被检查,但它应该足够让我知道我在说什么。

"derived object is instantiated when the when the base class is not instantiated in VB.Net?" “当在VB.Net中没有实例化基类时,派生对象是实例化的吗?”

When you instantiate a derived class, the base class constructor is always called . 实例化派生类时, 始终会调用基类构造函数。 A constructor for an instance of a type implicity calls that type's base class constructor first - even if you don't explicitly choose a constructor, the default constructor is run. 类型为implicity的实例的构造函数首先调用该类型的基类构造函数 - 即使您没有显式选择构造函数,也会运行默认构造函数。

Does the shared constructor get called at the base level? 共享构造函数是否在基类调用?

Yes. 是。 The shared constructor will get called at some point before the first access of the base class type . 在第一次访问基类类型之前,将在某个时刻调用共享构造函数。 This includes instantiation of the derived class, as any instantation of the derived class automatically (implicity) calls the base class constructor. 这包括派生类的实例化,因为派生类的任何瞬时自动(implicity)都会调用基类构造函数。 This, in turn, forces the type to be accessed. 反过来,这会强制访问类型。

The CLI specification guarantees that the shared (static) constructors will all be run prior to this occurring. CLI规范保证共享(静态)构造函数将在此之前运行。 You do not have to worry - your shared members will all get initialized via the shared constructor prior to being used. 您不必担心 - 您的共享成员将在使用之前通过共享构造函数进行初始化。

Shared members (Static in C#) don't concern themselves with instantiation so they will act just as they were intended. 共享成员(C#中的静态)不关心实例化,因此它们将按照预期的方式运行。 Shared constructors get called the first time the type is used so your sub class will have it's shared constructor called then the base class's 共享构造函数在第一次使用类型时被调用,因此您的子类将拥有它的共享构造函数,然后调用基类

Since shared (static) cannot be inherited, they will both run. 由于无法继承共享(静态),因此它们都将运行。

If the set of members are static then they will be "initialized" on that TYPE. 如果成员集是静态的,则将在该TYPE上对其进行“初始化”。 Again, you can't inherit shared (static) members. 同样,您不能继承共享(静态)成员。 You would have to explicitly call them from the base class to "initialize" them from the sub class 您必须从基类中显式调用它们以从子类“初始化”它们

as far as derived from another language, it doesn't matter. 就另一种语言而言,无关紧要。 It will work the same. 它将工作相同。 I'ts all IL in the end. 最后我都没有IL。

If you want to initialize a sub class but not the base class then you need to use composition, not inheritence. 如果要初始化子类而不是基类,则需要使用合成而不是继承。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在VB.NET的派生类中禁止基类的属性或方法? - How can I supress a property or method from a base-class in a derived class in VB.NET? 在其他项目中实例化时如何访问派生类的继承成员 - How to access inherited members of derived class when instantiated in a different project 调用需要在VB.NET或C#中将派生类实例键入为基类的方法 - Call a method that requires a derived class instance typed as base class in VB.NET or C# 在.NET中,基类可以以某种方式确保派生类定义共享成员吗? - In .NET, can a base class somehow ensure derived classes define shared members? 我如何将类(派生自通用“基”类)转换为该通用“基”类 - How do i convert a class (which is derived from a generic "base" class) to that generic "base" class 仅知道通用基类的类型时,如何查找通用基类的派生类? - How to find a Derived Class of a Generic Base Class when you only know the type of the Generic Base Class? 在对象派生类中强制转换对象基类 - Cast Object Base Class in Object Derived Class 序列化派生的 class 时,不包括 ProtoBuf.net 基础 class 属性 - ProtoBuf.net Base class properties is not included when serializing derived class 当派生类中存在新属性时,选择基类属性 - Choose base class property when a new property exists in derived class 在基类中执行静态方法时,如何获取派生类的类名? - How to get the class name of a derived class when executing a static method in the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM