Although it says interface and static method I'm not talking about ** static functions ** in interfaces implemented in java8.
Those who expected this are sorry.
ArleneFamily.java
public final class ArleneFamily {
public static final class Father {
public static String name() {
return "Irene's dad";
}
public static String birthDay() {
return "3/2";
}
}
public static final class Mother {
public static String name() {
return "Irene's mom";
}
public static String birthDay() {
return "10/30";
}
}
}
Sample1.java
public class Sample1 {
public static void main(String[] args) {
System.out.println(ArleneFamily.Father.name() + ":" + ArleneFamily.Father.birthDay());
System.out.println(ArleneFamily.Mother.name() + ":" + ArleneFamily.Mother.birthDay());
}
}
There is a constant class like this ... It is the Irene family composition class (ArleneFamily.java) and the class that displays it (Sample1). Actually, it seems that there are many XX family composition classes other than the Irene family.
As an existing specification, I only displayed the configuration of each house. I want to display birthdays for both the Brett and Cindy families! !! There is something like that ...
Sample2.java
public class Sample2 {
public static void main(String[] args) {
System.out.println(ArleneFamily.Father.name() + ":" + ArleneFamily.Father.birthDay());
System.out.println(ArleneFamily.Mother.name() + ":" + ArleneFamily.Mother.birthDay());
System.out.println(BretFamily.Father.name() + ":" + ArleneFamily.Father.birthDay());
System.out.println(BretFamily.Mother.name() + ":" + ArleneFamily.Mother.birthDay());
・
・
・
}
It looks like this, but ** Korehanaidaro ... **
So with an interface, I decided to think that the user wants to have something like the following.
Sample3.java
public static void main(String[] args) {
printBirthDay(~ Moyamoya ~);
printBirthDay(~ Moyamoya ~);
・
・
・
}
private static void printBirthDay(~ Moyamoya ~) {
System.out.println(interface.name() + ":" + interface.birthDay());
}
Human.java
public interface Human {
public String name();
public String birthDay();
}
ArleneFamily.java
public final class ArleneFamily {
public static final class Father implement Human {
public static String name() {
return "Irene's dad";
}
public static String birthDay() {
return "3/2";
}
}
public static final class Mother implement Human {
public static String name() {
return "Irene's mom";
}
public static String birthDay() {
return "10/30";
}
}
}
Yes. This is an error. I was angry that I should remove the static method.
I understand that * static cannot be overridden, but I think it's okay to write if you are compelled to implement the interface *
Since it doesn't work, remove static from the method.
ArleneFamily.java
public final class ArleneFamily {
public static final class Father implement Human {
public String name() {
return "Irene's dad";
}
public String birthDay() {
return "3/2";
}
}
public static final class Mother implement Human {
public String name() {
return "Irene's mom";
}
public String birthDay() {
return "10/30";
}
}
}
Yes, this time there is an error in the part you are using. ↓ Here part
System.out.println(ArleneFamily.Father.name() + ":" +
Create an instance! Well, that's because I removed the static.
There is no choice but to fix the side that is using it ... There are quite a few, and it will be difficult within the range of influence. When I thought, ** I was shocked! ** **
ArleneFamily.java
public final class ArleneFamily {
public static final Human Father = new Human() {
@Override
public String name() {
return "Irene's dad";
}
@Override
public String birthDay() {
return "3/2";
}
};
If you do it like this, you don't have to touch it, right?
If you try Sure enough, this doesn't cause an error on the user side ... ↓ This part. It goes well.
System.out.println(ArleneFamily.Father.name() + ":" +
Furthermore, since the class structure was finalized in the form of instance creation Eliminates the writing of the moody part.
Sample3.java
public static void main(String[] args) {
printBirthDay(ArleneFamily.Father);
printBirthDay(ArleneFamily.Mother);
・
・
・
}
private static void printBirthDay(Human human) {
System.out.println(human.name() + ":" + human.birthDay());
}
For the time being, since it was originally a Constant class, I made the default constructor private so that it would not be new. With this, the repair of the original purpose is completed safely ~~
Well finally the main subject. (Lol)
If the above compiles, The original inner class (Father) and the variable that implements the Human interface (Father) Can you coexist? Which will be prioritized if possible? To the question ...
ArleneFamily.java
public final class ArleneFamily {
public static final Human Father = new Human() {
@Override
public String name() {
return "Irene's dad";
}
@Override
public String birthDay() {
return "3/2";
}
};
public static final class Father {
public static String name() {
return "Irene's father";
}
public static String birthDay() {
return "11/12";
}
}
Yes, I was able to live together. .. Change the return value and try which one is actually called.
Execution result
Irene's dad:3/2
The variable has been prioritized. The result of various investigations Here's 6.4.2. Obscuring, 6.5. Determining the Meaning of a Name It was written properly.
** Variables take precedence over types. Type takes precedence over package. ** **
I did not know··· Well, it's impossible to have the same name on site. It was known at a good opportunity this time.
By the way, I took Family as an example, but it is actually a product system. If you replace the name, it looks like this orz It feels strange.
It's a story behind java, so there is no heading.
Recommended Posts