【问题标题】:Interface and extending接口和扩展
【发布时间】:2019-09-30 21:02:40
【问题描述】:

在如何使用以下代码或什至可能时遇到问题。 我是新来的。 我有以下内容:

variables.java

public interface variables extends library{
     public int func1(String a, String b);
     public int func2(String a, String b); 
}

CallVariables.java

public CallVariables extends variables{
    String hi = "Hi";
    String by = "Bye";
    //Then here somehow call my variables.java and be able to use it...
}

ma​​in.java

// Now, here I want to be able to actually call the first or either the second .java class.
// Is this possible? If yes, then how? 

我想知道这是否可能,如果可以,请提供示例。

【问题讨论】:

    标签: java interface extends


    【解决方案1】:

    通过指定关键字“interface”来声明接口。例如:

    interface MyInterface
    {
       /* All the methods are public abstract by default
        * As you see they have no body
        */
       public void method1();
       public void method2();
    }
    

    Java 接口示例

    这就是类实现接口的方式。它必须提供接口中声明的所有方法的主体,或者可以说类必须实现接口的所有方法。

    一个类实现了接口,但一个接口扩展了另一个接口。

    interface MyInterface
    {
       /* compiler will treat them as: 
        * public abstract void method1();
        * public abstract void method2();
        */
       public void method1();
       public void method2();
    }
    class Demo implements MyInterface
    {
       /* This class must have to implement both the abstract methods
        * else you will get compilation error
        */
       public void method1()
       {
        System.out.println("implementation of method1");
       }
       public void method2()
       {
        System.out.println("implementation of method2");
       }
       public static void main(String arg[])
       {
        MyInterface obj = new Demo();
        obj.method1();
       }
    }
    

    输出:

    implementation of method1

    我希望这可以更清楚地说明类如何与接口一起使用。

    【讨论】:

    • 非常感谢!谢谢!帮助我!
    【解决方案2】:

    请遵循接口和类的命名约定。 类可以实现接口,接口可以扩展另一个接口,类可以扩展一个类。 这可能会对您有所帮助:

    interface Library{}
    //First variables.java:
         interface Variables extends Library{
             public int func1(String a, String b);
             public int func2(String a, String b); 
        }
    
    //Second call Variables.java
         class CallVariables implements Variables{
             String hi = "Hi";
             String by = "Bye";
             //Then here somehow call my variables.java and be able to use it...
            @Override
            public int func1(String a, String b) {
                // TODO Auto-generated method stub
                System.out.println("func1 says :"+hi);
                return 1;
            }
            @Override
            public int func2(String a, String b) {
                // TODO Auto-generated method stub
                System.out.println("func2 says :"+by);
                return 2;
            }
         }
    //Third main.java
         //Now, here I want to be able to actually call the first or either the second .java class. 
        // Is this possible? If yes, then how? 
         public class Main{
            public static void main(String[] args) {
                Variables variables = new CallVariables();
                variables.func1("a", "b");
                variables.func2("c", "d");
            }
         }
    

    【讨论】:

    • 非常感谢!这很有帮助!
    • 很高兴它对您有所帮助。如果对您有帮助,请考虑投票,如果是解决方案,请接受作为答案。
    猜你喜欢
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2023-04-08
    • 2015-04-01
    • 1970-01-01
    相关资源
    最近更新 更多