Interface interface personal understanding

Interface interface

interface use (eclipse)

Variables in interface

Because eclipse is powerful, we often use eclipse tips to display and compile lines of code that we thought were compiled incorrectly.
For example, let's enter variables.

interface A{
	public static final int a=0;
	public int b=0;
	public static int c=0;
	int d = 0;
}
public class Test implements A{
	public static void main(String[] args) {
		Test eTest = new Test();
		d=d+1;
		//The final field A.d cannot be assigned This comment shows the cause of d=d+1 compilation errors. Variables modified by final are not allowed to be changed
		System.out.println(d);
		}
}

We modify variables once with public static final, public static
Even in the end, I omitted writing int d = 0 directly from public.
Because the compiler will automatically add these modifiers, we can ignore the lengthy modifier prefixes and be lazy.
In d=d+1, it is proved that final modified variables can not be changed, and can be simply understood as perfect format. Perfection does not need to be changed.

The Method in interface

interface Test{

public abstract void a();
	void b();
}

The method in the interface is the same as that in variables, and can also omit public and abstract keywords.
The compiler will do a good job of adding modifiers for us, because the methods in the interface are always modified by public, abstract, and there will be no exceptions.
Because the methods in the interface are abstract, we can understand them as follows
The method in the interface is incomplete, similar to the God that someone believes in, so since it's abstract and ambiguous, we don't need to move him.
If there are subclasses that want to explain him, just push this method to subclasses.

Implementation of interface

The implementation of interface is accomplished with keyword implements
example

interface A {
	public abstract void menu();
	public abstract void mm1();
}
public class Test implements A{
	public void menu(){
		System.out.println("The Method of Implementing Interface");
	}


	public static void main(String[] args) {
		Test eTest = new Test();
		eTest.menu();
	}

}

Here class Test implements interface A, and implements the menu method inside.
Be careful
If this Test class fails to implement all the abstract methods in the A interface, as shown in Figure 1, the mm1 method does not handle it.
So this Test class is considered an abstract method.

Inheritance of interface

interface A{
	void t();
}
interface B{}
interface C{}
interface D extends A,B,C{}

As shown above
Interfaces can inherit other interfaces or abstract methods and variables of other interfaces.
Classes that implement interfaces can directly call abstract methods in interfaces inherited by this interface
It's complicated, to put it simply
A test class implements D interface, because D interface inherits the relationship of A interface, test class can directly implement t() method in A interface.

Daily use of interface

The interface cannot be instantiated. To prove the accuracy of this statement, let's try to create an interface object.

interface A {
	public A() {
		//Interfaces cannot have constructors
		//The previous line of comments shows that the compiler gave the reason for the error. There is no constructor in the interface.
		//So we naturally make mistakes when we create objects and call parametric constructors
	}
}
public class Test implements A{
	public static void main(String[] args) {
		A a =new A();//Here's the wrong reason. Look at the above.
	}

}

Troubleshooting

interface A {
	int x = 0;
}

class B {
	int x = 1;
}

class C extends B implements A {
	public void pX() {
		System.out.println(x);//There will be compilation errors because the compiler does not know whether the x we want to output is inherited or implemented.
							  //We can use super.x to denote the X of the parent class, but there's no way to interface it. Who makes people static final modifiers?
		//System.out.println(super.x);
	}

	public static void main(String[] args) {
		new C().pX();
	}
}

proxy pattern

The proxy mode uses interfaces. I'll introduce this proxy method using interfaces in several easy-to-understand classes. You remember to guide what the interface does here.

interface Heroer {	//We define a class that is a hero.
	//What can heroes do? Here's a blurred way, because we don't know what can be done to execute these Heroes'methods.
	public abstract void in();//Enter the Summoner's Canyon
	public abstract void fight();//Fight with other heroes
	public abstract void died();//The hero died
}

class Caller implements Heroer{	//Well, the hero has it. Who controls them, of course, is the summoner?
	private Heroer hero;
	public Caller() {
		this.hero=new Yasuo();//By default, of course, we're playing Yasso. Of course, you can also define a wandering mage. Here, you can default to the parameters of the hero, which are passed when the main function creates the object.
	}
	public Caller(Heroer hero) {
		this.hero=hero;//Well, in this case, who's playing who's playing?
	}
	public void in() {
		this.hero.in();//How can I possibly have heroic skills, so I just need to tell them to do these actions.
	}
	public void fight() {//I let the hero fight. If I were myself, I would be killed every minute.
		this.hero.fight();
	}
	public void died() {
		this.hero.died();//Still quote, after all, I won't die if the hero dies. Besides, how can my pit teammates possibly be? That's the network card.
	}
}

class Yasuo implements Heroer{	//We need to be the first exact hero, otherwise nobody knows which hero appears in our vision. Here we define Yasso.
	public void in() {
		System.out.println("Yasso has entered the Summoner's Canyon");
	}
	public void fight() {
		System.out.println("Heisergi,Yasso fought again. He was happy.");
	}
	public void died() {
		System.out.println("Is the life of a leaf just for the final analysis?");
	}	
}

class Liulang implements Heroer{//Because there are wandering wizards in the hero list, we can treat wandering wizards as heroes, otherwise we can't. Here's the relationship between father and son.
	public void in() {
		System.out.println("The wandering mage enters the Summoner's Canyon");
	}
	public void fight() {
		System.out.println("The wandering mage is supernatural!");
	}
	public void died() {
		System.out.println("I have witnessed the collapse of the kingdom, and I have never heard of it.");
	}	//We now define another hero: the wandering mage.
}

public class Computer {
	public static void main(String[] args) {
		//ok, let's start playing games.
		//Let's open the hero list first. Note that I opened it, not the computer.
		Caller call = new Caller();
		call.in();
		call.fight();
		call.died();
		//Okay, so we'll do it, because the presence of the summoner's agent makes it easier for us.
	}
}

Agent model represents a solution, just like you know the house through the landlord, because everyone does not know the house, but the landlord knows, so he will replace us to complete the method of understanding the house, we just need to determine this method to pay for it.
Of course, the werewolf who came directly to the house when I hadn't said that.

Tags: Eclipse network

Posted on Tue, 13 Aug 2019 05:52:39 -0700 by kobayashi_one