不是封闭的类Java

本文翻译自:Is not an enclosing class Java

I'm trying to make a Tetris game and I'm getting the compiler error 我正在尝试制作俄罗斯方块游戏,但出现编译器错误

Shape is not an enclosing class

when I try to create an object 当我尝试创建对象时

public class Test {
    public static void main(String[] args) {
        Shape s = new Shapes.ZShape();
    }
}

I'm using inner classes for each shape. 我为每个形状使用内部类。 Here's part of my code 这是我的代码的一部分

public class Shapes {
    class AShape {
    }
    class ZShape {
    }
}

What am I doing wrong ? 我究竟做错了什么 ?


#1楼

参考:https://stackoom.com/question/1Myet/不是封闭的类Java


#2楼

ZShape is not static so it requires an instance of the outer class. ZShape不是静态的,因此它需要外部类的实例。

The simplest solution is to make ZShape and any nested class static if you can. 最简单的解决方案是,如果可以的话,使ZShape和任何嵌套的类static

I would also make any fields final or static final that you can as well. 我还可以使任何字段为finalstatic final


#3楼

I have encountered the same problem. 我遇到了同样的问题。 I solved by creating an instance for every inner public Class. 我通过为每个内部公共类创建一个实例来解决。 as for you situation, i suggest you use inheritance other than inner classes. 至于您的情况,我建议您使用继承而不是内部类。

public class Shape {

    private String shape;

    public ZShape zShpae;
    public SShape sShape;

    public Shape(){
      int[][] coords =  noShapeCoords;
      shape = "NoShape";
      zShape = new ZShape();
      sShape = new SShape();
    }

    class ZShape{
      int[][] coords =  zShapeCoords;
      String shape = "ZShape";
    }

    class SShape{
      int[][] coords = sShapeCoords;
      String shape = "SShape";
    }

 //etc
}

then you can new Shape(); 然后可以新建Shape(); and visit ZShape through shape.zShape; 并通过shape.zShape访问ZShape;


#4楼

Suppose RetailerProfileModel is your Main class and RetailerPaymentModel is an inner class within it. 假设RetailerProfileModel是您的Main类,而RetailerPaymentModel是其中的内部类。 You can create an object of the Inner class outside the class as follows: 您可以在类外创建Inner类的对象,如下所示:

RetailerProfileModel.RetailerPaymentModel paymentModel
        = new RetailerProfileModel().new RetailerPaymentModel();

#5楼

No need to make the nested class as static but it must be public 无需将嵌套类设为静态,但它必须是公共的

public class Test {
    public static void main(String[] args) {
        Shape shape = new Shape();
        Shape s = shape.new Shape.ZShape();
    }
}

#6楼

What I would suggest is not converting the non-static class to a static class because in that case, your inner class can't access the non-static members of outer class. 我建议不要将非静态类转换为静态类,因为在这种情况下,您的内部类无法访问外部类的非静态成员。

Example : 范例:

class Outer
{
    class Inner
    {
        //...
    }
}

So, in such case, you can do something like: 因此,在这种情况下,您可以执行以下操作:

Outer o = new Outer();
Outer.Inner obj = o.new Inner();
在开发中遇到的“`Toast`提示不是封闭类”错误,通常出现在尝试对`Toast`进行某些不符合其设计意图的操作时,例如尝试继承或修改其内部行为。`Toast`在Android中是一个封装好的非开放(即非`open`),这意味着它不允许被继承或重写其方法,这是为了确保系统的稳定性和安全性[^1]。 ### 解决方案 #### 1. **避免继承 `Toast` ** 由于`Toast`不是开放,直接继承`Toast`并尝试重写其方法会导致编译错误。正确的做法是使用`Toast`的现有API来创建和显示提示信息,而不是尝试修改其行为。例如: ```kotlin Toast.makeText(context, "这是一个Toast提示", Toast.LENGTH_SHORT).show() ``` #### 2. **使用封装函数替代继承** 如果需要对`Toast`进行统一的样式或行为管理,可以通过封装一个工具或函数来实现,而不是继承`Toast`。例如: ```kotlin object CustomToast { fun show(context: Context, message: String) { val toast = Toast.makeText(context, message, Toast.LENGTH_SHORT) // 可以在此处统一设置样式或位置 toast.setGravity(Gravity.CENTER, 0, 0) toast.show() } } ``` 然后在代码中调用: ```kotlin CustomToast.show(context, "自定义Toast提示") ``` #### 3. **使用 `Snackbar` 或其他替代方案** 如果发现`Toast`在某些场景下无法满足需求(例如需要交互性或更复杂的UI),可以考虑使用`Snackbar`或其他自定义的视图组件来替代。`Snackbar`提供了更丰富的用户反馈方式,并且支持点击操作[^1]。 ```kotlin Snackbar.make(view, "这是一个Snackbar提示", Snackbar.LENGTH_LONG) .setAction("确定") { // 点击后的操作 } .show() ``` #### 4. **检查 Kotlin 编译器设置(仅限 Kotlin)** 如果使用的是 Kotlin 编写代码,并且错误提示中提到“不是密封”或“不是封闭类”,请检查是否启用了 Kotlin 的密封(sealed class)或封闭类(final class)相关特性。默认情况下,Kotlin 中的封闭的,不能被继承,除非显式使用 `open` 关键字。`Toast`本身是 Java ,但在 Kotlin 中使用时也应遵循这一规则。 ### 总结 “`Toast`提示不是封闭类”错误的根本原因在于尝试对一个非开放进行继承或重写。解决方案包括:避免继承`Toast`、使用封装函数统一管理提示逻辑、考虑使用`Snackbar`等替代组件,以及检查 Kotlin 的继承规则。通过这些方式,可以有效避免此编译错误并提升用户体验。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值