从基地继承继承类? [英] Casting up to inheriting class from base?

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

问题描述

假设我有一个类继承了一些基类:


BaseClass

{

void Foo()

{

更新();

}

}

Class1:BaseClass

{

void Update(){}

}

如何让基类知道哪个实例类已继承

它并在实例类中使用方法?只有某些类型的

类才会使用基数,这些类总是会有一个

Update()方法。


谢谢,

Brett

Say I have a class inheriting some base class:

BaseClass
{
void Foo()
{
Update();
}
}
Class1 : BaseClass
{
void Update(){}
}
How can I have the base class know which instance class has inherited
it and use a method in the instance class? Only certain types of
classes will use the base and those classes will always have an
Update() method.

Thanks,
Brett

推荐答案

这样做的一种方法是使用在
继承类。在基地,这样做:


((IMyClass)this).Update();


我对任何更好的东西感兴趣方法。


谢谢,

布雷特

One way of doing this is to use an interface implemented in the
inheriting class. In the base, do this:

(( IMyClass )this).Update();

I''m interested in any better methods.

Thanks,
Brett




Brett Romero写道:

Brett Romero wrote:

假设我有一个类继承了一些基类:


BaseClass

{

void Foo()

{

更新();

}

}


Class1:BaseClass

{

void Update(){}

}


如何让基类知道哪个实例类已经继承了?b $ b它并在实例类中使用一个方法?只有某些类型的

类将使用基数,这些类将始终具有

Update()方法。
Say I have a class inheriting some base class:

BaseClass
{
void Foo()
{
Update();
}
}
Class1 : BaseClass
{
void Update(){}
}
How can I have the base class know which instance class has inherited
it and use a method in the instance class? Only certain types of
classes will use the base and those classes will always have an
Update() method.



也许我错过了什么,但为什么你不能只做基类

摘要,像这样:


摘要BaseClass

{

void Foo()

{

更新();

}


摘要无效更新()

{...}

}




Maybe I''m missing something, but why can''t you just make the base class
abstract, like this:

abstract BaseClass
{
void Foo()
{
Update();
}

abstract void Update()
{ ... }
}

?


你好Brett,

我很好奇为什么你在BaseClass中有Foo(),如果Update()可能不存在,那么你需要调用
吗?将Foo()放在

子类中会更有意义吗?看起来你可能想重新考虑你的设计 -

否则你会进入向下倾斜状态。 (从基类转换为
到派生类)。通常当我遇到这个问题时,我会把它作为一个提示

,它是时候重构代码......


另外,什么会如果确定派生类确实没有实现Update(),那么Foo()会这样做吗?如果它什么都不做,那么也许你b / b
应该在BaseClass中有一个虚拟的Update(),只需要什么,然后在子类中覆盖该函数。


不是我推荐这个,但是为了回答你的问题你可以

总是使用是。用于检查您正在处理的类型的关键字。

例如:


class Base

{

public void Foo()

{

if(this is Derived)

{

Derived d =(派生)这个;

d.Update();

}

}

}


类派生:基础

{

public void更新()

{

Console.WriteLine(我来自);

}

}


class Program

{

static void Main(string [] args)

{

派生d = new Derived();

d.Foo(); //打印我是派生的


基础b =新基数();

b.Foo(); //什么都不打印

}

}


希望有所帮助,

John


Brett Romero写道:
Hi Brett,
I am curious why you have Foo( ) in BaseClass if Update( ) might not
exist to be called? Would it just make more sense to put Foo( ) in the
subclass? It seems like you might want to reconsider your design --
otherwise you are getting into "downcasting" (casting from a baseclass
to a derived class). Usually when I run into that, I take it as a hint
that it''s time to refactor the code...

Also, what will Foo( ) do if it determines that the derived class does
not implement Update( ) ? If it just does nothing, then maybe you
should have a virtual Update( ) in the BaseClass that simply does
nothing, then override that function in the subclass.

Not that I''m recommending this, but to answer your question you can
always use the "is" keyword to check what type you''re dealing with.
For example:

class Base
{
public void Foo()
{
if (this is Derived)
{
Derived d = (Derived)this;
d.Update();
}
}
}

class Derived : Base
{
public void Update()
{
Console.WriteLine("I am derived");
}
}

class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.Foo(); // prints "I am derived"

Base b = new Base();
b.Foo(); // prints nothing
}
}

Hope that helps,
John

Brett Romero wrote:

一种方法是使用

中实现的接口继承阶级。在基地,这样做:


((IMyClass)this).Update();


我对任何更好的东西感兴趣方法。


谢谢,

Brett
One way of doing this is to use an interface implemented in the
inheriting class. In the base, do this:

(( IMyClass )this).Update();

I''m interested in any better methods.

Thanks,
Brett


这篇关于从基地继承继承类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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