建议多看书先
thinking in java

解决方案 »

  1.   

    就是可以这样操作了:interface I{}class C implements I{}class T{
    I i = new C();
    }
      

  2.   

    interface Classjava
    {
    void method();
    }
    public class Test implements Classjava
    {
    public void method()
    {
    System.out.println("接口的实现");
    }
    public static void main(String[] args)
    {
     new Test().method();
    }
    }
      

  3.   

    补充一点:
    如果想实现(implement)接口(interface)一定要完全实现interface中的所有方法,如果不能完全实现接口所有方法的类,我们称之为 abstract class(抽象类).共同进步!~
      

  4.   

    What Is an Interface? 
    This section shows you how to create and to use interfaces and talks about why you would use an interface instead of a class. 
    An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. An interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior. --------------------------------------------------------------------------------
    Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants. 
    --------------------------------------------------------------------------------
    Because an interface is simply a list of unimplemented, and therefore abstract, methods, you might wonder how an interface differs from an abstract class. The differences are significant. 
    An interface cannot implement any methods, whereas an abstract class can. 
    A class can implement many interfaces but can have only one superclass. 
    An interface is not part of the class hierarchy. Unrelated classes can implement the same interface. 
    Let's set up the example we'll be using in this section. Suppose that you have written a class that can watch stock prices coming over a data feed. This class allows other classes to register to be notified when the value of a particular stock changes. First, your class, which we'll call StockMonitor, would implement a method that lets other objects register for notification:
    public class StockMonitor {
         public void watchStock(StockWatcher watcher,
         String tickerSymbol, double delta) {
         ...
         }
    }The first argument to this method is a StockWatcher object. StockWatcher is the name of an interface whose code you will see in the next section. That interface declares one method: valueChanged. An object that wants to be notified of stock changes must be an instance of a class that implements this interface and thus implements the valueChanged method. The other two arguments provide the symbol of the stock to watch and the amount of change that the watcher considers interesting enough to be notified of. When the StockMonitor class detects an interesting change, it calls the valueChanged method of the watcher. The watchStock method ensures, through the data type of its first argument, that all registered objects implement the valueChanged method. It makes sense to use an interface data type here because it matters only that registrants implement a particular method. If StockMonitor had used a class name as the data type, that would artificially force a class relationship on its users. Because a class can have only one superclass, it would also limit what type of objects can use this service. By using an interface, the registered objects class could be anything--Applet or Thread--for instance, thus allowing any class anywhere in the class hierarchy to use this service.
      

  5.   

    interface 纯粹是设计的一种表达方式,而类则是设计和实现的混合体。
    ——Gosling
      

  6.   

    接口用来描述类的功能,而不指明具体的实现方法。一个类可以实现一个或多个接口。
    只要类实现了接口,就可以在任何需要该接口的地方使用这个类的对象。
    在Java中,接口不是类,而是一组对类的要求,这些类要与接口一致。
      

  7.   

    如果把类中的方法设为public,不是跟接口一样了么?还要接口干什么?请解释!谢谢!
      

  8.   

    To 楼上的:不是说了吗?接口是一系列功能的抽象定义,而类中的方法是要有实现的。个人认为,接口是一种更加全局化的设计,它使得所有实现了它的类都具有类似的行为,而且还可以在需要的时候使用“UpCast”将它们相同对待。
      

  9.   

    哦 原来是这样!但我感觉,每个类都可以implement接口,是不是很混乱阿!
      

  10.   

    public interface Actionlistener(){     public abstract void actionPerformed(ActionEvent event);     }   只有实现了这个接口(重写这个接口中的唯一一个方法),你才有资格去事件监听器列表里注册(参数为Actionlistener类型),当事件源变动时,自动调用这个唯一的actionPerformed方法.
      

  11.   

    给我100分,给你1000字的解释。呵呵哈,不是一句话的问题。简单的说,理解interface 要站在“设计、 OO design”的角度去考虑,不要考虑功能实现的问题。
      

  12.   

    没有接口,你实现个command都麻烦.
      

  13.   

    我举个例子,欢迎大家批评指正! (不止1000字啊,楼主要给我高分哦!!!)假设我们要为篮球设计一个类Basketball,由于篮球是球形物体,可以从类Ball继承而来,类Ball定义了一般球形物体的特性,如半径,滚动等。//Ball.javapublic class Ball {    protected long radius;    public Ball (long radius) {
            this.radius = radius;
        }    public void roll (long distance) {
            System.out.println("Rolling for " + distance);
        }}
    //BasketBall.javapublic class BasketBall extends Ball {    public BasketBall (long radius) {
            super(radius);
        }}因为只要是球形物体就可以滚动,篮球继承了球形物体的所有特性,当然也可以。但是和其他球形物体(如地球)不同,篮球是可以拿来玩的。我们会想到在类Basketball里面来实现这部分功能,修改Basketball为://BasketBall.javapublic class BasketBall extends Ball {    public BasketBall (long radius) {
            super(radius);
        }    public String play () {
            return "Interesting...";
        }}一个学生可以玩篮球,但是他/她当然不希望自己只能玩篮球,而不能玩其它的东西。如果把所有可以玩的东西都罗列一遍,那将是愚蠢的,能玩的东西太多了://Student.javapublic class Student {    public void play(Basketball nikes) { }    //public void play(Football nikes) { }
        //public void play(Tabletennis nikes) { }
        //There will be MUCH more...}但是,如果把"可以拿来玩"定义为一项抽象功能(即接口),并且让任何对象选择性的去实现它的话,我们就可以确定哪些对象是可以玩的,而哪些不是了。//Playable.javapublic interface Playable {    public String play();}下面用接口的方法重新实现篮球://Basketball.javapublic class BasketBall extends Ball implements Playable {    public BasketBall (long radius) {
            super(radius);
        }    public String play () {
            return "Interesting...";
        }}同样的方法可以实现任何可以玩的物体,不管它是不是球类://Football.javapublic class FootBall extends Ball implements Playable {    public BasketBall (long radius) {
            super(radius);
        }    public String play () {
            return "Not so interesting...";
        }}//Computer.javapublic class Computer implements Playable {    public Computer () { }    public String play () {
            return "Very interesting...";
        }}这样一个学生就可以是://Student.javapublic class Student {    public void play(Playable aThing) { }}JAVA规定,实现一个接口的类必须实现接口里的所有方法,因为如果你不去实现,就没有implements那个接口的必要,这是显而易见的。上面的例子也同样可以解释为什么接口方法一定是抽象的,因为不同对象有不同的实现细节,比如同样是可以玩的物体,有些很有趣,而有些就不是那么有趣。怎么样?用了接口以后,程序是不是变得合理多了?
      

  14.   

    Dan1980(( 努力泡分中)) :
    和我抢分,呵呵。
    基本正确。"JAVA规定,实现一个接口的类必须实现接口里的所有方法,因为如果你不去实现,就没有implements那个接口的必要,这是显而易见的。"不太正确。实现一个接口的类可以成为抽象类。
      

  15.   

    To yqj2065(严千钧):谢谢!我不是抢分啊!我就是想把我的理解说出来让大家指点指点,自我提高一下。我也很菜的。你说的对,“实现一个接口的类可以成为抽象类”,这是一种整体设计的考虑。