• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
346
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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