As3.0 Interface and the use of classes

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_4d65c19e0100bfkb.html

abstract class: also known as abstract base class: Can contain all the attributes contained in a generic class, for example, fields, properties, methods, abstract classes cannot be instantiated he is mainly used in the definition and partial implementation of the class, So he needs to be fully expanded and implemented in the extension class. In addition, abstract classes contain a very special method, called abstract methods , which are basically functions that do not execute code, are overridden by classes that inherit from the class (override), and provide a concrete implementation. In Java and C #, there are abstract keywords to define abstraction classes, and in As3, there is no support for abstract classes , even though abstract is already a keyword, we can only implement the mechanism of abstract classes through simulation methods. In the example of AS3 Note (11), adapter mode (Adapter pattern),adaptee.as is an abstract class, and Requestb () is an abstract method.


Package{
PublicclassAdaptee {
PublicfunctionRequesta ():void{
Trace"Adaptee:requesta ()");
}
PublicfunctionRequestb () {

}
Public functionREQUESTC ():void{
Trace"ADAPTEE:REQUESTC ()");
}
}
}

Interface : an interface is a special abstract class, marked with the interface keyword, which can contain instance/static and Getter/setter methods, and any class that implements the interface must implement these methods as defined by the interface. the classes in AS3 can implement (implements) multiple interfaces. Compared to the class, the interface is more like a method of protocol, the use of interfaces can better manage the function of the module, easy to organize and reference. In the example of AS3 Note (11), adapter mode (Adapter pattern),itarget.as is a typical interface.


package  {
    public  interface itarget {
         function  renamedrequesta (): void ;
        function   Requesta (): void ;
        function   REQUESTB (): void ;
        function   REQUESTC (): void ;
        function   REQUESTD (): void ;
    }
}


As mentioned in Help, an interface is a data type that defines a method group, where the method must be defined by any class that implements the interface.

Interfaces are similar to classes, but have the following important differences:

    • The interface contains only the declaration of the method and does not contain its implementation. That is, each class that implements an interface must provide an implementation for each method declared in that interface.
    • An interface method definition cannot have any properties, such as public or private , but in the definition of the class that implements the interface, the implemented method must be marked as public .
    • Multiple interfaces can be extends inherited through a statement by an interface, or by a class through a implements statement.


From a programmatic point of view, abstract classes and interfaces can be used to implement the idea of "design by contract".
In the specific use, abstract class represents an inheritance relationship, in AS3, only support single inheritance does not support multiple inheritance . However, a class can implement multiple interfaces. This is the same as Java.

Secondly, in the definition of abstract class, we can give the method the default method. However, in the definition of an interface, the method cannot have the default behavior. It is not possible to define the default method, which is relatively cumbersome for post-code maintenance, such as the need to modify the default method for all classes that implement the interface, perhaps to modify each class, and if it is inherited, only the abstract class needs to be modified. Although we can bypass defining default behavior through delegates, there is still a bit of trouble.

From a programming point of view, the interface can largely compensate for the inability to use multiple inheritance problems. See
Figure 1

Figure 1 is a typical case of inheritance. For example, generate a bus driver instance class and a taxi driver instance class.

Figure 2


As there is no more inheritance, we are going to build a male bus driver and a female taxi driver that doesn't look so reliable. What then? generally in addition to the one of the classes as a function of composite into the last instance class, the more standard is to use the interface. See:
Figure 3

Figure 3 showsinstance classes that inherit from man and woman bus driver and taxi driver implement employment class functionality via the Dirver interface standard。 In general, the application of abstract class is usually tree structure, and the application of joining interface can make the structure richer and more reliable.
Summarize the differences between abstract classes and interfaces in the syntax and design principles

1. Abstract classes are abstractions of objects, which can be understood as classes as objects and abstract classes as abstract classes.
An interface is just a specification or a rule of behavior, and abstract classes are more defined in a series of tightly related classes, whereas interfaces are mostly classes that have loose relationships and that implement a function.
2. A class may implement several interfaces at a time, but only one parent class can be extended.
3. By default, all classes in ActionScript 3.0 are sealed, so abstract classes can also be sealed.
4. Interfaces are similar to non-abstract classes, and abstract classes must also provide their own implementations for all members of the interfaces listed in the base class list of the class. However, an abstract class is allowed to map an interface method to an abstract method.
5. Abstract classes implement a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable is implemented as subclasses.
6. A good interface definition should be functional, not multi-functional, or cause interface pollution. If a class just implements one of the functions of this interface, and has to implement other methods in the interface, it is called interface pollution.
7. Try to avoid using inheritance to implement the build function, but instead use black box multiplexing, which is the object combination. Because of the increasing level of inheritance, the most immediate consequence is that when you call a class in this group, you have to load them all into memory and consume resources.
8. If an abstract class implements an interface, it is possible to map the methods in an interface to an abstract class as an abstract method without having to implement the methods in the interface in the subclass of the abstract class.

Application Scenarios:
If you expect to create multiple versions of a component, create an abstract class. Abstract classes provide an easy way to control the component version.
If you create a feature that will be used across a wide range of total images, use an interface.
If you want to design a small and concise function block, use the interface.
If you are designing large functional units, use an abstract class.
To provide common implemented functionality across all implementations of a component, use an abstract class.

For an abstract class and interface on AS3, you can also look at several articles:
http://www.asv5.cn/blog/article.asp?id=164
Http://riaidea.com/article.asp?id=26
Http://space.actionscript3.cn/html/73/t-973.html

As3.0 Interface and the use of classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.