Java OOP基础知识 [英] Java OOP basics

查看:129
本文介绍了Java OOP基础知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题让我无法继续前进,这个错误在我看来根本不符合逻辑,我从一本书中学习,代码来自那里。
这是代码:

I have a problem that keeps stalling me from advancing further, this error is not logical at all in my opinion , I am learning from a book and the code is from there. This is the code :

package test_area;

public class clzzz {

    class SimpleCircle{
        double radius;

        SimpleCircle()
        {
            radius = 1;
        }

        SimpleCircle(double newRadius)
        {
            radius = newRadius;
        }

        double getArea()
        {
            return radius*radius*Math.PI;
        }

        double getPerimeter()
        {
            return 2*radius*Math.PI;
        }

        void setRadius(double newRadius)
        {
            radius = newRadius;
        }

    }


    public static void main(String[] args)
    {
        SimpleCircle circle1 = new SimpleCircle();

    }



}

这是错误

This is the error

如果我从void main中消除静态错误消失了,但这样做我正在改变主方法的签名.....我真的很困惑,遵循书中的代码来自单词。

If I eliminate the static from void main the error vanishes, but doing that I am altering the signature of the main method..... I am really confuse, followed the code from the book word by word.

为什么以上帝的名义我需要静态标签?我不需要让相应的类只有一个实例,因为我可以通过对象的名称控制它的实例,因此静态只是一个障碍?

Why in the name of God do I need the static tag ? I don't need to oblige the respective class to have only one instance since I can control it's instances by the names of the objects thus static is just a barrier ?

推荐答案

你将SimpleCircle定义为一个内部类,这是一个不必要的复杂问题,它正在阻止编译。从clzzz声明中移出SimpleCircle类声明,你将解决问题。

You defined SimpleCircle as an inner class, that is an unnecessary complication here and it is what is keeping this from compiling. Move the SimpleCircle class declaration out from inside the declaration of clzzz and you'll fix the problem.

或者你可以通过添加static关键字使SimpleCircle成为静态内部类。如果你把它保存为静态内部类,那么,如果你可以在clzzz之外使用它,你需要使用外部类和内部类(clzzz.SimpleCircle)来引用它,以便JVM可以找到它。

Alternatively you could make SimpleCircle a static inner class by adding the static keyword. If you keep this as a static inner class then, if you can use it outside of clzzz, you'll need to refer to it using the outer class as well as the inner class (clzzz.SimpleCircle) so that the JVM can find it.

通常,静态内部类用于组织目的,因为你有一些与另一个类一起使用的东西,但它不依赖于它(参见 java.util.Map.Entry 一个例子,虽然它是一个接口而不是一个类。)

Typically static inner classes are used for organizational purposes, because you have something you use along with another class but it doesn't depend on it (see java.util.Map.Entry for an example, although it's an interface and not a class).

static 并不意味着你只能有一个实例。在类定义的上下文中,它意味着不依赖于外部类的实例。您仍然可以使用静态内部类创建多个实例(再次考虑Map和Map.Entry,您可以在其中迭代地图的所有条目,每个条目都是Map.Entry的单独实例)。您可以将 static 视为含义,您不需要从定义此事物的类实例化对象。

static doesn't mean you can have only one instance. In the context of a class definition it means that there is no dependency on an instance of the outer class. You still can create multiple instances using the static inner class (again, think of Map and Map.Entry, where you can iterate through all the entries of the map, each one is a separate instance of Map.Entry). You can think of static as meaning, "you don't need an object instantiated from the class where this thing is defined."

使某些东西成为非静态内部类意味着内部类的对象正在访问外部类实例范围内的事物,因此如果没有引用实例,则无法创建内部类的实例外部类,这就是编译器所抱怨的。

Making something a non-static inner class means objects of the inner class are accessing things in the scope of the outer class' instance, so you can't create an instance of the inner class without a reference an instance of the outer class, and that's what the compiler is complaining about.

这篇关于Java OOP基础知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆