What to handle this time
--Method --Inheritance
Like the previous "constructor", it's hard to understand because it's an object-oriented place. I will describe it with an example. Please read up to part3. If you can understand this part, it will be easier to program in a different language, which is a very convenient way of thinking. Let's do our best.
Here, I will talk about the object-oriented and super important part, the method. A method is a part that describes "what kind of behavior" is given to an object created by oneself. When you create an object called "notebook PC", the constructor describes the object itself such as "who", "color", and "hardware configuration". On the other hand, ** methods can define behavior / state transitions such as "start", "connect to network", and "compress files" **.
java/Car.java
public class Car
{
int number;
String owner;
int speed;
public Car()
{
this(0000, "None", 0);
}
public Car(int number, String owner, int speed)
{
this.number = number;
this.owner = owner;
this.speed = speed;
}
void info()
{
System.out.println("number:" + this.number);
System.out.println("owner:"+ this.owner);
System.out.println("speed:"+ this.speed);
}
}
java/Main.java
public class Main
{
public static void main(String[] args)
{
Car car1 = new Car(1234, "Takahashi", 40);
car1.info();
Car car2 = new Car();
car2.info();
}
}
This time I made two cars. One is a simple owner, and the other is a car with nothing set. Car.java's "void info ()" is called by "~ .info ();" in Main.java, and it is executed in order, so all the information is output as information. When an object calls a method, like "car1.info ()", ** describe the method you want to call with "." After the variable name ** and call it. As with the constructor, if the method you call has ** arguments, include them as well **.
...
I want you to wait a moment.
Let's look back on part3.
"Transfer of calculation to a file, but execute by calling the part" int math (int num1, int num2) "written in another file in the Main file"
Yes, you ** have already written a "method with a return value" **. Incorporating object orientation basically doesn't change anything. ** "Methods that can only be executed by objects" ** Is it ** "Methods that the main function itself executes" ** Only changed.
Since the method is the part that describes the behavior of the object, in a sense ** "characteristics", the advantage is that the behavior is the same even if it is called by multiple objects **. This part is useful for the inheritance and interface that we will deal with next.
--What is a method? ――What does object-oriented programming do? ――What are the benefits?
I really wanted to write inheritance right away, but I'll touch on something that's really useful for doing inheritance with untouched syntax.
It is an array. I haven't done it yet.
As the meaning of the word, it is "a group of arrays arranged in a row from the beginning to the end". However, there are some annoying parts, so I will go carefully.
java/Array.java
public class Array
{
public static void main(String[] args)
{
int[] array = {1, 3, 5, 7, 9};
/*
↓ Doing the same thing in one line
int array[] = new int[5]; //Object syntax
array[0] = 1;
array[1] = 3;
array[2] = 5;
array[3] = 7;
array[4] = 9;
*/
for(int i = 0; i < array.length; i++) //array.length represents the length of the array
{
System.out.println((i+1) + "Number of items:" + array[i]);
//The for statement is turning println for 5 lines
//array[i]By, you can output from the beginning of the element
}
}
}
Arrays are hard to understand because the first number of the ** element number (array ** ** [?]) Is from 0 to **. I want you to divide it into such a thing. The syntax is almost the same as the object, but the difference is that [] is added to the name. ** The type is just "int []" **. Of course, it can be double or String, and ** objects can also be inserted **. Note that if you write in one line, "[]" will be attached to the pattern.
The contents of the for statement simply want to output the contents of the array. ** ".length" ** is used to prevent bugs as much as possible. What this means is that ** Java takes care of it without specifying the length of the array **. For example, when you want to manage data using multiple arrays, copying and pasting the same syntax with different elements is a long file and hard to see. In such a case, just knowing this, if you define it as a method, it will be executed with consideration for everything in one shot. Convenient.
Before explaining inheritance in Java, let's talk about the idea of inheritance.
** Inheritance means "giving one's own child behavior and characteristics" **. Specifically, the parent "car" can run and stand still, and is heavy and large. And the inherited "supercar" runs very fast, and the weight is a little lighter and bigger. In this way, ** a child that has been improved while retaining some functions and characteristics is called a subclass, and its parent is called a superclass **.
I will create a car this time as well, and create and explain a supercar that inherits it.
java/Car.java
public class Car
{
double weight;
int speed;
Car()
{
this(0.0, 0);
}
Car(double weight, int speed)
{
this.weight = weight;
this.speed = speed;
}
Car(int speed, double weight)
{
this(weight, speed);
}
void drive()
{
System.out.println("Speed" + this.speed + "Ran in");
}
void info()
{
System.out.println("Weight is" + this.weight + "kg");
System.out.println("The speed is" + this.speed + "is");
}
}
java/SuperCar.java
public class SuperCar extends Car
{
int max_speed;
SuperCar()
{
this(0, 0, 0);
}
SuperCar(double weight, int speed, int max_speed)
{
super(weight, speed);
this.max_speed = max_speed;
}
void CarChase()
{
System.out.println("Speed" + max_speed + "Car chase at");
}
void info()
{
super.info();
System.out.println("The maximum speed is" + max_speed + "is");
}
}
java/Main.java
public class Main
{
public static void main(String[] args)
{
Car normal = new Car(200.0, 40);
SuperCar superCar = new SuperCar(180.0, 50, 200);
superCar.drive(); //You can call it even though it is not written!
System.out.println("\n");
superCar.info();
}
}
This time, the car element was inherited by the supercar, and the method of the parent class was called by the subclass **. In Java (or rather object-oriented), children are better than parents. It is also possible to create methods and constructors unique to the child class and ** modify the contents of the parent class for the child class **. If you want to call the contents of the parent class as it is, use "super".
――What is inheritance? -> Handing down from parent to child ――What is a parent? What is a child? -> The handed down class is "child" ――What is "super"? -> Required when a child calls a parent
Recommended Posts