1 / 9

Objects and Classes, Lecture 2 Definition

Objects and Classes, Lecture 2 Definition. “an object is a combination of some data (attributes) and some actions (methods)”. Hopefully the data and methods are closely related NOT randomly thrown together Creating Software Objects in Java. Example of an object.

madonna
Télécharger la présentation

Objects and Classes, Lecture 2 Definition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects and Classes, Lecture 2Definition “an object is a combination of some data (attributes) and some actions (methods)”. Hopefully the data and methods are closely related NOT randomly thrown together Creating Software Objects in Java

  2. Example of an object A Ball: Could have: Attributes : xPosition, yPosition diameter, colour graphics context Constructor Methods: display changeXPosition, changeYPosition moveXPosition, moveXPosition, slowMoveXPosition, slowMoveYPosition (xPosition, yPosition) diameter

  3. Classes Java provides many of its own classes.BUT for something like a Ball, a Triangle etc, the programmer must define what a ball or triangle is ( ie what it looks like) A class is like a template and an instance is an object ‘constructed’ from this template. “Constructor” a special method used to create or construct an object, most often used to initialize variables So what’s so special about it: Same name as class, never returns a value.

  4. //shortened version of class public class Ball { //attributes private int xPosition; private int yPosition; private int diameter; private Color col; private Graphics g; //constructor public Ball(int xPosIn, int yPosIn, int diamIn, Color colIn, Graphics gIn) //public method headers or signatures public void display() public void changeXPosition(int xPosIn) public void changeYPosition(int yPosIn) public void moveXPosition(int xMove) public void moveYPosition(int yMove) public void slowMoveXPosition(int xMove) public void slowMoveYPosition(int yMove) }

  5. public or private If private,the programmer has control, that is no-one outside the class can “see”, ie directly changethe variables. It is usual to make attributes private and write public methods to enable necessary changes This is “Encapsulation” or information hiding

  6. The constructor methods are made public so they can be seen, ie used from a different class, any private methods cannot be used from a different class. pinkBall = new Ball(50,100,20,Color.pink,g); diameter xPosition yPosition colour Graphics object

  7. Creating Multiple Instances? // declare Ball orangeBall, pinkBall; //construct orangeBall = new Ball(20,40,30,Color.orange,g); pinkBall = new Ball(50,100,20,Color.pink,g); //as many balls as you want/need all instances or objects

  8. A second class CreateTwoBalls which extends Applet, is used to construct, display and manipulate the Ball objects by using their methods: pinkBall.moveXPosition(50); pinkBall.moveYPosition(40); pinkBall.slowMoveXPosition(80);

  9. Class CreateTwoBalls (three main objects) 1. User interface object created from the class CreateTwoBalls . This is created by the browser or AppletViewer. 2. Ball objects, orangeBall and pinkBall, created from the class Ball. These are created by the user interface object of CreateTwoBalls .

More Related