Vous êtes sur la page 1sur 2

Classes Demo: Part 1

Transcript

In this section we learned all about how to make classes, how to create constructors and we
learned about how to make methods for a class, how to take in parameters , how to send
values back from methods in a class and how to create instances of a class.

Lets take a look at a sample class I put together, and lets figure out the anatomy of the class.
At the top you'll see we define the class. And then we're listing some variables that are
instance variables for the class. I'm going to put in some comments here as we notice the
anatomy of our class instance variables.

And then I'm going to go down here. This is a special kind of method called a constructor. And
how many constructor methods can a class have? It can have as many as it wants to. This
example has one. One will always be provided by default, even if you don't define one, there
will be the empty constructor, that would look something like this...it would be blank and have
no body and none of the variable would be set up.

But we can define a blank constructor, and we'll be nice here and set up the instance
variables. We'll make sure that suit is set as equal to something, such as Diamonds. We can
set the name for the suit as Jack. And then we can set the points for the suit equal to 10
points.

Now up here, this constructor looks like it takes in two parameters, n1 and n2. With the n1, it
looks like the programmer has decide to pass n1 into the method getSuit and have it bring
back a value that is then assigned a suit. Let's go down here and look at getSuit, we scroll
down to the bottom and it looks like a I have a method here called getSuit that returns a string
data type. So if we pass in 1 it will return Diamonds, if we pass in 2 it will return Clubs, 3
it will return Spades and 4 it will return Hearts.

Likewise there's a similar method called getName, and if we pass in n2 to it, and you'll see
getName right here, it will assign it one of the different names. So I could actually use these
methods down here to save myself the time of actually typing out the word Diamonds, I could
say getSuit and then I need it to pass the number 1.

Then if I wanted to have the method to return the appropriate string for Jack, I could say
getName and I'll pass it the number 10, actually 10 would return word Ten and I want Jack
and the Jack is number 11, so I need to make that an 11. Then you can see that this method
would return the string Jack if I pass in an integer i11.

And then here we wanted to actually calculate the points for the Jack. So there was this
method called getPoints, down here. And if it takes in as a parameter a string and it returns an
integer which is the amount of points.

Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
So it will take in the string: Jack, King, Queen or Ten would return an integer of 10, otherwise
its going to return the appropriate point value for these other named cards.

So up here I'm going to say I want to getPoints and it needs a string , so I'll pass it the name
string and it will figure out how many points I need to get.

There are two constructors in our method and then down here we have four instance methods
that we've already looked at a little bit. The one we haven't looked at is the first one right here,
the toString instance method and this will convert our object card into a string it will actually
write out The and then here it will put the string for the name, so in this case it would put in
Jack and then of and Diamonds.

Now we're going to run over here to our main method, because this is a beautiful cookie
cutter, or template, but no cards actually exists unless we call them over here in main. So
over here in main, I'm going to create a card called c1 and I'll just say I want a new card and
that should make the correct card. Now I want to print that out on the screen, so I'm going to
do Sytem.out.printIn.

Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

Vous aimerez peut-être aussi