This article organizes my experience-based understanding of the book Why make it object-oriented and the Youtube channel of Kuromoto author teaches !? Java. I wrote it because I wanted to. The above was very easy to understand, so please take a look.
Also, all the sample code that appears in the explanation below is Java. Please note that Java-specific writing is avoided as much as possible.
Experience in programming object-oriented languages But people who don't understand object orientation
It seems that it is difficult to develop into philosophy when it comes to deep stories, not limited to programming and design. So if a programmer like me needs to understand philosophy, that's NO.
In programming ** Object-oriented = Dividing a program into meaningful units ** I think it's good if you think about it.
As mentioned in the books and channels that appeared in "Introduction," I think there are many opportunities to explain object-oriented programming and programming by analogy. As is often the case, object-orientation is a representation of the real world! That said, I can't tell if it's an accurate explanation, but at least it's hard for me to understand.
Certainly there are things that are similar in expression to the real world, but I feel that it is difficult to connect with the real world unless you have a lot of imagination. So, I would like to talk about the program base.
For example, if you have the following code:
public class Main {
public static void main(String[] args) throws Exception {
String name = "Taro";
System.out.println(name);
}
}
It is a program that only displays the name "Taro" on the console. Let's add weight information here.
public class Main {
public static void main(String[] args) throws Exception {
String name = "Taro";
String weight = "70kg";
System.out.println(name + "Weight" + weight);
}
}
The rush that the weight is String type ...? Is omitted, and "Taro's weight is 70 kg" is output. Add Jiro here.
public class Main {
public static void main(String[] args) throws Exception {
String name1 = "Taro";
String weight1 = "70kg";
System.out.println(name1 + "Weight" + weight1);
String name2 = "Jiro";
String weight2 = "15kg";
System.out.println(name2 + "Weight" + weight2);
}
}
There is a set of name1 and weight1 and name2 and weight2 respectively, but with this writing method, the variable name is just like that, and the set is not guaranteed as a program. Let's define a class here.
public class Main {
public static class Human {
public String name;
public String weight;
public Human(String name, String weight) {
this.name = name;
this.weight = weight;
}
public void print() {
System.out.println(this.name + "Weight" + this.weight);
}
}
public static void main(String[] args) throws Exception {
Human taro = new Human("taro", "70kg");
taro.print();
Human jiro = new Human("jiro", "15kg");
jiro.print();
}
}
The execution result has not changed, and the amount of code has increased. However, name and weight are now a set. The main reason for using object-oriented programming is that it brings together related data and processing in this way, which is the basis of object-oriented programming, which makes it more maintainable (programs are easier to maintain). I think.
There are many other benefits to object orientation. In the above example, the amount of code increased, but it may decrease, and it also has an important mechanism for sharing development among multiple people.
Only keywords are listed here.
--Class
If there is next time, I would like to write an article that delves into the above contents.
Recommended Posts