C When should I use the interface?

Updated on number 2024-02-08
9 answers
  1. Anonymous users2024-02-05

    Interfaces are used for architecture.

    For example, to make a program, you need a network module, but you haven't implemented it yet.

    Then first define a network interface, such as inet, define it, etc., the sub-interface can define virtual functions, so that we don't have to wait for the network module to be written before writing other modules.

    Another example is chatting, I'll write the main control first:

    inet = new net() doesn't work with the interface.

    iui = new ui()

    oninetreceive

    onlocalchat

    onlocalchat(str)

    oninetreceive(str)

    In this way, I can let any two people give me the ui and net implementation.

  2. Anonymous users2024-02-04

    C is an event-based program, and C++ is message-based. An event is a special kind of delegate, and the function that the delegate points to is often an interface.

  3. Anonymous users2024-02-03

    Let me tell you about my own experience:

    What is the irreplaceable role of the interface in C? Simply abstracting the common features of a class?

    I tried the example, removing class a: icloneable, and it was still possible. So what's the use? I conclude that the interface is used for two reasons:

    1: The reason for your own design, with the specification, it is easier for others to understand how your system works together with your interface.

    2: If someone else has designed an interface, for example, icompare); Since someone else's argument is an interface, if you want to use this function, your class must also implement this interface. Remember:

    Interfaces correspond to classes, not methods, and the materialization of interfaces is classes. But the interface specifies the method to be implemented by the class.

    For example: public class valuecompare: icomparer}

  4. Anonymous users2024-02-02

    The interface is for encapsulation and is the simplest.

  5. Anonymous users2024-02-01

    1. The use of interfaces is not always considered from a design point of view. Interfaces, like other grammatical phenomena, together form the entire language system.

    2. The significance of the interface is abstract and unrestricted, so that similar things are universal and substitutable at the same level.

    3. Regarding decoupling, it is not that the interface can be decoupled, but that the abstract can be decoupled The interface is just a means, if the two things are necessarily related, then there will be no complete decoupling, only coupling and transfer.

  6. Anonymous users2024-01-31

    When you are doing hierarchical development, it is often used that the bottom layer advertises the interface for the upper layer to call.

    The interface is a kind of convention, just like everyone only does things according to the specified agreement, but as for this provision, how to do it, there can be different ways, this is the way to define the interface first and then implement, no need to wait for the implementation to be completed before it can be exposed, after the interface is defined, once it is released, it is best not to modify, otherwise it will cause the integrated classes to be modified.

    For example, let's say you have a project that wants to support a hardware device. In the design of the structure, it is quite possible that the implementation of this part will be implemented independently (possibly.

    It's done by your development team, or it may be outsourced). At this point, an interface can be developed. Both the client and server sides develop according to this definition. That's fine.

    It is guaranteed that the two parties can cooperate in the future. It's like producing screws and nuts.

    Sometimes the interface can also play a certain role in copyright protection. Let's say you make a remoting service. You need to publish a stub to your client. If the customer.

    The end is written by a third party. You probably don't want to go out without implementing a partial release. At this point, you can post an excuse definition.

    In general, changes to a service are made by adding to an existing interface rather than making a direct modification. For ease of management. This is generally done commercially, and you can of course rely on versions.

    to control, but no one does. If you change the existing interface, it will be difficult for you to gain the initiative if you encounter a more serious dispute and get into a dispute.

  7. Anonymous users2024-01-30

    Didn't find a suitable way to rewrite.

    Didn't find a suitable way to rewrite.

    It's very simple, I won't tell you exactly what was wrong, but if you tell you why it was wrong, you can change it yourself and be more impressed.

    First you have to understand the concept of rewriting.

    Here's an example. You see, why didn't this sentence say it, I didn't find a suitable way to rewrite it, and when you hit it, vs will give you a smart hint to tell you exactly what should be written in it.

    Whether it's int, string, or nothing, you're going to learn this method.

    Didn't find a suitable way to rewrite.

    Take this, for example.

    When you write this, vs will definitely be a smart hint.

    Tell you what you can write in it, but it must not be said that it can be empty, you are empty, so I didn't find a suitable way to rewrite it.

  8. Anonymous users2024-01-29

    Use the interface in the following cases:

    1.When there is a set of related methods that may be called.

    2.When a class only needs a single implementation of a method.

    3.When a class that uses an interface wants to cast that interface to a different interface or class type.

    A good example of using a single method interface without using a delegate is the icomparable or generic version of icomparable<(of <(t>)> icomparable declares the compareto method, which returns an integer specifying a relationship between two objects of the same type that is less than, equal to, or greater than. icomparable can be used as the basis for a sorting algorithm.

    While it is effective to use the delegate comparison method as the basis for the sorting algorithm, it is not ideal. Because the ability to compare is a class, and the comparison algorithm doesn't change at runtime, a single-method interface is ideal.

  9. Anonymous users2024-01-28

    Agreement. Reduced coupling. You ask this question, and the answer is everywhere. That's all I have to say. See what the downstairs has to say.

Related questions
13 answers2024-02-08

Go to bed at 10 o'clock at night at the latest but more than 11 o'clock, the best time for beauty sleep is from 10 o'clock in the evening to 2 o'clock in the middle of the night, this time period of the body's various functions begin to carry out corresponding detoxification activities, night is our most vigorous metabolism time, we must use this time to achieve the effect of sleep beauty.

6 answers2024-02-08

Define the struct:

typedef struct _legaladdress_{ >>>More

4 answers2024-02-08

Step by step, it is not good to wean suddenly, so that the child can not accept. If the child is more than 4 months old, you can gradually add complementary food, reduce the number of breastfeeding, if the original is 8 times a day of breastfeeding, it can be reduced to 7 times a week, add a complementary food, if the child adapts well, the next week can be reduced to 5 times of breastfeeding, and gradually wean off breast milk. It is best mixed with more nutritious milk powder, such as Shengyuan Yili Duomez.

16 answers2024-02-08

Around 6 to 7 months of age, babies may be able to start crawling soon after they are able to sit firmly on their own without support. Next, your baby will look up and look around. The muscles in his arms, legs, and back were strong enough to keep him from falling when he supported himself with his hands and knees. >>>More

16 answers2024-02-08

Medium-rare is the best! The egg yolk is hot and not solidified, and there is no fishy smell in the mouth! So it's the best to eat! >>>More