注意:这个问题是用类似于伪代码的C#编写的,但我真的会问哪些语言有解决方案。请不要挂上语法。有趣的铸造和继承
说我有两类:
class AngleLabel: CustomLabel
{
public bool Bold; // Just upping the visibility to public
// code to allow the label to be on an angle
}
class Label: CustomLabel
{
public bool Bold; // Just upping the visibility to public
// Code for a normal label
// Maybe has code not in an AngleLabel (align for example).
}
他们都从该类decend:
class CustomLabel
{
protected bool Bold;
}
大胆场暴露在后裔类公众。
这些类没有可用的接口。
现在,我有一个方法,我希望可以通过CustomLabel并设置Bold属性。这可以在不需要1)找出对象的真实类别并且2)投射到该对象然后3)为每个标签类型的每个变量分别设置粗体代码的情况下完成。有点像这样:
public void SetBold(customLabel: CustomLabel)
{
AngleLabel angleLabel;
NormalLabel normalLabel;
if (angleLabel is AngleLabel)
{
angleLabel= customLabel as AngleLabel
angleLabel.Bold = true;
}
if (label is Label)
{
normalLabel = customLabel as Label
normalLabel .Bold = true;
}
}
这将是很好,也许做一个演员,然后在一个变量上设置粗体。
我在想什么是做第四课,只是公开大胆的变量,并将我的自定义标签投给该类。
会这样吗?
如果是这样,它将使用哪种语言? (这个例子是从Delphi的旧版本(德尔福5)中提取的)。我不知道它是否适用于该语言,(我仍然需要尝试一下),但我很好奇它是否适用于C++,C#或Java。
如果没有,有什么想法会有效吗? (请记住没有提供接口,我不能修改类。)
任何人都有猜测?
你为什么要这么做? – alternative 2010-05-25 21:25:58
'AngleLabel'和'Label'不能控制''粗体'标志吗?为什么他们必须拥有自己的? (如果'Control.bold'与问题无关,为什么要显示它?) – sbi 2010-05-25 21:27:22
@sbi - 对于混淆,我会删除控件引用。正如我所说,这个例子是从Delphi中提取的。 Delphi中的控件类没有大胆的属性。 – Vaccano 2010-05-25 21:30:24