铸造问题 [英] Casting issue

查看:63
本文介绍了铸造问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计


i有这样的设置:


界面IMyInt //界面


class myClass:IMyInt // myCLass意识到这个界面


class myOtherClass:myClass //一个继承自该父级的类

绑定到界面

class myClassA:myOtherClass //继承

的类然后在我的代码中我这样做:


myClassA obj =( myClassA)myOtherClass; //< ----并抛出一个不能

将myOtherCLass转换为myCLassA错误

myOtherClass没有私有变量。为什么不能施展?

Hey guys

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that parent
bound to the interface
class myClassA : myOtherClass //a class that inherits
then in my code i do this:

myClassA obj = (myClassA)myOtherClass; //<----and this throws a cannot
cast myOtherCLass to myCLassA error
myOtherClass has no private variables. Why can''t it cast?

推荐答案

我有这样的设置:
i have a set up like this:

>

界面IMyInt //界面


class myClass:IMyInt // myCLass意识到这个界面


class myOtherClass:myClass //继承自该界面的

父级的类


class myClassA:myOtherClass //继承的类


然后在我的代码中我这样做:

myClassA obj =(myClassA)myOtherClass; //< ----并抛出一个

无法将myOtherCLass转换为myCLassA错误


myOtherClass没有私有变量。为什么不能演员?
>
Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that
parent bound to the interface

class myClassA : myOtherClass //a class that inherits

then in my code i do this:

myClassA obj = (myClassA)myOtherClass; //<----and this throws a
cannot cast myOtherCLass to myCLassA error

myOtherClass has no private variables. Why can''t it cast?



这实际上是您使用的代码吗?如果是这样,你就不能施放类名,

你只能施放一个类的实例。所以,这段代码是非法的:


myClass obj =(myClassA)myOtherClass;


但是,这段代码应该有效:


myOtherClass instance = new myOtherClass();

myClass obj =(myClassA)实例;


最好的问候,

Dustin Campbell

Developer Express Inc.

Is this actually the code your using? If so, you can''t cast a class name,
you can only cast an instance of a class. So, this code is illegal:

myClass obj = (myClassA)myOtherClass;

But, this code should work:

myOtherClass instance = new myOtherClass();
myClass obj = (myClassA)instance;

Best Regards,
Dustin Campbell
Developer Express Inc.


AHH!对不起,我在我的例子中犯了一个错误。是的,这是一个

的实例。


好​​的更正:


i有这样的设置:


界面IMyInt //界面


class myClass:IMyInt // myCLass意识到这个界面


class myOtherClass:myClass //继承自该父级的类

绑定到接口

class myClassA:myOtherClass //继承的类

然后在我的代码中我这样做:

myClassA obj =(myClassA)some_Instance_of_myOtherClass; //< ----并且这个

抛出一个无法将myOtherCLass转换为myCLassA错误

myOtherClass没有私有变量。为什么不能演员?


既然你说它应该演员我认为你看到我的上面的系数是正确的。所以

它还能做什么呢?我唯一的另一件事就是myClass与myClassA一样,是b $ b可序列化的。这会导致问题/我不明白为什么?

但是我不明白为什么这也不会工作。
AHH! Sorry i made a mistake in my example. Yes that was meant to be an
instance.

Ok corrected:

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that parent
bound to the interface
class myClassA : myOtherClass //a class that inherits
then in my code i do this:

myClassA obj = (myClassA)some_Instance_of_myOtherClass; //<----and this
throws a cannot cast myOtherCLass to myCLassA error
myOtherClass has no private variables. Why can''t it cast?

Since you said it should cast i presume you see my above coe as right. So
what else could it be? The only other thing i have is that the myClass is
serialisable as is the myClassA. Would that cause an issue/ i don''t see why?
But then i don''t get why this wont work either.


AHH!对不起,我在我的例子中犯了一个错误。是的,这是一个
AHH! Sorry i made a mistake in my example. Yes that was meant to be an

实例。


好​​的更正:


i有这样的设置:


界面IMyInt //界面


class myClass:IMyInt // myCLass实现界面


class myOtherClass:myClass //继承自

父级的类绑定到接口


class myClassA: myOtherClass //继承


的类然后在我的代码中我这样做:

myClassA obj =(myClassA)some_Instance_of_myOtherClass; //< ----和

这会抛出一个无法将myOtherCLass转换为myCLassA错误


myOtherClass没有私有变量。为什么不能演员?
instance.

Ok corrected:

i have a set up like this:

Interface IMyInt //interface

class myClass : IMyInt //myCLass realises that interface

class myOtherClass : myClass // a class that inherits from that
parent bound to the interface

class myClassA : myOtherClass //a class that inherits

then in my code i do this:

myClassA obj = (myClassA)some_Instance_of_myOtherClass; //<----and
this throws a cannot cast myOtherCLass to myCLassA error

myOtherClass has no private variables. Why can''t it cast?



实际上,我也没想过我的代码。变量

需要是要投射的myClassA的实例。像这样:


myOtherClass instance = new myClassA();

myClassA obj =(myClassA)实例;


最诚挚的问候,

Dustin Campbell

Developer Express Inc.

Actually, I hadn''t thought my code through very well either. The variable
needs to be an instance of myClassA to cast. Like this:

myOtherClass instance = new myClassA();
myClassA obj = (myClassA)instance;

Best Regards,
Dustin Campbell
Developer Express Inc.


这篇关于铸造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆