caohaoxue 2021-07-30 17:29 采纳率: 100%
浏览 25
已结题

这行代码为什么不可以运行求支点

public class lx {
class Computer {
String brand; //品牌 }
}

            public class SxtStu2 {
                // field
                int id;
                String sname;
                int age;
                Computer comp;

                void study() {
                    System.out.println("我正在学习!使用我们的电脑," + comp.brand);
                }

                SxtStu2() {

                }

                public static void main(String[] args) {
                    SxtStu2 stu = new SxtStu2();
                    stu.sname = "张三";
                    Computer comp = new Computer();
                    comp.brand = "联想";
                    stu.comp = comp;
                    stu.study();

                }
                }
            }

img

  • 写回答

2条回答 默认 最新

  • 八云黧 2021-07-30 17:39
    关注

    main函数最好别写在内部类里面,因为IDE会在外部类编译出的文件中寻找main函数作为程序入口
    代码最好这么写:

    class lx {
        class Computer {
            String brand; // 品牌 }
        }
    
        public class SxtStu2 {
            // field
            int id;
            String sname;
            int age;
            Computer comp;
    
            void study() {
                System.out.println("我正在学习!使用我们的电脑," + comp.brand);
            }
            SxtStu2() {
            }
        }
        public static void main(String[] args) {
            lx x = new lx();
            SxtStu2 stu = x.new SxtStu2();
            stu.sname = "张三";
            Computer comp = x.new Computer();
            comp.brand = "联想";
            stu.comp = comp;
            stu.study();
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月7日
  • 已采纳回答 7月30日
  • 创建了问题 7月30日

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改