This article is for understanding the structure of the program through Processing. This time I will write about the class.
table of contents 0. What is a class?
A template (type) that contains related fields (variables) and methods (functions). It is often compared to recipes and blueprints.
The main reason is ① ** Because the program becomes easier to read. ** ** ② ** Because it can be reused. ** ** (Details on reuse later)
There are two types of classes: "** pre-prepared classes " and " self-defined classes **".
The classes prepared in advance are, specifically, String, PImage, PFont, PShape, PVector ... etc.
In Processing, ** many classes are prepared in advance **. String, PImage, PFont, PShape, PVector ... etc.
Since the class is just a template (type), it cannot be used as it is. So
① Make the class concrete ** Create an object ** ② ** Put the contents in the object ** ③ Treat the object as a ** variable **
In the procedure, we will use the class (information in it).
Let's look at a simple example.
string.java
//Declaration of object
String s1;
//Object definition
s1 = "breakfast"
//Use of objects
println(s1.length());
9
This program is a program that acquires and displays the character length. Let's take a closer look at each step.
** ① Create an object by embodying a class **
//Declaration of object
String s1;
** ② Put the contents in the object. ** **
//Object definition
s1 = "breakfast"
** ③ Treat the object as a variable **
//Use of objects
println(s1.length());
Every object has ** fields and methods ** that the class has.
To access the fields and methods of an object, use dots (.). The object itself is treated like a variable.
To use it like a variable ** Definition (declaration-> initial setting)-> use ** It means to use it like this.
Programming allows you to create your own original classes to simplify your program. Initially, we will simplify the program that displays the following bouncing balls.
bouncingball.java
float x = 30;
float y = 30;
float xspeed = 5;
float yspeed = 3;
void setup(){
size(398,198);
}
void draw(){
background(255);
noStroke();
fill(234,159,217);
ellipse(x,y,50,50);
x += xspeed;
y += yspeed;
if(x > width-20 || x < 20){
xspeed *= -1;
}
if(y > height-20 || y < 20){
yspeed *= -1;
}
}
To simplify the program ① ** Modularize ** ② ** Make it reusable ** I will do that.
bouncingball_module.java
float x = 30;
float y = 30;
float xspeed = 5;
float yspeed = 3;
void setup(){
size(398,198);
}
void draw(){
background(255);
display();
move();
edges();
}
//Function to display the ball
void display(){
fill(234,159,217);
noStroke();
ellipse(x,y,30,30);
}
//Function to move the ball
void move(){
x += xspeed;
y += yspeed;
}
//Function to bounce the ball
void edges(){
if(x > width-15 || x < 15){
xspeed *= -1;
}
if(y > height-15 || y < 15){
yspeed *= -1;
}
}
Point : By combining ** into one function **, the inside of the draw () function will be clean and the program will be easy to understand.
Reusable means that ** multiple outputs are possible **.
bouncingball_reuse.java
//Declaration of object
Ball b1;
Ball b2;
void setup(){
size(600,400);
//Define an object
b1 = new Ball(50,50,7,5);
b2 = new Ball(400,50,-7,-5);
}
void draw(){
background(255);
//Use object b1
b1.display();
b1.move();
b1.edges();
//Use object b2
b2.display();
b2.move();
b2.edges();
}
//Define a class
class Ball{
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;
//Ball class constructor
Ball(int xpos,int ypos,float xvelocity,float yvelocity){
x = xpos;
y = ypos;
xspeed = xvelocity;
yspeed = yvelocity;
}
//Function to display the ball(Method)
void display(){
fill(234,159,217);
noStroke();
ellipse(x,y,30,30);
}
//Function to move the ball(Method)
void move(){
x += xspeed;
y += yspeed;
}
//Function to bounce the ball(Method)
void edges(){
if(x > width-15 || x < 15){
xspeed *= -1;
}
if(y > height-15 || y < 15){
yspeed *= -1;
}
}
}
class Ball{
}
(The contents are removed so that the structure is easy to understand.)
class Ball{
//Function to display the ball(Method)
void display(){
}
//Function to move the ball(Method)
void move(){
}
//Function to bounce the ball(Method)
void edges(){
}
}
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;
//Define a class
class Ball{
//Function to display the ball(Method)
void display(){
}
//Function to move the ball(Method)
void move(){
}
//Function to bounce the ball(Method)
void edges(){
}
}
//Declare the object b1 to use.
Ball b1;
//Declare the object b2 to use.
Ball b2;
void setup(){
}
void draw(){
}
class Ball{
}
Declaration of object
Ball b1;
Ball b2;
void setup(){
//Assign an instance of the Ball class to object b1.
//An instance is a concrete example of a class.
//Object b1 is treated as a variable.
b1 = new Ball();
b2 = new Ball();
}
void draw(){
}
class Ball{
}
Point :b1⬅︎new Ball()
You are assigning a ** instance ** Ball () of the Ball class to a ** object ** b1.
Ball b1;
Ball b2;
void setup(){
b1 = new Ball();
b2 = new Ball();
}
void draw(){
//Dot(.)Use to access the methods inside the object.
b1.display();
b1.move();
b1.edges();
//Dot(.)Use to access the methods inside the object.
b2.display();
b2.move();
b2.edges();
}
class Ball{
}
The constructor is what you need to create multiple objects from your class. The object is for accessing the functions in the class.
//Declaration of object
//There is nothing inside yet
Ball b1;
Ball b2;
void setup(){
//Object creation(Definition)
//Assign an instance that embodies the class to the created object
b1 = new Ball(50,50,7,5);
b2 = new Ball(400,50,-7,-5);
}
void draw(){
//Dot(.)Use to access the methods inside the object.
b1.display();
b1.move();
b1.edges();
//Dot(.)Use to access the methods inside the object.
b2.display();
b2.move();
b2.edges();
}
class Ball{
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;
//Ball class constructor
//Data type variable
Ball(int xpos,int ypos,float xvelocity,float yvelocity){
x = xpos;
y = ypos;
xspeed = xvelocity;
yspeed = yvelocity;
}
void display(){}
void move(){}
void edges(){}
}
** Point **: Constructor The constructor connects the instance and the field.
(In this case) 7 = float xvelocity = float xspeed When the program is executed, xspeed is assigned 7.
** Point **: Instance The materialization of the object. In this case, ** Ball (50,50,7,5) ** is one of the class instances. Ball (400,50, -7, -5) is also an instance of the class. ** You can create multiple instances from the same class. ** **
Thank you for reading. We appreciate your opinions and suggestions in order to make the article even better.
Recommended Posts