When writing an instance field with "private" Since it may be missed by the getter and an error has occurred Be careful to write in a habit.
printData -Even if there is no instruction, you can create an instance method for output by yourself. Be able to write. I want to keep in mind the writing style that is easy for anyone to see.
-After writing in private, instead of outputting in Main.java When grouping the output with printData, it is in the same class, so instead of getName etc. If you write it with this.name etc., you can write it concisely, so remember it.
I used to use two classes in the exercises so far There was some code duplication in the class. Duplication should be avoided as it will be difficult to correct and improve.
At that time, a method of inheriting the contents of one class can be taken. This is called inheritance.
The original class is called a "super class" The inherited class is called a "subclass".
When first defining in a subclass
class subclass name extends superclass name{...
Write in the form of.
class Vehicle{
...
public void setName(String name){...
}
public void setColor(String color){...
}
}
//Main.in java
class Main{
public static void main(String[]args){
Car car = new Car();
car.setName("Ferrari");
car.setColor("Red");
...
}
}
When actually calling a method, write as above. (One case)
The fuel part of the Car class used so far is not in other classes Since it is a unique product, it must be customized in the Car class.
class Car extends Vehicle{
private int fuel = 50;
public int getFuel(){
return this.fuel;
}
When actually calling it, call it with car.getFuel () ;. (vehicle.getFuel (); gives an error)
super() How to call a superclass constructor in a subclass [Example 1]
//Super class
class Vehicle{
Vehicle(){
System.out.println("Super class");
}
//Car class
class Car extends Vehicle{
Car(){
super();
}
}
//Main class
class Main{
public static void main(String[]args){
Car car = new Car();
...
}
}
In the above, the output will be "superclass". [Example 2]
//First, define the constructor in the superclass.
class Vehicle{
private String name; ...
Vehicle(String name,String color){
this.name = name;
this.color = color;
}
//Call the Car class constructor / superclass constructor
class Car extends Vehicle{
Car(String name,String color){
super(name,color); //Don't forget to pass arguments.
}
}
protected When using superclasses and subclasses For fields that use private as before Access from subclasses cannot be done as it is. I used to use getters and setters, but when I use [protected] Allows access from subclasses and within the class.
・ Public → Can be accessed from anywhere ・ Private → Only accessible from that class · Protected → accessible from that class and subclass
Until now, subclasses of cars and bicycles in the superclass of vehicles I summarized it, but when there are methods with different contents (for example, run class) Use abstract methods.
class Vehicle{ ..
abstract public void run(int distance);
//Here we define the abstract method. I will not write the processing after that!
After that, overwriting is required in each subclass.
I don't actually write abstract methods, so I feel that I don't understand much. I would like to review Java once and create it as a page. For various reasons, it is better to touch PHP as soon as possible, so before that, let's move on to learning PHP.
Recommended Posts