Just put static
in the head.
The example below is an image of multiple people holding one wallet.
In this case, the idea is that each Human instance h1
, h2
does not have a field, but the Human
class has a field.
So static fields are also called ** class variables **.
Human.java
public class Human{
static int money=100;
}
Main.java
public class Main{
public static void main(String[] args){
//Create the first instance
Human h1 = new Human();
//Create a second instance
Human h2 = new Human();
// h1.Rewrite money class variable
h1.money=200;
// h2.Show money variable
System.out.println(h2.money);
//Since money is a class variable, it should be displayed as 200
}
}
Static methods can be called without instantiating. So, in the above example, suddenly
Main.java
System.out.println(Human.money);
It's okay to write something like that.
That's why the main
method is a static method.
One way to achieve encapsulation is to add access modifiers to classes and members to set visibility.
--public
: All classes
--protected
: Classes that belong to the same package as you or inherit from you
--packaged private
(default): Classes that belong to the same package as you
--private
: Only your own class
Test.java
public class Test{
private int i=1;
public void calc(int j){
return this.i+j;
}
}
The basic way to write how to set the field to private
and the method to public
and pass the value with getter and setter.
Test.java
public class Test{
private int num;
public int getNum(){
return this.num;
}
public void setNum(int num){
this.num = num;
}
}
--Inheritance: ʻexetends --Implementation: ʻimplements
Here, the inheritance relationship is ʻInu←
Shibainu. Note that the ʻosuwari
method is abstracted and needs to be overridden by the Shibainu
class.
Inu.java
public abstract class Inu{
public void ote(){
System.out.println("Hold out your left hand");
}
public abstract void osuwari();
//I don't write anything here
}
Shibainu.java
public class Shibainu extends Inu{
public void osuwari(){
System.out.println("Sit on the ground");
}
public void mate(){
System.out.println("Put up with rice");
}
}
Main.java
public class Main{
public static void main(Strgin[] args){
Shibainu dog = new Shibainu();
dog.ote(); //Inu derived method
dog.osuwari(); // Inu(←Shibainu)Origin method
dog.mate(); //Shiba inu derived method
}
}
Using ** polymorphism **
Main.java
Inu dog = new Shibainu();
dog.osuwari();
You can also say that.
In this case, the only methods that can be called are those that the ʻInuclass has. Therefore, even if the actually created instance is
Shibainu,
dog.mate cannot be called. When the ʻosuwari
method is called, the method of Shibainu
is processed instead of the method of ʻInu`.
Using this polymorphism, the following processing can also be written.
Main.java
Inu[] dogs = new Inu[3];
Inu[0] = new Shibainu();
Inu[1] = new Akitaken();
Inu[2] = new Tosaken();
for (Inu d: dogs){
d.osuwari();
}
It is very convenient when the contents of the same mate
method, such as" sit on the ground "," sleep "," do not listen ", etc., differ depending on the class.
super
.
The super
used here should be recognized as different from the super
when calling the method of the parent class in the method of the child class (this is the same for this
). *There are two conditions that can make a class an interface.
-** All methods are abstract methods. ** ** -** Basically it doesn't have any fields. ** **
"Basically" is because only fields (constants) with public static final
are allowed to be declared. However, in that case, public static final
may be omitted.
Declare ʻinterface instead of
class`.
Animal.java
public interface Animal{
void run();
}
Inu.java
public class Neko implements Animal{
void run(){
System.out.println("Run on four legs");
}
}
Since the interface allows multiple implementations
Neko.java
public calss Neko extends Mammal implements Animal, Organic{
//It has Mammal class, Animal interface and Organic interface as parents.
}
You can also write like this. The cat class has a mammal class as a parent and has an interface between animals and organic matter. It's not a very good example.
The beauty of interfaces and abstract methods is
-** Forced to override. ** ** -** Avoid unintended instantiation. ** ** -** Clearly distinguishable from methods that do nothing. ** **
Such a place.
[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%8F%E3%81%8B%E3%82%8BJava%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E4%B8%AD%E5%B1%B1-%E6%B8%85%E5%96%AC/dp/484433638X) Pp.332-524
Recommended Posts