• No results found

Finishing the Sample

In document C# Player’s Guide (Page 153-156)

OK, there’s one more final thing we want to do here before we wrap up this chapter: make sure we see how to use our new Book class.

This class will work very much like what we saw in the previous chapter about using classes. In your Main method, you can create an instance of your Book class, and work with it like this:

Book book = new Book("Harry Potter", "J.K. Rowling");

// Changed my mind. Let's use the full name.

book.SetTitle("Harry Potter and the Half-Blood Prince.");

// Now I forgot. What was the title again?

Console.WriteLine(book.GetTitle());

This chapter has described the basics of building classes. You’ll keep learning more as you keep going through the next few chapters, but the real learning will come over the next few weeks, months, and even years. Splitting apart your program into classes is not always easy or intuitive.

I always tell people that class design is one part science, one part art, and one part black magic.

The science comes from learning basic principles and rules that tend to lead to better overall design (see the entire discipline of software engineering).

The art comes from practice and experience. After creating 10000 classes, you’ll have a natural, intuitive sense of what will work and what won’t.

The black magic part comes from the fact that no matter how much you know and how experienced you are, you’ll still get it wrong sometimes. Especially as you develop a program and try to add features you had never thought of. Fortunately, software can easily be refactored and rearranged from the wrong organization to the right organization.

There’s no such thing as getting it right all the time, so don’t beat yourself up when you do. That applies to both beginners and professionals. Just change it to make it work like you think it should and keep moving forward.

Finishing the Sample 131

Try It Out!

Designing and Building Classes. Try creating the two classes below, and make a simple program to work with them, as described below.

Create a Color class:

 On a computer, colors are typically represented with a red, green, blue, and alpha (transparency) value, usually in the range of 0 to 255. Add these as instance variables.

(What type did you chose to represent these and why?)

 A constructor that takes a red, green, blue, and alpha value.

 A constructor that takes just red, green, and blue, while alpha defaults to 255 (opaque).

 Methods to get (retrieve) the red, green, blue, and alpha values from a color object, as well as set new values for each.

 A method to get the grayscale value for the color, which is the average of the red, green and blue values.

Create a Ball class:

The Ball class should have a size/radius as well as a color instance variable. You should use the Color type you just created. Let’s also add an instance variable that keeps track of the number of times it has been thrown.

 Create any constructors you feel would be useful.

Create a Pop method, which changes the ball’s size to 0.

Create a Throw method that adds 1 to the throw count, but only if the ball hasn’t been popped (has a size of 0).

 A method that returns the number of times the ball has been thrown.

Write some code in your Main method to create a few balls, throw them around a few times, pop a few, and try to throw them again, and print out the number of times that the balls have been thrown. (Popped balls shouldn’t have changed.)

Try It Out!

Classes Quiz. Answer the following questions to check your understanding. When you’re done, check your answers against the ones below. If you missed something, go back and review the section that talks about it.

1. True/False. Classes are reference types.

2. (Classes/Objects) define what a particular type of thing can do and store, while a/an (class/object) is a specific instance that contains its own set of data.

3. Name three types of members that can be a part of a class.

4. True/False. If something is static, it is shared by all instances of a particular type.

5. What is a special type of method that sets up a new instance is called?

6. Where can something private be accessed from?

7. Where can something public be accessed from?

8. Where can something internal be accessed from?

Answers: (1) True. (2) Classes, object. (3) Instance variables, methods, constructors. (4) True. (5) Constructor. (6) Only within the class. (7) Anywhere. (8) Anywhere inside of the project it is contained in.

19

19 Properties

In the last chapter, I talked about how it is very common to have an instance variable, and then want to provide methods to get its value and set its value. This leads to lots of GetSomething and

SetSomething methods. This is so common that C# provides a very powerful feature that makes it easy to access the value of an instance variable called properties. This chapter will discuss why properties are so helpful, and several ways to create them.

In a Nutshell

 Properties provide a quick and effective approach to creating getters and setters for instance variables.

 You can create a property with code like the following:

public int Score {

get {

return score;

} set {

score = value;

if (score < 0) score = 0;

} }

 Not all properties need both a setter and a getter.

 Getters and setters can have different accessibility levels.

 Auto-implemented properties can be created that allow you to quickly define a simple property with default behavior (public int Score { get; set; }).

Auto-implemented properties can have a default value: public int Score { get; set; } = 10;

In document C# Player’s Guide (Page 153-156)

Related documents