简体   繁体   English

Java扩展类一团糟

[英]Java extending classes mess

I don't know what pot I was smoking when I posted the original, but I came to my senses and came up with this.我不知道我在发布原版时抽的是什么锅,但我清醒过来并想出了这个。 I am not an experienced coder, but the entire post was made mostly as a question that for the most part has been answered.我不是一个有经验的编码员,但整篇文章主要是作为一个大部分已经回答的问题。 I now know classes can't have code directly in them, and more so about the core structure.我现在知道类不能直接在其中包含代码,核心结构更是如此。

class Shape {类形状{

    public static void ShapeAttemptTwo()
    {
        class Circle extends Shape
            {
                public static CircleAttemptTwo()
                {
                int pi = 3.14;
                int r=4;
                }
            }
        class Rectangle extends Shape
        {
                public static Rectangle()
                {
                int l = 14;
                int b = 10;
                int z = l*b;
                }
        }
        class Square extends Shape
        {
            public static Square()
            {
            int a = 11;
            System.out.println(a * a);
            }
        }

// Java code 7, invalid method declare, return type required. // Java 代码 7,无效方法声明,需要返回类型。 (public static //CircleAttemptTwo()) // I'm lost on this one, could I have some help? (public static //CircleAttemptTwo()) // 我对这个迷路了,我可以帮忙吗? // and reached end of file while parsing }, confusing to me. // 并在解析} 时到达文件末尾,让我感到困惑。

/EDIT THANK YOU VERY MUCH. /编辑 非常感谢。 The constructive crit.建设性的暴击。 really helped, as I ended up with a lot of knowledge and my final code was真的很有帮助,因为我最终获得了很多知识,我的最终代码是

class Shape {

public static void ShapeAttemptTwo()
{
    class Circle extends Shape
        {
            public static CircleAttemptTwo()
            {
            int pi = 3.14;
            int r=4;
            }
        }
    class Rectangle extends Shape
    {
            public static Rectangle()
            {
            int l = 14;
            int b = 10;
            int z = l*b;
            }
    }
    class Square extends Shape
    {
        public static Square()
        {
        int a = 11;
        System.out.println(a * a);
        }
    }

You're doing several things wrong:你做错了几件事:

1) Your "static main()" belongs inside a class 1) 你的“静态 main()”属于一个类

2) You can only have one "public class" per module. 2) 每个模块只能有一个“公共课程”。

SUGGESTED CHANGE:建议更改:

public abstract class Shape 
{

    public static void main(String[] args)  {
      Shape rectangle = new Rectangle (14, 10);
      System.println ("rectangle's area=" + rectangle.getArea ());
      ...
    }

}

class Circle extends Shape  {
   ...
}

class Rectangle extends Shape  {

   int l;
   int b;

   public Rectangle (int l, int b) {
     this.l = l;
     this.b = b;
   }

   public int getArea () {
     return l * b;
   }
}
...

Are you joking?你在开玩笑? You're missing basic knowledges of Java:您缺少 Java 的基本知识:

1) Method yea doesn't have a return type. 1) 方法 yea 没有返回类型。

2) There's a code directly inside the class Rectangle, not in method. 2) Rectangle 类中直接有代码,而不是方法中。

3) There's a code directly inside the class Square, not in method. 3) 在类 Square 中直接有一个代码,而不是在方法中。

4) You're using very strange and weird code-style and formatting. 4)您使用了非常奇怪和怪异的代码样式和格式。 Update : Formatting has been fixed.更新:格式已修复。

2nd Update :第二次更新

1) Code has been changed and it's formatted wrong again. 1) 代码已更改,格式再次错误。

2) There are three static methods without defined return type. 2) 共有三种没有定义返回类型的静态方法。

Here are some more informations related on the topic.以下是与该主题相关的更多信息。

Aside from the errors Tomas pointed out:除了托马斯指出的错误:

5) You forgot the word class in the Square declaration. 5) 你忘记了Square声明中的class一词。

6) You cannot declare a public class directly inside a method. 6) 不能直接在方法内部声明public class Java does allow you to declare classes, called "local classes" inside methods. Java 确实允许您在方法中声明称为“本地类”的类。 However, I'm not sure if that's what you really want to do;但是,我不确定这是否是您真正想要做的; if you do, you can only use Circle inside your main method, so you're not really creating a hierarchy.如果这样做,则只能在main方法中使用Circle ,因此您并没有真正创建层次结构。 Anyway, when you declare a local class, it can't have public , protected , or private keywords on it, so that explains the first error message you're seeing.无论如何,当你声明一个本地类时,它不能有publicprotectedprivate关键字,所以这解释了你看到的第一条错误消息。

EDIT: Based on the second post: Every method has to have a return type;编辑:基于第二篇文章:每个方法都必须有一个返回类型; if you don't actually want the method to return anything, the return type should be void .如果您实际上不希望该方法返回任何内容,则返回类型应为void Thus:因此:

public static void CircleAttemptTwo() 
{
    //int pi = 3.14;  should be
    double pi = 3.14;
    int r=4;
}

However, constructors don't need a return type, but they can't be static .但是,构造函数不需要返回类型,但它们不能是static So public Square() and public Rectangle() are OK.所以public Square()public Rectangle()是可以的。 CircleAttemptTwo doesn't match the class name, so it isn't a constructor. CircleAttemptTwo与类名不匹配,因此它不是构造函数。

"reached end of file" usually means you're missing a } , as you did here. “到达文件末尾”通常意味着您缺少一个} ,就像您在这里所做的那样。

And 3.14 isn't an integer. 3.14 不是整数。

You need to change你需要改变

int pi = 3.14;

to

double pi = 3.14; 

Also, static method is in wrong place.另外,静态方法在错误的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM