Challenge: Encapsulated Product Class
Aufgabe
Swipe to start coding
Write the complete Product class starting from the class declaration inside the ConsoleApp namespace. Your class must demonstrate encapsulation by controlling access to its internal data.
- Define
public class Productinside theConsoleAppnamespace. - Add two private fields:
name(of typestring) andprice(of typedecimal). - Implement a public method
GetName()that returns the value ofname. - Implement a public method
SetName(string value)that sets the value ofname. - Implement a public method
GetPrice()that returns the value ofprice. - Implement a public method
SetPrice(decimal value)that sets the value ofpriceonly if the value is not negative. - Do not add any code outside the
Productclass; do not change aMainmethod or any additional classes.
Lösung
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 4
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Großartig!
Completion Rate verbessert auf 4.17
Challenge: Encapsulated Product Class
Swipe um das Menü anzuzeigen
Aufgabe
Swipe to start coding
Write the complete Product class starting from the class declaration inside the ConsoleApp namespace. Your class must demonstrate encapsulation by controlling access to its internal data.
- Define
public class Productinside theConsoleAppnamespace. - Add two private fields:
name(of typestring) andprice(of typedecimal). - Implement a public method
GetName()that returns the value ofname. - Implement a public method
SetName(string value)that sets the value ofname. - Implement a public method
GetPrice()that returns the value ofprice. - Implement a public method
SetPrice(decimal value)that sets the value ofpriceonly if the value is not negative. - Do not add any code outside the
Productclass; do not change aMainmethod or any additional classes.
Lösung
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 4
single