Java:可重用的接口、抽象类或内部类封装?

发布于 2024-08-30 12:27:14 字数 2111 浏览 4 评论 0原文

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

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

    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);
        }
}

< strong>输出

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!

相关

I try to encapsulate. Exeption from interface, static inner class working, non-static inner class not working, cannot understand terminology: nested classes, inner classes, nested interfaces, interface-abstract-class -- sounds too Repetitive!

BAD! --- Exception 'illegal type' from interface apparently because values being constants(?!)

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

MANY WAYS TO DO IT: Interface, static inner class image VS non-static innner class image

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);
        }
}

OUTPUT

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!

Related

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

国粹 2024-09-06 12:27:15

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

我建议您从这里开始学习 Java。

You cannot have code in interfaces. Just method signatures.
Top level interfaces cannot be static.

I suggest you start your learning of Java here.

一袭水袖舞倾城 2024-09-06 12:27:14

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

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、停止、暂停)时,请使用接口。您可以类似地使用抽象类,但是当所有的类都会以相同的方式实现一些相同的事情时。

The problem is that you're writing code outside of a method. You do need a class for this and you must put your code inside a method. For example:

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

This does assume that java.io.File was imported.

You can then call UserInfo.myMethod();

You might also want to import java.util.IOException and catch an IOException instead of a general Exception.

Also, classes and interfaces start with a capital letter by Java conventions.

EDIT: To describe your recent comment on your question:

Use an interface when you want to force similar classes (Think different types of DVD players) to have the same basic functionality (playing dvds, stopping, pausing. You use an abstract class similarly, but when all of the classes will implement some of the same things the same way.

幸福还没到 2024-09-06 12:27:14

我想你想这样做:

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

I think you wanted to do this:

static class userInfo
    {
         public static void something() {
            File startingFile=new File(".");
            String startingPath="dummy";
            try{
                    startingPath=startingFile.getCanonicalPath();
            }catch(Exception e){e.printStackTrace();}
         }
    }
谈场末日恋爱 2024-09-06 12:27:14

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

you cant put code in an interface, an interface only describes how an object will behave. Even when you use Classes, you should put this kind of code in a method, and not directly in the class body.

ㄟ。诗瑗 2024-09-06 12:27:14

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

You can't have actual code in an interface, only method signatures and constants. What are you trying to do?

楠木可依 2024-09-06 12:27:14

看起来您想在这里编写一个 class

Looks like you want to write a class here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文