I wrote about the object-oriented programming I learned in the training as a memo for myself. I chew and summarize the contents that I have clogged or investigated.
·class ·instance ・ Member variables ・ Method ·constructor ・ Class variables (static variables) ・ Inheritance ·override
・ Class: Template, template (ex. Human) -Instance: An entity created based on a class (ex. A, B, ...)
-Member variables: Instance attributes (ex. Physical strength, intelligence, age) -Method: Action that the instance can do (ex. Walk, say hello) -Constructor: Initial setting of member variables
-Class variable (static variable): Class attribute (If you rewrite Mr. A's data, Mr. A's data and Mr. B's data will be changed in the same way)
-Inheritance: A mechanism that allows you to use the functions of the original class (superclass) when deriving a class -Override: When inheriting a class, redefining (overriding) the method (processing) of the superclass in the subclass.
At the bottom, I wrote the code that shows the relationship between classes, instances, inheritance, etc., so please read the explanation while comparing it.
After making a human model, create a Japanese model with human characteristics, Based on that, we are creating a person named Taro.
At this time, humans are superclasses, Japanese are subclasses, and Taro is an instance.
Member variables are the status of an instance. This time, humans will have the status of physical strength, intelligence, and language.
This is the initial setting of member variables. In this case, human physical strength and intelligence are set by arguments, and the language is set to null by default.
Also, since Japanese is my mother tongue, I set the language to Japanese in the Japanese class (Japanese template).
We have set two actions that all humans can do: walking and greeting.
In addition, as a behavior peculiar to Japanese, the behavior of bowing was set in the Japanese class.
Furthermore, greetings are actions that humans commonly perform, but the form of their output is If the Japanese "Hello", different from the like if the Chinese "Nihao". Therefore, you overload the greet method in Japanese class, we try to greet with "Hello".
================
Human.java
//Class (human template)
public class Human{
//Member variables (instance attributes)
int pysicalPower; //Physical fitness
int intellectualPower; //Intelligence
String langage; //language
//Class variable (static variable)
static int magicPower = 0; //Since it is static, all humans have 0 magical power. If someone's magic power is set to 10, everyone else will also have magic power of 10.
//Constructor (initial setting of member variables)
public Human(int pysical, int intelligence) {
this.pysicalPower = pysical;
this.intellectualPower = intelligence;
this.langage = null;
}
//Method (behavior)
public void walk() { //walk
System.out.println("Tech tech");
}
public void greet() { //Greet
System.out.println("Hello");
}
//Setting confirmation method
public int getPysicalPower() {
return pysicalPower;
}
public int getIntellectualPower() {
return intellectualPower;
}
public String getLangage() {
return langage;
}
}
Japanese.java
//Class (Japanese template)
public class Japanese extends Human { //A subclass that inherits from the Human class
//constructor
Japanese(int pysical, int intelligence) {
super(pysical, intelligence); //Call parent constructor
super.langage = "Japanese";
}
//Method
public void ojigi() { //to bow
System.out.println("Pecoli");
}
//override
@Override
public void greet() {
System.out.println("Hello");
}
}
Main.java
public class Main {
public static void main(String[] args) {
//Generate an instance (individual based on a template)
Japanese Taro = new Japanese(50, 30); /*Japanese Taro(Physical strength 50, intelligence 30)Generate a*/
//Check Taro's status
System.out.println("Taro's physical strength" + Taro.getPysicalPower());
System.out.println("Taro's intelligence" + Taro.getIntellectualPower());
System.out.println("Taro's language is" + Taro.getLangage());
System.out.println(); //Blank line
//Call a method (behavior)
Taro.walk(); //Human class methods
Taro.ojigi(); //Japanese class method
Taro.greet(); //Method overridden in Japanese class
}
}
Execution result
Taro's physical strength is 50
Taro's intelligence is 30
Taro's language is Japanese
Tech tech
Pecoli
Hello
When I thought about and wrote the code that actually uses object orientation, I got a little deeper understanding. There are some parts that I couldn't explain well because I didn't understand well, and some contents that I didn't include so as not to be complicated, so I think it might be a good idea to write the second one as the learning progresses. (If you feel like it ...)
Sites that I referred to when learning
-Thorough explanation for beginners! What is object orientation? (Especially, once I copied the part of "object-oriented programming learned from heroes", I understood it considerably) -What is a member variable? | An IT terminology dictionary that makes you feel like you understand even if you don't understand it
Recommended Posts