Interfaces介绍外文翻译

上传人:lizhe****0001 文档编号:31225684 上传时间:2018-02-06 格式:DOC 页数:11 大小:95KB
返回 下载 相关 举报
Interfaces介绍外文翻译_第1页
第1页 / 共11页
Interfaces介绍外文翻译_第2页
第2页 / 共11页
Interfaces介绍外文翻译_第3页
第3页 / 共11页
Interfaces介绍外文翻译_第4页
第4页 / 共11页
Interfaces介绍外文翻译_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《Interfaces介绍外文翻译》由会员分享,可在线阅读,更多相关《Interfaces介绍外文翻译(11页珍藏版)》请在金锄头文库上搜索。

1、1外文原文InterfacesThe interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but

2、 these are implicitly static and final. An interface provides only a form, but no implementation. An interface says, “This is what all classes that implement this particular interface will look like.” Thus, any code that uses a particular interface knows what methods might be called for that interfa

3、ce, and thats all. So the interface is used to establish a “protocol” between classes. (Some object-oriented programming languages have a keyword called protocol to do the same thing.)To create an interface, use the interface keyword instead of the class keyword. Like a class, you can add the public

4、 keyword before the interface keyword (but only if that interface is defined in a file of the same name) or leave it off to give package access, so that it is only usable within the same package. To make a class that conforms to a particular interface (or group of interfaces), use the implements key

5、word, which says, “The interface is what it looks like, but now Im going to say how it works.” Other than that, it looks like inheritance. The diagram for the instrument example shows this:图 1You can see from the Woodwind and Brass classes that once youve implemented an interface, that implementatio

6、n becomes an ordinary class that can be extended in the regular way. 2You can choose to explicitly declare the method declarations in an interface as public, but they are public even if you dont say it. So when you implement an interface, the methods from the interface must be defined as public. Oth

7、erwise, they would default to package access, and youd be reducing the accessibility of a method during inheritance, which is not allowed by the Java compiler. You can see this in the modified version of the Instrument example. Note that every method in the interface is strictly a declaration, which

8、 is the only thing the compiler allows. In addition, none of the methods in Instrument are declared as public, but theyre automatically public anyway.The rest of the code works the same. It doesnt matter if you are upcasting to a “regular” class called Instrument, an abstract class called Instrument

9、, or to an interface called Instrument. The behavior is the same. In fact, you can see in the tune( ) method that there isnt any evidence about whether Instrument is a “regular” class, an abstract class, or an interface. This is the intent: Each approach gives the programmer different control over t

10、he way objects are created and used. “Multiple inheritance” in JavaThe interface isnt simply a “more pure” form of abstract class. It has a higher purpose than that. Because an interface has no implementation at allthat is, there is no storage associated with an interfacetheres nothing to prevent ma

11、ny interfaces from being combined. This is valuable because there are times when you need to say “An x is an a and a b and a c.” In C+, this act of combining multiple class interfaces is called multiple inheritance, and it carries some rather sticky baggage because each class can have an implementat

12、ion. In Java, you can perform the same act, but only one of the classes can have an implementation, so the problems seen in C+ do not occur with Java when combining multiple interfaces:图 2In a derived class, you arent forced to have a base class that is either an abstract or “concrete” (one with no

13、abstract methods). If you do inherit from a non-interface, you can 3inherit from only one. All the rest of the base elements must be interfaces. You place all the interface names after the implements keyword and separate them with commas. You can have as many interfaces as you want; each one becomes

14、 an independent type that you can upcast to. You can see that Hero combines the concrete class ActionCharacter with the interfaces CanFight, CanSwim, and CanFly. When you combine a concrete class with interfaces this way, the concrete class must come first, then the interfaces. (The compiler gives a

15、n error otherwise.) Note that the signature for fight( ) is the same in the interface CanFight and the class ActionCharacter, and that fight( ) is not provided with a definition in Hero. The rule for an interface is that you can inherit from it (as you will see shortly), but then youve got another i

16、nterface. If you want to create an object of the new type, it must be a class with all definitions provided. Even though Hero does not explicitly provide a definition for fight( ), the definition comes along with ActionCharacter, so it is automatically provided and its possible to create objects of Hero. In class Adventure, you can see that there are four methods that take as arguments the various interfaces and the concrete class. When a Hero object is created, it can

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 毕业论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号