Classes are one of the most important programming constructs of Java and in many other languages. They are the programming languages, allowing us to use the object-oriented paradigm to organise code and make it more understandable. Classes are an abstract model or blueprint for an object. An object, like in the real world, can literally be anything, and all objects have properties and functions. And, like the real world, our code can consist almost entirely of interacting objects.
We can also understand classes as extended data types. Unlike the primitive data types we have already looked at, classes define both variables and functions. Programmers call class variables attributes, and class functions as function methods, but otherwise, they work pretty much the same to as the variables and functions we already know. But in order to To get access to these attributes and methods, we need to user use a dot syntax: a , i.e. "." must be placed after the object name, followed by the name of the attribute or the method.
The structure of a class:
class className {
[Attributes - Variables]
[Constructor – Initialising function]
[Methods- Functions]
}
Constructor . For example car.speed might store the speed attribute of an object and the car.drive() method might be a method for moving the car around the screen.
Constructor
The constructor is the initialisation function of a class. This function is called when an a new instance (a new object) of a class is created.
An object is created with the command 'new' and the subsequent call to the constructor.
Attribute
Attributes are the variables of a class.
...
Methods are the functions of a class.
Child classes - Keyword 'extends'
A The basic principle of a class can be derived from another class, keeping all the original's qualities and allowing us to make new ones. This new class inherits the methods and attributes of of the base class. However, if new methods or attributes in the child class have the same names as those in the base class, the base class properties are overwritten. To call the constructor of a base class (in the case of a child class), the keyword 'super' is used.
The structure of the child class:
class className extends basicClass
{
[Attributes - Variables]
[Constructor – Initialising function]
[Methods- Functions]
}
Example BallKlasse
Note, that this example file uses tabs and multiple files for the first time. These are just to help us organise our code. When we run our program, processing puts these all together and treats them as one long document. As general rule, use a seperate file for each class, using the name of the class as the file name.
...
understood and used fairly quickly, and in some regards it Object Oriented Coding lends itself to a more Intuitive understanding of code. However, there is a lot of nuance and theory to writing a good class and it takes considerable practice.
Example BallClass
Below the draw function, you will find the BouncingBall
class. It’s common practice to separate classes into their own file, to help keep code organised.
Code Block |
---|
var ball1; var ball2; PVector p1 = new PVector(); PVector p2 = new PVector(); boolean drag = false; void function setup() { sizecreateCanvas(800,600); ball1 = new BouncingBall(100,100, 30); ball2 = new BouncingBall(50,50); shapeMode(CENTER); smooth(, 10); } voidfunction draw() { background(255); fill(255,255,255,60); rect(0,0,width,height); if(drag) { // draw the direction of shoot p1.set(ball1._pos.x,ball1._pos.y,0); line(p1.x,p1.y,p2.x,p2.y); } // draw the balls ball1.draw(); ball2.draw(); } voidfunction mousePressedmouseClicked() { drag = true; p1.set(ball1._pos.x,ball1._pos.y,0); p2.set ball1.setDirection(mouseX, mouseY,0); } void mouseDragged() { p2.setball2.setDirection(mouseX, mouseY,0); } voidclass mouseReleased()BouncingBall { drag = false; // shooting direction calculation.constructor PVector dir = PVector.sub(p2,p1);constructor(x,y,radius) //{ shorten the length this.x dir.mult(.09)= x; //the ball is given a new direction ball1.set(p1,dir,.993); ball2.set(p1,dir,.95); } |
BouncingBall
Code Block |
---|
class BouncingBall { PVector _pos; PVector _dir; float _dampV; PShape _shape; int _w; int _h; // constructor BouncingBall(int shapeWidth,int shapeHeight) { _pos = new PVector(width/2, height/2); _dir = new PVector(0,0); _dampV = 1; _w = shapeWidth; _h = shapeHeight; _shape = loadShape("ball1.svg")this.y = y; this.velocityX = 1; this.velocityY = 1; this._dampV = 0.99; this.gravity = 1.9; this.r = radius; } // set the new position + direction + dampening setDirection(targetX,targetY) { let newVelocityX = targetX-this.x let newVelocityY = targetY-this.y this.velocityX = newVelocityX *0.10; this.velocityY = newVelocityY*0.10; } // setupdate the newcurrent position + direction + dampening void set(PVector pos,PVector dir,float dampVcalcPos() { // add gravity _pos = pos.get(); _dir.add(dir); this.velocityY += this.gravity; _dampV =// dampV;apply friction to }velocity this.velocityX = // update the current position void calcPos() { // curent position shifted _pos.add(_dir); this.velocityX * this._dampV; this.velocityY = this.velocityY * this._dampV; // add velocity to position this.x += this.velocityX; // movement vector modified this.y _dir.mult(_dampV); += this.velocityY; // test horisontal if(_posthis.x + _w/2this.r > width) { _dirthis.xvelocityX *= -1; _posthis.x = width - _w/2this.r; } else if(_posthis.x - _w/2this.r < 0) { _dirthis.xvelocityX *= -1; _posthis.x = _w/2this.r; } // test vertical if(_posthis.y + _h/2this.r > height) { _dirthis.yvelocityY *= -1; _posthis.y = height - _w/2this.r; } else if(_posthis.y - _h/2this.r < 0) { _dirthis.yvelocityY *= -1; _posthis.y = _h/2this.r; } } // draw the ball void draw() { this.calcPos(); shape(_shape, _poscircle(this.x,_posthis.y, _w,_hthis.r); } } |
Download Example
Exercise
...
A
Modify the program
,so that
severalmany new balls
can be placed independently of each other
Exercise 12 B
- Extend the class again to make a third kind of ball. Modify its attributes and methods to create new kinds of behavior
Example BallKlasse Extended
...
are placed on the screen. Using an array and a for loop, it’s possible to quickly add hundreds balls!
Polymorphism
...
Object-Oriented Coding (using classes to organise code) provides a number of powerful ways to use classes and objects. One more advanced feature is inheritance, where a class can be based on an existing class. This new class inherits the methods and attributes of the base class. However, if new methods or attributes in the child class have the same names as those in the base class, the base class properties are overwritten. To reference the constructor of a base class from a child class, the keyword 'super' is used.
The structure of the child class:
class className extends basicClass
{
[Attributes - Variables]
[Constructor – Initialising function]
[Methods- Functions]
}
Example BallClass Extended
Code Block | ||
---|---|---|
| ||
class BallEx extends BouncingBall { BallExconstructor(int size) { super(size,size); } voidfunction draw() { super.draw(); line(0, 0 ,_pos this.x,_posthis.y); } } |