By +Biscuits Brown,
We rely on ads to keep creating quality content for you to enjoy for free.
Please support our site by disabling your ad blocker.
Continue without supporting us
If the prompt is still appearing, please disable any tools or services you are using that block internet ads (e.g. DNS Servers, tracking protection or privacy extensions).
Question
Ch33f
Here are 5 classes and their code. The entry point is set in program.cs and the class JewelThief inherits properties from Locksmith class .
class Jewels
{
public string Sparkle()
{
return "Sparkle, sparkle!";
}
}
class Safe
{
private Jewels contents = new Jewels();
private string safeCombination = "12345";
public Jewels Open(string combination)
{
if (combination == safeCombination)
return contents;
else
return null;
}
public void PickLock(Locksmith lockpicker)
{
lockpicker.WriteDownCombination(safeCombination);
}
}
class Owner
{
private Jewels returnedContents;
public void ReceiveContents (Jewels safeContents)
{
returnedContents = safeContents;
Console.WriteLine("Thank you for returning my jewels!" + safeContents.Sparkle());
}
}
class Locksmith
{
public void OpenSafe(Safe safe, Owner owner)
{
safe.PickLock(this);
Jewels safeContents = safe.Open(writtenDownCombination);
ReturnContents(safeContents, owner);
}
private string writtenDownCombination = null;
public void WriteDownCombination(string combination)
{
writtenDownCombination = combination;
}
public void ReturnContents(Jewels safeContents, Owner owner)
{
owner.ReceiveContents(safeContents);
}
}
class JewelThief : Locksmith
{
private Jewels stolenJewels = null;
public void ReturnContents(Jewels safeContents, Owner owner)
{
stolenJewels = safeContents;
Console.WriteLine("I'm stealing the contents!" + stolenJewels.Sparkle());
}
}
class Program
{
static void Main(string[] args)
{
Owner owner = new Owner();
Safe safe = new Safe();
JewelThief jewelThief = new JewelThief();
jewelThief.OpenSafe(safe, owner);
Console.ReadKey();
}
}
As mentioned in program.cs the line jewelThief.OpenSafe(safe, owner); the output should be
"I'm stealing the contents! Sparkle, sparkle!" since the class Jewel.thief is not properly overrided the output appears as "Thank you for returning my jewels!, Sparkle, sparkle!" my question here is not about inheritance or overriding.
In the Class Safe.cs and Class Owner.cs there are variables declared in their methods with reference to other classes, they are as follows respectively :-
public void PickLock(Locksmith lockpicker){...} and public void ReceiveContents (Jewels safeContents){...}
How are lockpicker and safeContents initialized and yet there is no error
Is this because of Visual studio C# which makes a shortcut and let these lines compile ?
if so whats the use of declaring variables like
string lockpicker or string safeContents
Link to comment
https://www.neowin.net/forum/topic/1321160-some-dumb-questions-about-c/Share on other sites
2 answers to this question
Recommended Posts