Objecten Using Objects
Agenda <ul><li>Swing vs. AWT </li></ul><ul><li>Object vs. Class </li></ul><ul><li>instance vs. Class </li></ul><ul><li>ins...
Classes and objects <ul><li>A  class  is the type of an  object </li></ul><ul><li>Just as a variable  counter  may have ty...
Assignment statements <ul><li>An  assignment statement  has the form: </li></ul><ul><ul><li>variable  =  expression  ; </l...
Combining declaration and assignment <ul><li>Declaration and assignment can be combined into a single statement: </li></ul...
Data in classes and objects <ul><li>A  class  is the type of an object </li></ul><ul><li>A class describes: </li></ul><ul>...
Messages to a  Graphics <ul><li>If you have a  Graphics , and its name is  g , here are some things you can do with  g : <...
Messages to a  Color <ul><li>Once you make a  Color , you cannot change it; you can only ask it for information </li></ul>...
The End “ If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will fru...
“ I invented the term ‘Object-Oriented’, and I can tell you I did not have C++ in mind.” --Alan Kay, creator of Smalltalk.
Upcoming SlideShare
Loading in …5
×

M C6java4

514 views

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
514
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

M C6java4

  1. 1. Objecten Using Objects
  2. 2. Agenda <ul><li>Swing vs. AWT </li></ul><ul><li>Object vs. Class </li></ul><ul><li>instance vs. Class </li></ul><ul><li>instance variable vs. Class variable </li></ul><ul><li>static vs. instance </li></ul><ul><li>main vs. new </li></ul>
  3. 3. Classes and objects <ul><li>A class is the type of an object </li></ul><ul><li>Just as a variable counter may have type int , Color.red has type Color </li></ul><ul><li>Just as 5 is a literal of type int , &quot;Hello&quot; is a literal of type String </li></ul><ul><li>There are exactly eight primitive types </li></ul><ul><li>There are thousands of classes, and you can create more </li></ul>
  4. 4. Assignment statements <ul><li>An assignment statement has the form: </li></ul><ul><ul><li>variable = expression ; </li></ul></ul><ul><li>Examples: </li></ul><ul><ul><li>classSize = 40; </li></ul></ul><ul><ul><li>area = pi * radius * radius; </li></ul></ul><ul><ul><li>uglyGreen = new Color(175, 175, 30); </li></ul></ul><ul><ul><li>myName = &quot;David Matuszek&quot;; </li></ul></ul>
  5. 5. Combining declaration and assignment <ul><li>Declaration and assignment can be combined into a single statement: </li></ul><ul><ul><li>int classSize = 40; </li></ul></ul><ul><ul><li>String myName = &quot;David Matuszek&quot;; </li></ul></ul><ul><ul><li>Color uglyGreen = new Color(175, 175, 30); </li></ul></ul><ul><li>You can only declare a variable once, but you can assign to it many times in many places </li></ul><ul><ul><li>A variable declared inside a method is declared only for that one method </li></ul></ul><ul><ul><ul><li>Hence, a method can declare a variable with the same name as a variable in another method—but it’s a different variable </li></ul></ul></ul>
  6. 6. Data in classes and objects <ul><li>A class is the type of an object </li></ul><ul><li>A class describes: </li></ul><ul><ul><li>How to make a new object of that class </li></ul></ul><ul><ul><ul><li>Example: new Color(175, 175, 30); </li></ul></ul></ul><ul><ul><li>What kind of data is in an object </li></ul></ul><ul><ul><ul><li>Example: a Color object contains three numbers representing the amount of red, green, and blue </li></ul></ul></ul><ul><ul><li>The methods of an object (the actions it can perform) </li></ul></ul><ul><ul><ul><li>Example: a Color object can tell you how much red it contains: </li></ul></ul></ul><ul><ul><ul><li>int amount = myColor.getRed(); </li></ul></ul></ul>
  7. 7. Messages to a Graphics <ul><li>If you have a Graphics , and its name is g , here are some things you can do with g : </li></ul><ul><ul><li>Tell it to use a particular color: </li></ul></ul><ul><ul><ul><li>g.setColor(Color.orange); </li></ul></ul></ul><ul><ul><li>Ask it what color it is using: </li></ul></ul><ul><ul><ul><li>Color currentColor = g.getColor(); </li></ul></ul></ul><ul><ul><li>Tell it to draw a line: </li></ul></ul><ul><ul><ul><li>g.drawLine(14, 23, 87, 5); </li></ul></ul></ul>
  8. 8. Messages to a Color <ul><li>Once you make a Color , you cannot change it; you can only ask it for information </li></ul><ul><li>The last method doesn’t change the color; it makes a new color </li></ul><ul><ul><li>// Make a new purplish color Color myColor = new Color(100, 0, 255); </li></ul></ul><ul><ul><li>// Ask how much blue is in it int amountOfBlue = myColor.getBlue(); </li></ul></ul><ul><ul><li>// Ask the color for a brighter version of itself Color brightColor = myColor.brighter(); </li></ul></ul>
  9. 9. The End “ If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.” --Anon.
  10. 10. “ I invented the term ‘Object-Oriented’, and I can tell you I did not have C++ in mind.” --Alan Kay, creator of Smalltalk.

×