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

bookChallenge: Encapsulated Product Class

Tehtävä

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.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

bookChallenge: Encapsulated Product Class

Pyyhkäise näyttääksesi valikon

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
single

single

some-alt