【问题标题】:Java: reusable encapsulation with interface, abstract class or inner classes?Java:接口、抽象类或内部类的可重用封装?
【发布时间】:2010-04-29 05:09:23
【问题描述】:

我尝试封装。接口例外,静态内部类工作,非静态内部类不工作,无法理解术语:嵌套类,内部类,嵌套接口,接口抽象类——听起来太重复了!

糟糕! --- 来自接口的异常“非法类型”显然是因为值是常量(?!)

    static interface userInfo
    {
            File startingFile=new File(".");
            String startingPath="dummy";
            try{
                    startingPath=startingFile.getCanonicalPath();
            }catch(Exception e){e.printStackTrace();}
    }

多种方法:接口、静态内部类图像 VS 非静态内部类图像

import java.io.*;
import java.util.*;

public class listTest{
        public interface hello{String word="hello word from Interface!";}

        public static class staticTest{
                staticTest(){}
                private String hejo="hello hallo from Static class with image";
                public void printHallooo(){System.out.println(hejo);}
        }
        public class nonStatic{
                nonStatic(){}
                public void printNonStatic(){System.out.println("Inside non-static class with an image!");}
        }
        public static class staticMethodtest{
                private static String test="if you see mee, you printed static-class-static-field!";
        }

        public static void main(String[] args){
                //INTERFACE TEST
                System.out.println(hello.word);
                //INNNER CLASS STATIC TEST
                staticTest h=new staticTest();
                h.printHallooo();
                //INNER CLASS NON-STATIC TEST
                nonStatic ns=(new listTest()).new nonStatic();
                ns.printNonStatic();
                //INNER CLASS STATIC-CLASS STATIC FIELD TEST
                System.out.println(staticMethodtest.test);
        }
}

输出

hello word from Interface!
hello hallo from Static class with image
Inside non-static class with an image!
if you see mee, you printed static-class-static-field!

相关

【问题讨论】:

  • @ggf31416,静态接口有效。见stackoverflow.com/questions/71625/…
  • 你应该让你的后续问题成为一个新问题,因为它会让这里的答案变得混乱。
  • 非静态内部类的正确语法是:new listTest().new nonStatic();。由于它不是静态的,它必须引用外部类的现有实例。

标签: java interface abstract-class nested-class reusability


【解决方案1】:

问题是您在方法之外编写代码。为此,您确实需要一个类,并且必须将代码放在方法中。例如:

static class UserInfo
{
    public static void myMethod()
    {
        File startingFile = new File(".");
        String startingPath = "dummy";
        try
        {
            startingPath = startingFile.getCanonicalPath();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

这确实假设 java.io.File 已导入。

然后你可以调用 UserInfo.myMethod();

您可能还想导入 java.util.IOException 并捕获 IOException 而不是一般的异常。

此外,根据 Java 约定,类和接口以大写字母开头。

编辑:描述您最近对您的问题的评论:

当您想强制相似的类(想想不同类型的 DVD 播放器)具有相同的基本功能(播放 dvd、停止、暂停)时,请使用接口。类似地使用抽象类,但当所有类都将实现一些相同的事情以相同的方式。

【讨论】:

    【解决方案2】:

    我想你想这样做:

    static class userInfo
        {
             public static void something() {
                File startingFile=new File(".");
                String startingPath="dummy";
                try{
                        startingPath=startingFile.getCanonicalPath();
                }catch(Exception e){e.printStackTrace();}
             }
        }
    

    【讨论】:

      【解决方案3】:

      您不能将代码放入接口中,接口仅描述对象的行为方式。即使你使用类,你也应该把这种代码放在一个方法中,而不是直接放在类体中。

      【讨论】:

      • s/should/must/s/method/method, constructor or initializer block/
      【解决方案4】:

      接口中不能有实际代码,只有方法签名和常量。你想做什么?

      【讨论】:

      • 不要吹毛求疵,但实际上可以。您可以在接口中实例化一个内部类并在那里执行方法。
      【解决方案5】:

      看起来你想在这里写一个class

      【讨论】:

        【解决方案6】:

        接口中不能有代码。只是方法签名。 顶级接口不能是静态的。

        我建议你开始你的Java学习here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-21
          • 2011-12-05
          • 2011-07-02
          • 1970-01-01
          • 2015-05-27
          • 2012-11-06
          相关资源
          最近更新 更多