Funs with casting and inheritance

NOTE. This question is written in a C # alias, but I will really ask which languages ​​have a solution. Please do not dwell on the syntax.

Let's say I have two classes:

 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).
 }

Both of them are divided into this class:

 class CustomLabel
 {
     protected bool Bold;
 }

A bold field is displayed as public in drop-down classes.

There are no interfaces in classes.

Now I have a method that I want to get to go to CustomLabel and set the Bold property. Is it possible to do this without the need to: 1) find out what the real class of the object is and 2) apply it to this object, and then 3) make a separate code for each variable of each type of label to set the bold font. A view like this:

 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;
     }
 }

It would be nice to make one cast, and then set in bold on one variable.

, , , , .

?

, ? ( Delphi (Delphi 5)). , ( ), , ++, # Java.

, - , ? (, , .)

- ?

+3
6

Delphi. , , ( ) , , . , CustomLabel:

type
  CustomLabel = class
  private
    FBold: Boolean;
  protected
    property Bold: Boolean read FBold write FBold;
  end;

CustomLabel:

type
  TAccessCustomLabel = class(CustomLabel);

procedure SetBold(customLabel: CustomLabel)
begin
  TAccessCustomLabel(customLabel).Bold := True;
end;

as, TAccessLabel. AngleLabel NormalLabel, , CustomLabel , , Bold . , :

type
  AngleLabel = class(CustomLabel)
  public
    property Bold;
  end;

, . , , .


- ++, , Delphi, , .

, Delphi. ++ , Delphi, , .

class AccessCustomLabel: public CustomLabel
{
  friend void SetLabel(CustomLabel* customLabel);
};

:

void SetLabel(CustomLabel* customLabel)
{
  // Not allowed:
  // customLabel->bold = true

  // Not ordinarily allowed; requires friendship
  reinterpret_cast<AccessCustomLabel*>(customLabel)->bold = true;
}

undefined, , . CustomLabel, , Bold AccessCustomLabel, , Bold CustomLabel.


Delphi ++ punning. # Java; , , CustomLabel AccessCustomLabel, . . .

+7

, , , .

Delphi , . # Java . ++ , .

Bold Public CustomLabel, . Delphi, ++, # Java - .

+3

++ (, SetBold Bold ):

template<typename T> void SetBold(T t) {
    t.SetBold();
}
+1

Delphi 2005 , . "". #, , .

. , .

type
   TLabelHelper = class helper for CustomLabel
   public 
     procedure SetBolded(ABold : Boolean);
   end;

procedure TLabelHelper.SetBolded(ABold : Boolean);
begin
  Bold := ABold;
end;

...

Label.SetBolded(True);
+1

# 3.0 ; Delphi, Gerry.

( this).

public static class CustomLabelExtensions // name here is not important, just make it readable
{
    public static void SetBolded(this CustomLabel customLabel, bool newValue)
    {
        customLabel.Bold = newValue;
    }
}   

. , , .
, CustomLabelExtensions , " ", .

, , ( Delphi, ).

:

AngleLabel angleLabel;
NormalLabel normalLabel;
// some code that assigns values to the variables
angleLabel.SetBolded(true);
normalLabel.SetBolded(true);

-

+1

, (, , CCC) CustomLabel .

public class CCC : CustomLabel

SetBold (bool bold), .

public void SetBold(bool bold)
{
    base.Bold = bold;
}

AngleLabel Label CCC

and the SetBold (...) parameter will be of type CCC

0
source

Source: https://habr.com/ru/post/1747092/


All Articles