Let's see after reading parts 1 and 2. Suku. From this time, I will touch on difficult-to-understand parts such as object-oriented programming and classes, so let's get enthusiastic **
Java uses object-oriented programming, which is a barrier for beginners to understand. From the outline, the idea is "all things in this world are objects, right?" To give a concrete example, ** "food" is an abstract concept and there are many kinds of things **, but ** "curry rice" generally exists as a fixed thing **. They are just called "objects".
To describe object orientation, first describe how to handle multiple files in Java. This is because I want to roughly divide what is described ** for each file. The following is a program that calculates the numbers read by the keyboard (standard input), and the files are separated and only the calculation process is saved in a separate file.
java/Main.java
//A file that describes rough processing
import java.io.*;
public class Main
{
public static void main(String[] args)throws IOException //Exception handling
{
BufferedReader br = //You don't have to break
new BufferedReader(new InputStreamReader(System.in));
int num1, num2; //Variables to which numbers will be assigned later
int ans; //Variable to assign the answer
System.out.println("Please enter the two numbers you want to add");
System.out.print("The first one>"); //println breaks, but print does not
String str1 = br.readLine();
num1 = Integer.parseInt(str1);
System.out.print("Second>");
String str2 = br.readLine();
num2 = Integer.parseInt(str2);
System.out.println("mathematics.Java call\n"); // \n is a line break code
ans = math(num1, num2); //Leave the calculation to another file and substitute
System.out.println("Calculation result>" + ans);
br.close(); //Declaration of termination of br
}
}
From here, processing with another file
java/mathematics.java
class mathematics
{
int math(int num1, int num2) //See below for details. Defining a math function
{
int ans;
ans = num1 + num2;
return ans; //return is the statement to send to the original file
//Collect these three lines and "return"(num1 + num2);"Is fine.()It may not be necessary, but it is easy to understand, so describe it
}
}
When you do this, you should see the result of calculating the two numbers. This time, when separating the files, I didn't write the syntax "public static void main (String [] args)" in mathematicals.java, because it represents a ** file dedicated to execution **. Main.java is specified because it is a file to be executed.
Then, the calculation is passed to the file, but it can be executed by calling the part "int math (int num1, int num2)" written in another file in the Main file. By writing the type and name of the type you want to call (different names are possible, not recommended), such as "int num" in a separate file, you can call it in the math function. This is called ** argument **
And "return" at the end of the math function is the process of returning the answer to the original Main function and moving the process to Main. This value is called ** return value **. The first int of "int math (int ...)" written in mathematics.java means ** return value to int **.
Look back again and look at the two files. I think the syntax is more "visible" than when you first saw it.
--Play Java with multiple files --What is an argument? --What is return? --What if you want to specify an int type return value?
That's the main subject. An object is the curry I mentioned earlier. Even udon and tea are all objects. Not limited to Java, ** objects are objects that allow you to create free types and enter free values in the same way as functions, which is very convenient because you can create a wide range of processes. First from the syntax. From this point on, there is basically more than one Java file.
java/Car.java
public class Car
{
String carName; //Vehicle type
String companyName; //company name
double weight; //weight
int number; //number
Car(String carName, String companyName, double weight, int number) //There are four arguments
{
this.carName = carName; //More on this later.
this.companyName = companyName;
this.weight = weight;
this.number = number;
}
}
So far, a java file that creates a "car" type. Next, create a Main file that can be executed.
java/Main.java
public class Main
{
public static void main(String[] args)
{
System.out.println("Now call the "car" type to create the NISSAN Leaf");
String carName = "Leaf";
String companyName = "NISSAN";
double weight = 1600.0;
int number = 1234;
Car car1 = new Car(carName, companyName, weight, number);
//Car in Car File(4 arguments)
System.out.println(car1.Name + "Called");
//After the variable name, ".Write the item you want to call with ""
}
}
This time, I prepared a syntax to confirm that the Nissan Leaf was created and called. Let's look at each one.
I will explain from the syntax written in Car.java. The item you want to prepare is described after class Car. The important thing is after this, the part that describes Car () describes what is called "this". It has a ** reference to itself ** and is used to distinguish whether CarName is an argument or a variable prepared in Car.java. In this case, ** this is attached to the variable ** prepared in Car.java. ** The argument disappears without being saved when the processing of the function is completed **, so it is saved in the function.
Let's take a look inside the Main function. This time, a new syntax "new Car (omitted)" has appeared. This is the "object" that I have repeatedly mentioned. Explaining this time, the syntax is ** "The name and number of the Car type car1 is decided by Car (omitted)!" **. And ** the moment it is called, the assignment is done in Car.java, the process is saved and it returns to the Main function **.
And there is a pitfall here: "If you don't ** write in order **, it will be assigned to different variables." For example, the moment you write "new Car (companyName, carName, ...)" in Main, the company name and car model are swapped. It must be described in the same order in Car.java. Furthermore, ** at this stage, "only cars with four arguments can be created" **. For example, you can't say, "I don't care about the weight of the car! I won't write it!" The only way to avoid this is to ** write a syntax with three additional arguments **.
... but it's annoying, isn't it? I purposely write the contents of the whole street.
If it is only the contents, it can be skipped to some extent. Banzai It's easy to do. See the syntax below.
java/Car.java
//abridgement
//The syntax I wrote earlier
Car(String carName, String companyName, double weight, int number) //There are four arguments
{
this.carName = carName; //More on this later.
this.companyName = companyName;
this.weight = weight;
this.number = number;
}
Car(String carName)//Syntax to decide only the car model for the time being
{
this(carName, "undefined", 0.0, 0); //Pass processing to four-argument syntax
}
In this way, even if there is only one argument of Car, you can enjoy it by calling the part of 4 arguments inside (and reduce mistakes) A ** "type-determining function" like this one is called a "constructor" **, and calling one of the constructors with two or more combinations is called an "overload" **. ..
--What is object-oriented? --Free type, free processing --What is this? --Constructor
Recommended Posts