我有两个类 - 基类和继承类如下.
基类:
TAlarm = class(System.Object)
private:
protected:
public:
constructor (tag:TTagname);
end;
Run Code Online (Sandbox Code Playgroud)
继承类:
TAlarmMsg = class(TAlarm)
public
constructor (aname:string);
method GetAlarmMsg:string; override;
method SendMsg(msg:string);
end;
Run Code Online (Sandbox Code Playgroud)
构造函数:
constructor TAlarm(tag:TTagname);
begin
Tagname := tag;
end;
constructor TAlarmMsg(aname:string);
begin
inherited TAlarm(aname); <========Here is my problem.
name := aname.ToCharArray;
end;
Run Code Online (Sandbox Code Playgroud)
无论我使用继承的构造函数调用什么或怎么调用,我在编译源文件时都会收到以下错误消息.
- 在继承的构造函数完成之前无法访问Self.和/或 - 在基类中找不到合适的构造函数,因此需要手动调用inherited
顺便说一下,我花了半天时间研究这个问题,并在网上找到了很好的信息.到目前为止没有任何帮助.我甚至在Delphi Prism Wikipedia(http://prismwiki.embarcadero.com/en/Constructors)上找到了直接讨论构造函数的网页.
那么,你会如何正确地做到这一点?谢谢,
MyForm继承自Form。我能够在VS2022应用程序中继承MyForm。但是,如果 MyForm 在库内,并且我的 VS2022 应用程序尝试创建从 MyForm 继承的表单,则会出现错误:
“无法显示此文件的设计器,因为无法设计其中的任何类。设计器检查了文件中的以下类: --- 无法加载基类 ''。确保已引用程序集并且所有项目都已建成。”
我认为这是 VS2022 最新版本中的一个错误。我在 Visual Studio 2019/2010 中没有遇到任何问题,解决此问题的任何解决方案都会很棒。
谢谢
我坚持使用BindingList,其中T是扩展A接口的接口.当我在绑定中使用此bindingList时,只有来自T的属性是可见的,而来自继承的A接口的属性则不可见.为什么会这样?它看起来像.net错误.我需要为我的2个项目分享一些常用功能.当PropertyChanged事件通过baseImplementation进行隧道传输时,绑定List的PropertyDescriptor为空.附加的接口和实现.SetUp方法到底
interface IExtendedInterface : IBaseInterface
{
string C { get; }
}
interface IBaseInterface : INotifyPropertyChanged
{
string A { get; }
string B { get; }
}
public class BaseImplementation : IBaseInterface
{
public string A
{
get { return "Base a"; }
}
public string B
{
get { return "base b"; }
protected set
{
B = value;
OnPropertyChanged("B");
}
}
protected void OnPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(p));
}
public event System.ComponentModel.PropertyChangedEventHandler …Run Code Online (Sandbox Code Playgroud) 我们知道你可以Func<T>像这样指向一个构造函数:
Func<MyObject> constructor = () => new MyObject();
var newObject = constructor();
Run Code Online (Sandbox Code Playgroud)
但有没有办法为你知道从MyObject继承的对象制作一个构造函数,但你不知道它的确切类型?
Type inheritedObjectType = obj; // Parameter
Func<MyObject> constructor = () => new MyObject(); // as inheritedObjectType
var newInheritedObject = constructor; // Should now be of the inherited type
Run Code Online (Sandbox Code Playgroud)
使用Activator或返回任何东西的答案都不Object是一种选择.
编辑:我不知道派生类型在编译时是什么类型.我只有一个System.Type.
根据这个页面
http://www.delphibasics.co.uk/RTL.asp?Name=Inherited
它说"它是在构造函数的开头调用,在析构函数的末尾调用.它不是强制性的,但建议作为良好的实践."
我没理解这个吗?这是否意味着我们不需要在构造函数或析构函数中放置'inherited',因为它会被编译器自动插入?
我在以下情况:
public class SuperClass
{
public static Object superClassStaticField;
}
public class ChildClass extends SuperClass
{
... some methods and fields
}
Run Code Online (Sandbox Code Playgroud)
我有一个看起来像这样的方法:
...modifiers... void action(Class <? extends SuperClass> cls)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想到达所有元素whit反射,包括超类静态字段,我(后来在ChildClass中)初始化,但看起来这里有一些逻辑在反射中失败:
superClassStaticField在Superclass中,所以如果我试图达到:
cls.getClass().get[Declared]Field("superClassStaticField");
Run Code Online (Sandbox Code Playgroud)
我会得到java.lang.NoSuchFieldException
所以我必须达到SuperClass的麻烦:
SuperClass.class.getDeclaredField("superClassStaticField").get(null);
Run Code Online (Sandbox Code Playgroud)
将成功运行,但如果有多个ChildClass在同一个运行时工作,我的程序变得疯狂.我认为beacuse所有方法都达到相同的对象,它在SuperClass中声明.
但是,我怎样才能达到实际给定的类静态字段?
我试过了:
SuperClass.class.getDeclaredField("superClassStaticField").get(cls);
Run Code Online (Sandbox Code Playgroud)
但结果是一样的.
我怎么能达到它?
编辑:我想现在只获取/设置静态字段,创建一个新实例是不安全的,(酸它有空构造函数?并且什么都不会修改?)
我写了一个小方法来列出继承的类型,但它不适TreeNode用于例如:
假设这个类:
class B { }
class A : B { }
class C :TreeNode { }
Run Code Online (Sandbox Code Playgroud)
然后:
GetInheritedTypes(typeof(A)); //typeof(B)
GetInheritedTypes(typeof(C)); // 0 items
Run Code Online (Sandbox Code Playgroud)
列出它们的方法:
List<Type> GetInheritedTypes(Type baseType)
{
return Assembly.GetAssembly(baseType)
.GetTypes()
.Where(type => type != baseType && type.IsAssignableFrom(baseType))
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
为什么GetInheritedTypes(typeof(C))返回0项而不是Typeof(TreeNode)?
我们直接进入代码:
public class Father {
public void DoSmth(Father object) {
// Do something else and print
Print(object);
}
public void Print(Father object) {
System.out.println("Father");
}
}
Run Code Online (Sandbox Code Playgroud)
public class Son extends Father {
public void Print(Son object) {
System.out.println("Son");
}
}
Run Code Online (Sandbox Code Playgroud)
public class Main {
public static void main(String[] args) {
Father o1 = new Father();
Son o2 = new Son();
o1.DoSmth(o1);
o1.DoSmth(o2);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我想得到:
Father
Son
Run Code Online (Sandbox Code Playgroud)
然后,我得到:
Father
Father
Run Code Online (Sandbox Code Playgroud)
我真的不太明白为什么(对于o1.DoSmth(o2))它从父类调用方法,因为o2是Son类型.无论如何我能得到想要的答案吗?
提前致谢
PS:实际上,我想从Father类中调用Print(Son类)方法.可能吗?
举个例子:
class Base {
Base (const Base & copyFrom) { globalRegister (* this); }
}
class Derived {
Derived (const Derived & copyFrom) : Base (copyFrom) {}
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了建议,在Baseived的初始化列表中包含Base的复制构造函数,以便复制Base的属性(如示例中所示).
但是,我有Base的复制构造函数将自身(*this)传递给其他对象(要向该对象注册).这是否真的必须在Derived的复制构造函数的初始化列表中使用(隐式或显式)Base(默认)构造函数,并且只在Derived的复制构造函数的主体中调用Base的复制构造函数,当实际存在对象时可以通过Base的复制构造函数附加吗?否则 - (*this)是一个有效的对象?
我正在尝试找到一种方法来创建具有各种继承对象的父对象列表.这是一个例子.
class Prog {
public Prog ( ) {
List<Shape> shapes = new List<Shape>();
shapes.Add( new Cube() );
shapes.Add( new Shape() );
//All those ways will not work, I cannot figure a way how to do this
shapes[0].CubeFunction();
(Cube)shapes[0].CubeFunction();
Cube cube = shapes[0];
}
}
class Shape {
public Shape (){}
}
class Cube : Shape {
public Cube (){}
public void CubeFunction(){}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何让Prog类中的代码工作?
在阅读了 TDictionary 相对于 TStringList 的显着性能改进后,我创建了以下类:
TAnsiStringList = class(TObjectDictionary<AnsiString,TObject>)
public
constructor Create(const OwnsObjects: Boolean = True); reintroduce;
destructor Destroy; override;
procedure Add(const AString: AnsiString);
procedure AddObject(const AString: AnsiString; AObject: TObject);
end;
Run Code Online (Sandbox Code Playgroud)
我这样编写构造函数:
{ TAnsiStringList }
constructor TAnsiStringList.Create(const OwnsObjects: Boolean = True);
begin
if OwnsObjects then
inherited Create([doOwnsKeys,doOwnsValues])
else
inherited Create;
end;
Run Code Online (Sandbox Code Playgroud)
...期望将调用此 TObjectDictionary 构造函数:
constructor Create(Ownerships: TDictionaryOwnerships; ACapacity: Integer = 0); overload;
Run Code Online (Sandbox Code Playgroud)
...如果指定了 Ownerships 参数。如果未指定 Ownerships 参数,我预计将调用以下继承的 TDictionary 构造函数:
constructor Create(ACapacity: Integer = 0); overload;
Run Code Online (Sandbox Code Playgroud)
代码编译并运行,但是当我调用时
inherited Create([doOwnsKeys,doOwnsValues]) I get the …Run Code Online (Sandbox Code Playgroud) 请注意:来电者只会抛出parentexception !!
说aexception,并bexception继承自parentexception.
在方法af,它throws aexception,bexception和parentexception.
void af() throws aexception, bexception, parentexception {}
Run Code Online (Sandbox Code Playgroud)
该方法caller调用af和throw parentexception只.
void caller() throws parentexception
Run Code Online (Sandbox Code Playgroud)
在这里,我们丢失了parentexception子类的信息.
该方法rootCaller调用该方法caller,rootcaller并且只能catch parentexception抛出caller并使用以下异常进程catch块:
void rootCaller() {
try {
caller();
} catch(parentexception e) {
if(e instanceof aexception) {
......
} else if(e instanceof bexception) {
......
} else if(e instanceof parentexception) {
...... …Run Code Online (Sandbox Code Playgroud) 我收到错误“{”类型 Device1 不是预期的。使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型。"}"
目前我有:
public abstract class Device
{
..
}
public class Device1 : Device
{ ... }
[Serializable()]
public class DeviceCollection : CollectionBase
{ ... }
[XmlRoot(ElementName = "Devices")]
public class XMLDevicesContainer
{
private DeviceCollection _deviceElement = new DeviceCollection();
/// <summary>Devices device collection xml element.</summary>
[XmlArrayItem("Device", typeof(Device))]
[XmlArray("Devices")]
public DeviceCollection Devices
{
get
{
return _deviceElement;
}
set
{
_deviceElement = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在做:
XMLDevicesContainer devices = new XMLDevicesContainer();
Device device = …Run Code Online (Sandbox Code Playgroud) inherited ×13
c# ×6
constructor ×3
java ×3
class ×2
delphi ×2
bindinglist ×1
c++ ×1
delphi-prism ×1
derived ×1
exception ×1
field ×1
forms ×1
func ×1
generics ×1
instanceof ×1
interface ×1
lambda ×1
list ×1
methods ×1
object ×1
oop ×1
overriding ×1
parent ×1
reflection ×1
static ×1
vb.net ×1
xmlinclude ×1