瓶颈的突破,豁然开朗!!!

哈哈,今天我在对接口的理解上有了个长足的进步!!! 
以前的理解,老是不得要领,找不到也抓不住这个概念的关键点,刚才看了一篇这个方面的文章眼前一片开朗!!!
 可不嘛,为了这个概念,我可是费了老大劲了,在这个费劲找材料的过程中,心里也不知不学中慢慢有了对学这个的怀疑,进而迁怒于别处把整个生活也弄得乱糟糟的,再加上现在又是刚从家放假回来,整个身心提不起来。
 现在好了,再一次地坚定了学编程的信心。:)
 下面是我前面提到的那篇文章:
 (源自于http://www.answers.com/topic/interface-java,这个answers网站可是不错呀,在此向大家推荐)
 interface (Java)
An interface in the Java programming language is an abstract type which is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are introduced with the interface keyword, and may only contain function signatures and constant declarations (variable declarations which are declared to be both static and final).

As interfaces are abstract, they cannot be instantiated. Object references in Java may be specified to be of interface type; in which case they must be bound to null, or an object which implements the interface.

The primary capability which interfaces have, and classes lack, is multiple inheritance. All classes in Java (other than java.lang.Object, the root class of the Java type system) must have exactly one base class (corresponding to the extends clause in the class definition; classes without an extends clause are defined to inherit from Object); multiple inheritance of classes is not allowed. However, Java classes may implement as many interfaces as the programmer desires (with the implements clause). A Java class which implements an interface, but which fails to implement all the methods specified in the interface, becomes an abstract base class, and must be declared abstract in the class definition.


Uses
Interfaces are used to collect like similarities which classes of various types share, but do not necessarily constitute a class relationship. For instance, a human and a parrot can both whistle, however it would not make sense to represent Humans and Parrots as subclasses of a Whistler class, rather they would most likely be subclasses of an Animal class (likely with intermediate classes), but would both implement the Whistler interface.

Another use of interfaces is being able to use an object without knowing its type of class, but rather only that it implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it is a human or a parrot, all that could be determined is that a whistler is whistling. In a more practical example, a sorting algorithm may expect an object of type Comparable. Thus, it knows that the object's type can somehow be sorted, but it is irrelevant what the type of the object is.

Finally, interfaces may be used in place of multiple inheritance of classes.


Usage

Defining an Interface
Interfaces must be defined using the following formula (compare to Java's class definition).

[visibility] interface Interface Name [extends other interfaces] {
        constant declarations
        abstract method declarations
}
The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required.

Thus, a simple interface may be

public interface Predator {
       public boolean chasePrey(Prey p);
       public void eatPrey(Prey p);
}

Implementing an Interface
The syntax for implementing an interface uses this formula:

... implements interface name[, another interface, another, ...] ...
Classes may implement an interface. For example,

public class Cat implements Predator {

        public boolean chasePrey(Prey p) {
               // programming to chase prey p
        }

        public void eatPrey (Prey p) {
               // programming to eat prey p
        }
}
If a class implements an interface and is not abstract, and does not implement a required interface, this will result in a compiler error. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods.

Classes can implement multiple interfaces

public class Frog implements Predator, Prey { ... }

Creating subinterfaces
Subinterfaces can be created as easily as interfaces, using the same formula are described above. For example

public interface VenomousPredator extends Predator, Venomous {
        interface body
}
is legal. Note how it allows multiple inheritance, unlike classes.


Examples
Some common Java interfaces are:

Comparable has the method compareTo, which is used to describe two objects as equal, or to indicate one is greater than the other. Generics allow implementing classes to specify which class instances can be compared to them.
Serializable is an interface with no methods or fields - it has an empty body. It is used to indicate that a class can be serialized. Its Javadoc describes how it should function, although nothing is programmatically enforced.

External links
What Is an Interface?
Interfaces and Packages


This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)
 

 在生活中,接口的一个例子就是,数学是一切理科的接口,别的学科不实现这个接口,在使用起来就不能充分发挥,接口定义了类与类之间非继承的一种关系,有了它类的使用更加方便,接口嘛就像USB一样,一个类是实体,它在某些场合下应用得用到一接口来衔接。

 啊,舒服!!!!

 下一步就是再熟练地使用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值