英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc

上传人:scccc 文档编号:11132179 上传时间:2021-07-04 格式:DOC 页数:16 大小:254.04KB
返回 下载 相关 举报
英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc_第1页
第1页 / 共16页
英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc_第2页
第2页 / 共16页
英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc_第3页
第3页 / 共16页
英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc_第4页
第4页 / 共16页
英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc》由会员分享,可在线阅读,更多相关《英文翻译-基于mvc的跑步社区管理系统的设计与实现.doc(16页珍藏版)》请在三一文库上搜索。

1、1外文资料翻译译文接口接口和抽象类提供了更加结构化的方式来实现接口分离。这样的机制在编程语言中并不常见。例如C +,只有对这些概念的间接支持。在Java中存在语言的关键字,这一事实表明,这些想法是重要的,足以提供直接的支持。首先,我们来看看抽象类,这是一种普通的类和接口之间的中间步骤。虽然第一步是创建一个接口,抽象类是用于构建有一些未实现的方法类的一个重要并且必要的工具。你不能总是用纯接口。抽象类和方法在前面的章节中所有的“乐器”的例子,在基类仪器的方法总是“虚拟”的方法。如果这些方法都曾经叫,你做错了什么。这是因为仪器的目的是创建一个通用接口,所有从它派生的类。在这些实施例中,唯一的原因来建

2、立该共同界面是使得它可以为每个不同的亚型表达不同。它建立了一个基本形式,这样就可以说什么是适用于所有的派生类。的另一种说法是调用仪器的抽象基类,或者只是一个抽象类。如果你有一个抽象类像仪,特定类的对象几乎都是没有任何意义。当你想通过它的通用接口来操纵一组类,你创建一个抽象类。因此,仪器是为了只表达接口,而不是一个特定的实现,因此创建一个仪表对象是没有意义的,你可能会想阻止用户这样做。这可以通过使在仪器的所有方法产生错误来完成,但是这延迟的信息,直到运行时间并需要对用户的部分可靠详尽的测试。通常最好赶在编译时的问题。Java提供一种机制,用于执行此操作称为抽象method.1这是一种方法,该方法

3、是不完全的;它只有一个声明,没有方法体。下面是语法的抽象方法声明。abstract void f( ); 包含抽象方法的类叫做抽象类。如果一个类包含一个或多个抽象方法,类本身必须是合格的抽象。 (否则,编译器给你一个错误信息。)如果一个抽象类是不完整的,什么是应该做的编译器,当有人试图使这一类的一个对象?它不能安全地创建一个抽象类的对象,所以你从编译器的错误信息。这样一来,编译器保证了抽象类的纯度,而且你不必担心误用。如果从一个抽象类继承,你想使新类型的对象,你必须提供方法定义为在基类中的所有抽象方法。如果不这样做(你可以选择不),则派生类也是抽象的,而且编译器会强迫你限定该类与抽象的关键字。

4、这有可能使一个类抽象而不包括任何抽象方法。当你有一个类中,它没有任何意义有任何抽象方法,但要防止任何类实例,这非常有用。从前面的章节中的仪器类可以很容易地变成一个抽象类。只有一些方法将是抽象的,因为使一个类摘要不强迫你做的所有方法抽象。这里是什么样子:这是乐团示例修改为使用抽象类和方法:这是乐团示例修改为使用抽象类和方法:/: interfaces/music4/Music4.java / Abstract classes and methods. package interfaces.music4; import polymorphism.music.Note; import static

5、net.mindview.util.Print.*; abstract class Instrument private int i; public abstract void play(Note n); public String what() return Instrument; public abstract void adjust(); class Wind extends Instrument public void play(Note n) print(Wind.play() + n); public String what() return Wind; public void a

6、djust() class Percussion extends Instrument public void play(Note n) print(Percussion.play() + n); public String what() return Percussion; public void adjust() class Stringed extends Instrument public void play(Note n) print(Stringed.play() + n); public String what() return Stringed; public void adj

7、ust() class Brass extends Wind public void play(Note n) print(Brass.play() + n); public void adjust() print(Brass.adjust(); class Woodwind extends Wind public void play(Note n) print(Woodwind.play() + n); public String what() return Woodwind; public class Music4 / Doesnt care about type, so new type

8、s / added to the system still work right: static void tune(Instrument i) / . i.play(Note.MIDDLE_C); static void tuneAll(Instrument e) for(Instrument i : e) tune(i); public static void main(String args) / Upcasting during addition to the array: Instrument orchestra = new Wind(), new Percussion(), new

9、 Stringed(), new Brass(), new Woodwind() ; /* Output: Wind.play() MIDDLE_C Percussion.play() MIDDLE_C Stringed.play() MIDDLE_C Brass.play() MIDDLE_C Woodwind.play() MIDDLE_C */: 你可以看到,除了基类其它没有改变。创建抽象类和方法是很有帮助的,因为他们的抽象性类显式,并告诉用户和编译器它是如何被使用。抽象类重构通行费也有用,因为它们允许您轻松地将常用的方法继承层次结构。接口接口关键字采用抽象的概念又进了一步。抽象的关键字

10、,您可以创建一个或多个未定义的方法在一个类中,您所提供的接口的一部分而不提供相应的实现。实现由继承者提供。接口关键字产生一个完全抽象类,一个提供没有实现的。它允许创建者确定方法名称,参数列表和返回类型,但没有方法体。一个接口提供仅一种形式,但没有实现。接口说:“实现这个特定的接口中的所有类都将是这样。”因此,使用特定接口的任何代码知道什么方法可以被称为该接口,而这一切。所以界面用于建立类之间的“协议”。 (一些面向对象的编程语言有一个名为协议做同样的事情关键字。)然而,接口是多到了极致只是一个抽象类,因为它可以让你通过创建一个类,可以向上转型到多个基类来执行“多重继承”的变化。要创建一个接口,

11、使用该接口的关键字,而不是class关键字。作为一类,可以在接口关键字前加上public关键字(但只有当该接口在同一个名称的文件中定义)。如果你离开过公众的关键字,你会得到包访问,所以接口是唯一在同一封装内使用。接口也可以包含字段,但这些都是隐式静态和最后。为了使一类符合特定接口(或一组接口),使用implements关键字,它说:“该接口是什么样子的,但现在我会说,它是如何工作的。”除此之外,它看起来像继承。该图的仪器例子显示了这一点:木管乐器和铜管的类中可以看到,一旦你实现了一个接口,实现成为一个普通的类,它可以扩展的常规方法。你可以选择显式地声明一个接口中的方法公开,但他们是公开的,即使

12、你不会说。当你实现一个接口,接口必须被定义为公共的方法。否则,他们会默认包访问,你会减少在继承方法的可访问性,这是由Java编译器不允许。你可以看到这个修改版本的工具的例子。注意,接口中的每个方法是严格意义上的声明,编译器允许唯一。此外,没有一个方法声明为公共工具,但它们自动公共无论如何:/: interfaces/music5/Music5.java / Interfaces. package interfaces.music5; import polymorphism.music.Note; import static net.mindview.util.Print.*; interfac

13、e Instrument / Compile-time constant: int VALUE = 5; / static & final / Cannot have method definitions: void play(Note n); / Automatically public void adjust(); class Wind implements Instrument public void play(Note n) print(this + .play() + n); public String toString() return Wind; public void adju

14、st() print(this + .adjust(); class Percussion implements Instrument public void play(Note n) print(this + .play() + n); public String toString() return Percussion; public void adjust() print(this + .adjust(); class Stringed implements Instrument public void play(Note n) print(this + .play() + n); pu

15、blic String toString() return Stringed; public void adjust() print(this + .adjust(); class Brass extends Wind public String toString() return Brass; class Woodwind extends Wind public String toString() return Woodwind; public class Music5 / Doesnt care about type, so new types / added to the system

16、still work right: static void tune(Instrument i) / . i.play(Note.MIDDLE_C); static void tuneAll(Instrument e) for(Instrument i : e) tune(i); public static void main(String args) / Upcasting during addition to the array: Instrument orchestra = new Wind(), new Percussion(), new Stringed(), new Woodwin

17、d() ; tuneAll(orchestra); /* Output: Wind.play() MIDDLE_C Percussion.play() MIDDLE_C Stringed.play() MIDDLE_C Brass.play() MIDDLE_C Woodwind.play() MIDDLE_C */:另外一个变化已经取得了这个版本的例子:在 what( )方法已更改为的toString(),因为这是怎样的方法被使用。自的toString()是根类对象的一部分,它不需要出现在界面。代码的其余部分的工作原理相同。请注意,这并不重要,如果你是向上转型到“常规”类叫做仪器,一个抽象类

18、仪器,或称为仪器的接口关系。的行为是相同的。事实上,你可以调tune( )方法中看到,是不是仪器是否是一个“普通”类,抽象类或接口的任何证据。2.外文原文InterfacesInterfaces and abstract classes provide more structured way to separate interface from implementation. Such mechanisms are not that common in programming languages. C+, for example, only has indirect support for t

19、hese concepts. The fact that language keywords exist in Java indicates that these ideas were considered important enough to provide direct support. First, well look at the abstract class, which is a kind of midway step between an ordinary class and an interface. Although your first impulse will be t

20、o create an interface, the abstract class is an important and necessary tool for building classes that have some unimplemented methods. You cant always use a pure interface. Abstract classes and methods In all the “instrument” examples in the previous chapter, the methods in the base class Instrumen

21、t were always “dummy” methods. If these methods are ever called, youve done something wrong. Thats because the intent of Instrument is to create a common interface for all the classes derived from it. In those examples, the only reason to establish this common interface is so that it can be expresse

22、d differently for each different subtype. It establishes a basic form, so that you can say whats common for all the derived classes. Another way of saying this is to call Instrument an abstract base class, or simply an abstract class. If you have an abstract class like Instrument, objects of that sp

23、ecific class almost always have no meaning. You create an abstract class when you want to manipulate a set of classes through its common interface. Thus, Instrument is meant to express only the interface, and not a particular implementation, so creating an Instrument object makes no sense, and youll

24、 probably want to prevent the user from doing it. This can be accomplished by making all methods in Instrument generate errors, but that delays the information until run time and requires reliable exhaustive testing on the users part. Its usually better to catch problems at compile time. Java provid

25、es a mechanism for doing this called the abstract method.1 This is a method that is incomplete; it has only a declaration and no method body. Here is the syntax for an abstract method declaration. abstract void f( ); A class containing abstract methods is called an abstract class. If a class contain

26、s one or more abstract methods, the class itself must be qualified as abstract. (Otherwise, the compiler gives you an error message.) If an abstract class is incomplete, what is the compiler supposed to do when someone tries to make an object of that class? It cannot safely create an object of an ab

27、stract class, so you get an error message from the compiler. This way, the compiler ensures the purity of the abstract class, and you dont need to worry about misusing it. If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all

28、the abstract methods in the base class. If you dont (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword. Its possible to make a class abstract without including any abstract methods. This is useful when

29、 youve got a class in which it doesnt make sense to have any abstract methods, and yet you want to prevent any instances of that class. The Instrument class from the previous chapter can easily be turned into an abstract class. Only some of the methods will be abstract, since making a class abstract

30、 doesnt force you to make all the methods abstract. Heres what it looks like: Heres the orchestra example modified to use abstract classes and methods: /: interfaces/music4/Music4.java / Abstract classes and methods. package interfaces.music4; import polymorphism.music.Note; import static net.mindvi

31、ew.util.Print.*; abstract class Instrument private int i; / Storage allocated for each public abstract void play(Note n); public String what() return Instrument; public abstract void adjust(); class Wind extends Instrument public void play(Note n) print(Wind.play() + n); public String what() return

32、Wind; public void adjust() class Percussion extends Instrument public void play(Note n) print(Percussion.play() + n); public String what() return Percussion; public void adjust() class Stringed extends Instrument public void play(Note n) print(Stringed.play() + n); public String what() return String

33、ed; public void adjust() class Brass extends Wind public void play(Note n) print(Brass.play() + n); public void adjust() print(Brass.adjust(); class Woodwind extends Wind public void play(Note n) print(Woodwind.play() + n); public String what() return Woodwind; public class Music4 / Doesnt care abou

34、t type, so new types / added to the system still work right: static void tune(Instrument i) / . i.play(Note.MIDDLE_C); static void tuneAll(Instrument e) for(Instrument i : e) tune(i); public static void main(String args) / Upcasting during addition to the array: Instrument orchestra = new Wind(), ne

35、w Percussion(), new Stringed(), new Brass(), new Woodwind() ; /* Output: Wind.play() MIDDLE_C Percussion.play() MIDDLE_C Stringed.play() MIDDLE_C Brass.play() MIDDLE_C Woodwind.play() MIDDLE_C */: You can see that theres really no change except in the base class. Its helpful to create abstract class

36、es and methods because they make the abstractness of a class explicit, and tell both the user and the compiler how it was intended to be used. Abstract classes are also useful refactoring tolls, since they allow you to easily move common methods up the inheritance hierarchy. Interfaces The interface

37、 keyword takes the concept of abstractness one step further. The abstract keyword allows you to create one or more undefined methods in a classyou provide part of the interface without providing a corresponding implementation. The implementation is provided by inheritors. The interface keyword produ

38、ces a completely abstract class, one that provides no implementation at all. It allows the creator to determine method names, argument lists, and return types, but no method bodies. An interface provides only a form, but no implementation. An interface says, All classes that implement this particula

39、r interface will look like this. Thus, any code that uses a particular interface knows what methods might be called for that interface, 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 th

40、e same thing.) However, an interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation of multiple inheritance by creating a class that can be upcast to more than one base type. To create an interface, use the interface keyword instead of the class

41、keyword. As with a class, you can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name). If you leave off the public keyword, you get package access, so the interface is only usable within the same package. An interface can also contai

42、n fields, but these are implicitly static and final. To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword, 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 di

43、agram for the instrument example shows this: You can see from the Woodwind and Brass classes that once youve implemented an interface, that implementation becomes an ordinary class that can be extended in the regular way. You can choose to explicitly declare the methods in an interface as public, bu

44、t 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. Otherwise, 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.

45、You can see this in the modified version of the Instrument example. Note that every method in the interface is strictly a declaration, which is the only thing the compiler allows. In addition, none of the methods in Instrument are declared as public, but theyre automatically public anyway: /: interf

46、aces/music5/Music5.java / Interfaces. package interfaces.music5; import polymorphism.music.Note; import static net.mindview.util.Print.*; interface Instrument / Compile-time constant: int VALUE = 5; / static & final / Cannot have method definitions: void play(Note n); / Automatically public void adjust(); class Wind implements Instrument public void play(Note n) print(this + .play() + n); public String toString() return Wind; pub

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1