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

bookChallenge: Encapsulated Product Class

Oppgave

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

close

bookChallenge: Encapsulated Product Class

Sveip for å vise menyen

Oppgave

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 desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
single

single

some-alt