public class one { class Inter { public void go() { System.out.println("hi"); } } }
时间: 2024-05-10 07:14:03 浏览: 9
这段 Java 代码定义了一个名为 `one` 的公共类,该类内部还定义了一个名为 `Inter` 的内部类。`Inter` 类中有一个名为 `go` 的公共无参方法,该方法将在调用时输出字符串 "hi"。
需要注意的是,由于 `Inter` 类被定义在 `one` 类中,因此在其他类中无法直接使用 `Inter` 类。如果需要在其他类中使用 `Inter` 类,可以通过创建 `one` 类的实例,然后使用该实例来访问 `Inter` 类。例如:
```
one o = new one();
Inter i = o.new Inter();
i.go(); // 输出 "hi"
```
相关问题
public class VariableDemo { int a = 111; final int b = 222; public void display() { System.out.println("执行display()"); int a = 333; System.out.println(a); System.out.println(this.a); System.out.println(b); System.out.println(this.b); } public static void main(String[] args) { // TODO Auto-generated method stub int a = 444; System.out.println("执行main()"); System.out.println(a); //System.out.println(this.a); {// 代码块1 int c = 666; System.out.println(c); } System.out.println(c); {// 代码块2 int d = 666; System.out.println(d); } int d = 888; System.out.println(d); VariableDemo vd = new VariableDemo(); vd.display(); vd.b = 999; System.out.println(vd.b); } }
该代码存在以下问题:
1. 在`main`方法中,不能直接访问非静态变量`a`和`b`,因为它们是属于对象的,必须先创建一个类的实例才能访问。
2. `final`修饰的变量`b`不能被修改,所以在`vd.b = 999;`中会编译错误。
3. 在代码块1中定义的变量`c`只能在代码块内部访问,超出代码块范围后就不能再访问了。在`System.out.println(c);`处会编译错误。
4. 变量名`d`在代码块2和`main`方法中都出现了,这是不合法的,因为在同一级作用域中变量名不能重复。
修正后的代码如下:
```
public class VariableDemo {
int a = 111;
final int b = 222;
public void display() {
System.out.println("执行display()");
int a = 333;
System.out.println(a);
System.out.println(this.a);
System.out.println(b);
System.out.println(this.b);
}
public static void main(String[] args) {
int a = 444;
System.out.println("执行main()");
System.out.println(a);
{
int c = 666;
System.out.println(c);
}
// System.out.println(c); // 编译错误
{
int d = 666;
System.out.println(d);
}
int d = 888;
System.out.println(d);
VariableDemo vd = new VariableDemo();
vd.display();
// vd.b = 999; // 编译错误
System.out.println(vd.b);
}
}
```
修正后的代码中,将`vd.b = 999;`注释掉,因为`b`是用`final`修饰的,不能被修改。同时,变量名重复的问题也得到了修正。
下面代码输出结果是public class Demo03{public static void main(String0args){Foo foo=new Foo0;foo.test(127);}}class Foo{void test(byte a){System.out.println("byte");void test(int...a){System.out.println("int...");void test(short a){System.out.println("short");void test(long a){System.out.printin("long");}}
这段代码是有语法错误的,编译无法通过。其中,Foo类中的多个test方法都没有正确闭合,缺少了右大括号。正确的代码应该是这样的:
```
public class Demo03 {
public static void main(String[] args) {
Foo foo = new Foo();
foo.test(127);
}
}
class Foo {
void test(byte a) {
System.out.println("byte");
}
void test(int... a) {
System.out.println("int...");
}
void test(short a) {
System.out.println("short");
}
void test(long a) {
System.out.println("long");
}
}
```
在这个修正后的代码中,如果执行main方法,会输出 "byte"。原因同上。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)