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();
}
}
}
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();
}
}
除非是静态内部类 不然内部类不能写静态方法
第二是 方法入口不要写在静态内部类里