Quindi capisco l'importanza del polimorfismo, compreso quanto sia vitale. Ma qualcosa che non capisco è ciò che riguarda il Constructor e qualsiasi% co_de ereditato% che potrebbe avere l'iniziale Class .
Ad esempio, ho una classe per testare alcuni criteri: livello dell'account, versione di Windows e altri controlli.
// Base Class
public class CheckInstalled : CheckUserLevel
{
public CheckInstalled()
{
// Check verify the CheckUserLevel token is true, if it isn't throw an exception and close.
if (!(_level == true))
{
// The above implementation would technically go here.
}
}
public virtual void IsInstalled()
{
// Implementation of initial check of general software.
}
}
Quindi in teoria sarebbe il Base Class che farà un controllo generico; ma ci sono altre applicazioni che devo testare se sono installate. Quindi ora creerei un Base Class per:
- Sql
- IIs
- Dnn
O qualsiasi cosa tu possa aver bisogno di assicurarsi sia selezionata. Quindi per ognuno di questi Class erediterei Classes . In ognuno avrei questo:
public override void IsInstalled()
{
// Implementation to check for that specific Class Item is installed.
}
Quindi vedo come Polymorphism semplificherà davvero e manterrà ognuno di questi CheckInstalled Class separato. Ma fornirà quella flessibilità per l'Orientamento agli oggetti.
La radice della mia domanda, Classes contiene quel valore di token ereditato. Ma fai tutto il resto Base Class Constructor che eredita il valore? Essenzialmente che Classes viene eseguito in modo coerente? O il token Base Class Constructor deve essere chiamato in ogni?
Speriamo che abbia senso. In caso contrario, fammi sapere e proverò a riformularlo.
Aggiornamento:
Il _level è invocato qui:
class CheckUserLevel
{
// Token Bool:
private bool _level = false;
#region Constructor:
protected CheckUserLevel()
{
// Invoke Method On Creation:
Elevate();
}
#endregion
public void Elevate()
{
// Get Identity:
WindowsIdentity user = WindowsIdentity.GetCurrent();
// Set Principal
WindowsPrincipal role = new WindowsPrincipal(user);
#region Test Operating System for UAC:
if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
{
// False:
_level = false;
// Todo: Exception/ Exception Log
}
#endregion
else
{
#region Test Identity Not Null:
if (user == null)
{
// False:
_level = false;
// Todo: "Exception Log / Exception"
}
#endregion
else
{
#region Ensure Security Role:
if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
{
// False:
_level = false;
// Todo: "Exception Log / Exception"
}
else
{
// True:
_level = true;
}
#endregion
} // Nested Else 'Close'
} // Initial Else 'Close'
} // End of Class.
}
La classe separata genera effettivamente il valore booleano; ogni volta che viene richiamata la classe, assegnerà un valore ad essa. Non volevo esporre troppo con questa classe principalmente per il suo ruolo nel test di amministrazione.
Il problema:
Several methods will require Administrative Permission; my thought was to inherit on the base
CheckInstalled. Which in it's creation will automatically check the boolean state of_level. Then from that point; thisCheckInstalledcan be inherited to other classes; then I can simplyoverride IsInstalledto bend to the desired task for that item.
Questo è un pensiero negativo da parte mia?