For everyone who started studying Java as a new employee I would like to update it irregularly.
The theme this time is "How to write a class".
First, the basic form of the class. As an example, consider a class that represents a "person."
public class Person {
/**Full name*/
private String name;
/**age*/
private int age;
/**
*constructor
* @param name full name
* @param age age
*/
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/**
*Getting age
*/
public int getAge() {
return this.age;
}
/**
*Get one year old
*/
public void countUpAge() {
this.age++;
}
/**
*Self-introduction
*/
public void introduction() {
System.out.println("Hello, I" + this.name + ", " + this.age + "I'm old.");
}
}
Not limited to the classes introduced this time Classes are roughly divided into three sections.
public class name{
field
constructor
Method
}
In the Person class, it refers to the following parts.
/**Full name*/
private String name;
/**age*/
private int age;
Fields are ** variables ** that can be used anywhere in the class. In other words, you can refer to the same variable from different methods within the same class.
In the Person class, it refers to the following part.
/**
*constructor
* @param name full name
* @param age age
*/
public Person(String name, int age) {
this.name = name;
this.age = age;
}
By the way, when initializing this Person class, the code will be as follows.
Person person = new Person("Taro", 10);
In this way, the constructor is used to perform certain actions at the start.
Since the constructor is always called when initializing the object, It is often used to set field values.
In the Person class, it refers to the following part.
/**
*Getting age
*/
public int getAge() {
return this.age;
}
/**
*Get one year old
*/
public void countUpAge() {
this.age++;
}
/**
*Self-introduction
*/
public void introduction() {
System.out.println("Hello, I" + this.name + ", " + this.age + "I'm old.");
}
The process to be executed is described in the method.
The method is used to combine one process.
You can put all the processing together, It's easier for people to see if you divide each procedure separately.
You can give the method a name, so It is important to divide them properly according to the purpose and variables to be handled.
The basic form of the method is as follows.
Access modifier Return type Method name(Argument 1,Argument 2, ...) {
processing
return Return value;
}
Do you remember hearing the word "function" during math time?
f(x) = x + 1
If this is converted to a Java method, the description will be as follows.
public void func(x) {
return x + 1;
}
Both are "return by adding 1 to the argument x".
In fact, "methods" and "functions" are very close to each other and refer to almost the same thing. For example, in C language, the following description is called a function.
int func(x) {
return x + 1;
}
In Java and other object-thinking languages, the description of a function described in a class is called a method.
I would like to discuss the exact difference between functions and methods at another time.
Access modifiers define the extent to which a field or method can be accessed. It is specified as public or private.
If public is given, it will be accessible from outside the class, With prvate, you can only access it from within that class.
As an example, in the following case, a method called ** getAge ** that returns ** int (numerical value) ** is defined.
public int getAge() {
return this.age;
}
By specifying the type of this return value, the caller of this method is You can recognize that "this method returns an int type".
//You can get the age of person like this
int himAge = person.getAge();
//This will result in a type mismatch and an error
String himAge = person.getAge();
If there is no return value, set the character "void" in the return value part. void means "empty / nothing".
If void is specified for the return value, no value is returned, so it is not necessary to describe return.
/**
*Get one year old
*/
public void countUpAge() {
this.age++;
//You can omit the "return" that should be here.
}
Now, let's add a "method to set the name".
The name has a surname and a given name, and may have a middle name. Let's create a name setting method corresponding to each.
/**
*First and last name support method
* @param lastName Last name
* @param firstName first name
*/
public void setName(String lastName, String firstName) {
this.name = lastName + " " + firstName;
}
/**
*Method for middle name
* @param lastName Last name
* @param firstName first name
* @param middleName middle name
*/
public void setName(String lastName, String firstName, String middleName) {
this.name = firstName + "・" + middleName + "・" + lastName;
}
Now let's do each of these.
person.setName("Sato", "Taro");
// ->"Taro Sato"
person.setName("Sato", "Christina", "Taro");
// ->"Taro, Christina, Sato"
In this way, you can do different things with the same method named "setName".
This is called ** "method overloading" **.
Overloads can be used when you have multiple arguments for the same purpose.
I think there are quite a few people who say, "I don't understand because there are so many things in the class."
However, there aren't many rules to remember.
The source code will be easier to read just by dividing it into the three elements introduced this time.
Without trying to read everything suddenly Isn't it the first step toward understanding to think separately little by little?
Recommended Posts