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

bookChallenge: Encapsulated Product Class

Taak

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.

Oplossing

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
single

single

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

close

bookChallenge: Encapsulated Product Class

Veeg om het menu te tonen

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
single

single

some-alt