In the following, What is object-oriented after all or only one thing to be careful about in programming About the part that could not be described in detail due to explanation Supplement.
I feel that the order is reversed, but for those who are new to "object-oriented", I will give a concrete example and get an image. The language is Java, but I write it so that I can understand it without knowing Java. Object-oriented is the programming of data and related methods (in real terms) in a ** class **. For example, consider the following "friend" class to manage friend information.
Friend.java
public class Friend {
private String name;//name
private String phoneNumber;//phone number
private String mailAddress;//mail address
public Friend(String name, String phoneNumber, String mailAddress) {
//this means self
this.name = name;
this.phoneNumber = phoneNumber;
this.mailAddress = mailAddress;
}
/**Output contacts*/
public void outputContact(){
System.out.println(name);
System.out.println("phone number:"+phoneNumber);
System.out.println("Email:"+mailAddress);
}
}
This class has three ** fields **, name
, phoneNumber
, and mailAddress
, and there is a ** method ** ʻoutputContact ()that uses them. The method
Friend (String name, String phoneNumber, String mailAddress)` with the same name as the class name is called the ** constructor ** and is used as follows.
Friend tanaka = new Friend("Ichiro Tanaka", "03-444-444", "[email protected]");
As you can see from the Friend constructor, this code sets "Ichiro Tanaka" in the name
field," 03-444-444 "in the phoneNumber
field, and "[email protected]" in the mailAddress
field. I am.
And when you call the method as below, the contact will be displayed.
tanaka.outputContact();
/*
Ichiro Tanaka
Phone number: 03-444-444
Email: [email protected]
*/
In this way, a class defines "what information it should have" (here name, phone number and email address) and "what function it has" (here contact output) * In the * blueprint **, the constructor is called with new and the data is actually put in and materialized, which is called ** instance **. One of the advantages of creating a class like this is that you can create multiple data and functions together. If you don't use a class, you'll end up with this code if you have many friends.
String tanakaName = "Ichiro Tanaka";
String tanakaPhoneNumber = "03-444-444";
String tanakaMailAddress = "[email protected]";
String yamadaName = "Yamada Taro";
String yamadaPhoneNumber = "03-555-555";
String yamadaMailAddress = "[email protected]";
String suzukiName = "Hanako Suzuki";
String suzukiPhoneNumber = "03-666-666";
String suzukiMailAddress = "[email protected]";
//Continued below...
System.out.println(tanakaName);
System.out.println("phone number:"+tanakaPhoneNumber);
System.out.println("Email:"+tanakaMailAddress);
System.out.println(yamadaName);
System.out.println("phone number:"+yamadaPhoneNumber);
System.out.println("Email:"+yamadaMailAddress);
System.out.println(suzukiName);
System.out.println("phone number:"+suzukiPhoneNumber);
System.out.println("Email:"+suzukiMailAddress);
//Continued below...
It's confusing and troublesome.
Friend tanaka = new Friend("Ichiro Tanaka", "03-444-444", "[email protected]");
Friend yamada = new Friend("Yamada Taro", "03-555-555", "[email protected]");
Friend suzuki = new Friend("Hanako Suzuki", "03-666-666", "[email protected]");
//Continued below...
tanaka.outputContact();
yamada.outputContact();
suzuki.outputContact();
//Continued below...
It will be refreshing if you write.
By the way, looking at the above fields and methods, the fields such as name
are ** private **, and the ʻoutputContact ()` method is ** public **. These are called ** modifiers **, meaning private means "private" and can only be accessed from within the class, and public means "public" and can be accessed externally.
System.out.println(tanaka.name);//Error because it is private
tanaka.outputContact();//Since it is public, it can be executed.
Please see Main part for why such a modifier exists.
The class above wrote "a blueprint that defines what information it should have and what features it has", but ** interface ** says "what features it should have". It is a kind of abstracted class that defines only "ka". For example, suppose you want to manage the data of your colleagues as well as your friends. But in either case, if you think there should be a contact output method, create an interface like this:
Human.java
public interface Human {
/**Output contacts*/
public void outputContact();
}
This means that " Human
should have a contact output method ". The implementation is done in a class that is ** inherited ** using the ** implements ** keyword, as the interface only defines what methods should be.
public class Friend implements Human{
...//(Omitted)
@Override
public void outputContact(){
System.out.println(name);
System.out.println("phone number:"+phoneNumber);
System.out.println("Email:"+mailAddress);
}
}
/**Colleague*/
public class Colleague implements Human{
...//(Omitted)
@Override
public void outputContact(){
//(Processing content)
}
}
Inheriting and overriding a method is called ** Override **.
Java final is a qualifier that means "that's it, it's a rule". Variables with this cannot be changed once the value is entered.
final int CONST_VARIABLE = 100;
CONST_VARIABLE = 50;//Error because I tried to change the value even though it was final
Variables whose values cannot be changed in this way are called "constants". It is customary to name with capital letters and "_" (underscore). So why do we need to create "variables" whose values cannot be changed? That's to make the code easy to understand. For example, suppose you have the following code.
double price = 100 * 1.08;
As of November 2017, you may know that you are multiplying the sales tax rate, but those who read this code after the consumption tax rate changes in the future may not understand why it is multiplied by 1.08. But what about code like this:
final double CONSUMPTION_TAX_RATE = 0.08;//sales tax rate
double price = 100 * (1 + CONSUMPTION_TAX_RATE);
In this case, you can see that the ".08" part means the consumption tax rate, so it will be a code that can be understood and modified by future people whose consumption tax rate has changed.
Java static means "static". It means that it is not different for each instance generated "dynamically" (opposite of "static") in new like the name
field of the Friend
class above, but is common to all classes. I will.
For example, if the Friend class has the following code,
public class Friend {
public static int hoge = 10;
public static int fuga(){
return 20;
}
//(Omitted below)
}
As shown below, it is common to all classes and can be used without new (instantiation).
Friend yamada = new Friend("Yamada Taro", "03-555-555", "[email protected]");
Friend suzuki = new Friend("Hanako Suzuki", "03-666-666", "[email protected]");
//Common to different instances
yamada.hoge = 30;
System.out.println(yamada.hoge);//30
System.out.println(suzuki.hoge);//30
//Can be used without creating an instance in the first place
System.out.println(Friend.hoge);//30
System.out.println(Friend.fuga());//20
However, it is better to avoid using the static field without the above final. As I wrote in Main part, object-oriented programming has the advantage that it can be used without knowing the inside of the class. For example, if Friend tanaka = new Friend ("Ichiro Tanaka", "03-444-444", "[email protected]");
, the data explicitly given ("Ichiro Tanaka", "03-444-444") "," [email protected] ") and the code is written assuming that it holds only the processed data. However, the presence of a static field can be rewritten in a completely different location and behave differently than expected, defeating the object-oriented benefits.
On the other hand, methods should be static as much as possible if possible. For example, a method to check whether the phone number is in the correct format ("xxx-xxxx-xxxx" (a series of numbers and hyphens)) with Regular expression If there is, it will be understood that making it static "does not depend on a specific person (instance)", so it does not cause unnecessary confusion.
public class Friend {
...//(Omitted)
public static boolean isValidPhoneNumber(String phoneNumber){
Pattern p = Pattern.compile("^[0-9]+-[0-9]+-[0-9]+$");
Matcher m = p.matcher(phoneNumber);
return m.matches();
}
}
System.out.println(Friend.isValidPhoneNumber("03-444-444"));//True for the correct format
System.out.println(Friend.isValidPhoneNumber("03-44a-444"));//False due to entry error
In addition, although I wrote above that non-final static fields should not be used, it is recommended to define constants common to classes in static final because it is easy to understand.
public class Friend {
...//(Omitted)
/**Phone number format*/
private static final Pattern PHONE_NUMBER_PATTERN = Pattern.compile("^[0-9]+-[0-9]+-[0-9]+$");
public static boolean isValidPhoneNumber(String phoneNumber){
Matcher m = PHONE_NUMBER_PATTERN.matcher(phoneNumber);
return m.matches();
}
}
Recommended Posts