开发者

Do derived classes inherit differently?

开发者 https://www.devze.com 2022-12-17 18:30 出处:网络
System.Web.UI.WebControls.UI.TableHeaderCell derives from System.Web.UI.WebControls.UI.TableCell So a method with the signature foo(TableCell tc){}
System.Web.UI.WebControls.UI.TableHeaderCell

derives from

System.Web.UI.WebControls.UI.TableCell

So a method with the signature foo(TableCell tc){}

Will accept instances of TableHeaderCell.

However if i create two new classes and derive one from TableCell and the other from TableHeaderCell then my new TableHeaderCell class will obviously not be able to cast into my new TableCell class as they are not directly related.

Is there开发者_StackOverflow anyway to get around this? Im guessing no.


You mean a hierarchy like this?

                  TableCell
                 /         \
                /           \
        TableHeaderCell   CustomTableCell
              /
             /                
  CustomTableHeaderCell

No, in that case you couldn't cast CustomTableHeaderCell to CustomTableCell.

Are you sure you really need inheritance in this case? Could you use composition instead? Or could you make CustomTableCell and CustomTableHeaderCell implement an ICustomCell interface? If you could give us more information about what you're trying to do, that would help.


You could just keep referring to all of them as TableCell objects, at least if the goal is to be able to call your foo().

0

精彩评论

暂无评论...
验证码 换一张
取 消