Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Encapsulated Product Class | Class Design and Encapsulation
C# OOP Class Construction Drills

bookChallenge: Encapsulated Product Class

Uppgift

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 Product inside the ConsoleApp namespace.
  • Add two private fields: name (of type string) and price (of type decimal).
  • Implement a public method GetName() that returns the value of name.
  • Implement a public method SetName(string value) that sets the value of name.
  • Implement a public method GetPrice() that returns the value of price.
  • Implement a public method SetPrice(decimal value) that sets the value of price only if the value is not negative.
  • Do not add any code outside the Product class; do not change a Main method or any additional classes.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookChallenge: Encapsulated Product Class

Svep för att visa menyn

Uppgift

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 Product inside the ConsoleApp namespace.
  • Add two private fields: name (of type string) and price (of type decimal).
  • Implement a public method GetName() that returns the value of name.
  • Implement a public method SetName(string value) that sets the value of name.
  • Implement a public method GetPrice() that returns the value of price.
  • Implement a public method SetPrice(decimal value) that sets the value of price only if the value is not negative.
  • Do not add any code outside the Product class; do not change a Main method or any additional classes.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
single

single

some-alt