The role of Interface

zhaozj2021-02-16  52

1 INTERFACE The role is to let people who use the outfab do not know how it is implemented, but the way to call this is good. Just like the closing station, its method is to collect money. You give money, it gives you a change. You don't have to manage it is a computer or an abacus. It doesn't want you to know. Anyway function is achieved. 2 When you use outfab, you must use the method in the implementation of IMPLEMENTS, and often use it. List l = new arraylist (); List is an interface, ArrayList is its implementation. It is also very likely that there is such a method of public void m1 (list l) {...}. Write list is more common to make this method, as long as it is the implementation of the List. It's like this, the table is the same, but the money can be different. But the dollar can also be ingot. But they are all "money" realization, "money" itself does not make sense. 3 IMPLEMENTS and Extends. Implements is to learn from the teacher. I can worship a lot of people as a teacher, so I have the skills of these people, but I am still me, but this is a big thing; Extends is recognizing Dad. A person is a dad, and I have a father's skill. You can go to your arm for your dad, but you can't replace the old age, but I can't remember my dad, but I can recognize the teacher, there is no relationship. There may be other differences between this, but I haven't met. There are some, I can't say it. I still don't know when using the interface or abstract class, but I use the interface. Someone told me that it is best to correspond to an interface, I still don't understand how big it is, but everyone is written. In other words, a class should not come out, and the worst should also inherit an abstract class. This is all books, remember, but there is nothing.

So, when should I use an abstract class, when should I use? We know that only method names, method parameters, and return values, and static variables can be defined in the interface. The abstract class can have a method of implementation, or it can also define a non-static class variable. Abstract classes are classes, so they can only be inherited by single, but the interface can be achieved at a time. So, when you need more inheritance characteristics, you can only use the interface; when you need to implement some code in the parent class, the class does not want to be instantiated, it is designed as an abstract class. A basic principle is that first considering the use of the interface.

转载请注明原文地址:https://www.9cbs.com/read-17657.html

New Post(0)