Home > Back-end >  Ordinary class can do, need to interface?
Ordinary class can do, need to interface?

Time:10-30

InterfacePerson {
Void the eat ();
Void the sleep ();
}

The class Studentimplements Person {
Public void eat () {
System. Out.println (" students go to the dining hall!" );
}
Public void sleep () {
System. Out.println (" students back to the dormitory to sleep!" );
}
}

The class Teacherimplements Person {
Public void eat () {
System. Out.println (" teacher staff restaurant!" );
}
Public void sleep () {
System. Out.println (" back to school teachers apartment sleeping!" );
}
}
The class Parents implements Person {
Publicvoid eat () {
System. Out.println (" parents to hostel restaurant for dinner!" );
}
Public void sleep () {
System. Out.println (" parents back to the hostel to sleep!" );
}
}

Public class PersonInterface {
Public static void main (String [] args)
{
Person p=new Student ();
p.eat();
P. leep ();
P=new the Teacher ();
p.eat();
P. leep ();
P=new Parents ();
p.eat();
P. leep ();
}
}







Program execution results:

Students go to the dining hall!

Students back to the dormitory to sleep!

Teachers to staff restaurant!

Back to school teachers apartment to sleep!

Parents go to hostel restaurant for dinner!

Parents back to the hostel to sleep!



Now need to add some functionality, you need to add "foreign guests, superiors" two types of roles, and after the tool need to add the identity of the corresponding role in, at this time, only need according to the need to add "foreigners", "leadership" class, and the main class can still use, the need for more changes, at this time can show the effect of the interface,

In the above program to add the following two classes,



The class Foreign implements Person {
Publicvoid eat () {
System. Out.println (" foreign guests to the hotel to eat!" );
}
Public void sleep () {
System. Out.println (" foreign guests back to hotel to sleep!" );
}
}

The class Leader implements Person {
Publicvoid eat () {
System. Out.println (" leadership to the hotel to eat!" );
}
Public void sleep () {
System. Out.println (" foreign guests back to hotel to sleep!" );
}
}







And the main function of usage is still the same,



The complete code is given below:



InterfacePerson {
Void the eat ();
Void the sleep ();
}

The class Studentimplements Person {
Public void eat () {
System. Out.println (" students go to the dining hall!" );
}
Public void sleep () {
System. Out.println (" students back to the dormitory to sleep!" );
}
}

The class Teacherimplements Person {
Public void eat () {
System. Out.println (" teacher staff restaurant!" );
}
Public void sleep () {
System. Out.println (" back to school teachers apartment sleeping!" );
}
}
The class Parents implements Person {
Publicvoid eat () {
System. Out.println (" parents to hostel restaurant for dinner!" );
}
Public void sleep () {
System. Out.println (" parents back to the hostel to sleep!" );
}
}
The class Foreign implements Person {
Publicvoid eat () {
System. Out.println (" foreign guests to the hotel to eat!" );
}
Public void sleep () {
System. Out.println (" foreign guests back to hotel to sleep!" );
}
}

The class Leader implements Person {
Publicvoid eat () {
System. Out.println (" leadership to the hotel to eat!" );
}
Public void sleep () {
System. Out.println (" led back to the hotel to sleep!" );
}
}

Public class PersonInterface {
Public static void main (String [] args)
{
Person p=new Student ();
p.eat();
P. leep ();
P=new the Teacher ();
p.eat();
P. leep ();
P=new Parents ();
p.eat();
P. leep ();
P=new Foreign ();
p.eat();
P. leep ();
P=new Leader ();
p.eat();
P. leep ();
}
}







Program execution results:

Students go to the dining hall!

Students back to the dormitory to sleep!

Teachers to staff restaurant!

Back to school teachers apartment to sleep!

Parents go to hostel restaurant for dinner!

Parents back to the hostel to sleep!

Foreign guests to the hotel to eat!

Foreign guests to the hotel to sleep!

Leading to the hotel to eat!

Led back to the hotel to sleep!


Interface I convert into ordinary classes, there are physical method, the following subclasses continue to rewrite, effect, so why interface?

CodePudding user response:

Such as for [the orc], [little people], [dead],

Eat and sleep only two functions, but the specific, their respective implementation,

Using just call, who is at the opposite? The fairy? The devil?

CodePudding user response:

You just need to know about interface such thing is ok,
When do you think of interface is redundant, that the scene is not suitable for using the interface,
When you meet need to the scene of the interface, you will naturally think of interface,

If it wasn't very basic wheel (for example: framework, engine), most of the time on the do not actually interface,

Suppose we now to develop games. There were 100 species of equipment (in the future will also upgrade to add more new equipment) equipment can increase the leading role of the HP MP speed defense of what,

Suppose each equipment are different classes, have equipped with E1 E2 E3...
Equipment on the list, and then lead in the list=[e1, e2, e3];
Then we should let the equipment affect the protagonist
For (E E: list) {
E.e ffect (hero);
}
Then you will find that E1 E2 E3 need to implement the interface, E and E need to have an effect method,
If there is no interface and class inheritance, this code is completely can't write,

CodePudding user response:

reference 1/f, old zizhu reply:
into [the orcs], for example, [little people], [dead],

Eat and sleep only two functions, but the specific, their respective implementation,

Using just call, who is at the opposite? The fairy? The devil?


I mean change the interface to ordinary class, effect is the same, are polymorphic, subclasses or write their own, but the top of the interface changed to regular classes, and interfaces, just ordinary class can write their own way, that is to say, general is on the top class, subclass inherits his below, polymorphic calls and use interface is the same effect, so I asked interface than regular classes where is good

CodePudding user response:

medical hand references 2 floor response:
you just need to know about interface such thing is ok,
When do you think of interface is redundant, that the scene is not suitable for using the interface,
When you meet need to the scene of the interface, you will naturally think of interface,

If it wasn't very basic wheel (for example: framework, engine), most of the time on the do not actually interface,

Suppose we now to develop games. There were 100 species of equipment (in the future will also upgrade to add more new equipment) equipment can increase the leading role of the HP MP speed defense of what,

Suppose each equipment are different classes, have equipped with E1 E2 E3...
Equipment on the list, and then lead in the list=[e1, e2, e3];
Then we should let the equipment affect the protagonist
For (E E: list) {
E.e ffect (hero);
}
Then you will find that E1 E2 E3 need to implement the interface, E and E need to have an effect method,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related