马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 帅雷必成c王 于 2022-7-4 20:39 编辑 public class Homework05 {
public static void main(String[] args) {
A a = new A(); //这里 外部其它类访问局部内部类方法
a.AA();
}
}
class A{
private String name = "小帕";
public void AA(){
class B{
private final String name = "类一起";
public void show(){
System.out.println("NAME=" +name );
System.out.println(A.this.name);
}
}
B b = new B();
b.show();
}
}
public class Homework07 {
public static void main(String[] args) {
Car car = new Car();
Car.a.flow();
}
class Car {
private double temperature;
public Car(double temperature) {
this.temperature = temperature;
}
class Air {
public void flow() {
if (temperature > 40) {
System.out.println("吹冷气");
} else if (temperature < 0) {
System.out.println("吹暖气");
} else {
System.out.println("关闭空调");
}
}
}
Air a = new Air();
a.flow();
}
??? 同样都是成员内部类,为什么第一个可以直接在外部类里面new 一个对象 然后调用方法
第二个就不行0.0
要放入方法中,你在类空间不能直接进行函数的调用,不然编译就过不了
public class Homework07 {
public static void main(String[] args) {
Car car = new Car(1.0);
car.a.flow();
}
static class Car {
private double temperature;
public Car(double temperature) {
this.temperature = temperature;
}
class Air {
public void flow() {
if (temperature > 40) {
System.out.println("吹冷气");
} else if (temperature < 0) {
System.out.println("吹暖气");
} else {
System.out.println("关闭空调");
}
}
}
Air a = new Air();
}
}
|