下面这段代码有什么问题? private static void Func2(){ System.out.println("chekc fun"); }
时间: 2024-06-11 21:07:50 浏览: 9
这段代码没有明显的语法错误,但是可能存在以下问题:
1. 函数名 Func2 不符合 Java 代码规范,应该改为 func2 或者 funcTwo。
2. 函数没有参数,也没有返回值,可能无法完成预期的功能。
3. 函数体只有一行代码,可能需要更多的代码来完成实际的任务。
4. 函数名和函数体中的单词拼写错误,应该是 check 而不是 chekc。
相关问题
找出下段代码中的语法错误,说明原因,并改正 public class AbstractDemo { private int i = 0; public void func1(); public static void func2() { system.out.println("变量i的值为:"+i); } } class Sub extend AbstractDemo { public static func2() { System.out.println("变量i的值为:"+i);
该段代码中存在两个语法错误。
第一个语法错误:在 AbstractDemo 类中,方法 func1() 后面有一个分号,应该去掉分号。
第二个语法错误:在 Sub 类中,方法 func2() 的修饰符缺失了关键字“void”,应该改为“public static void func2()”。
另外,由于 i 是 AbstractDemo 类的私有成员变量,Sub 类不能直接访问,所以需要使用 super.i 来访问。
改正后的代码如下:
```
public class AbstractDemo {
private int i = 0;
public void func1() {
// 方法体
}
public static void func2() {
System.out.println("变量i的值为:" + super.i);
}
}
class Sub extends AbstractDemo {
public static void func2() {
System.out.println("变量i的值为:" + super.i);
}
}
```
请给出下面代码的输出结果 clase Child extens Base { public void func1() { System.out.println("Child func1 print."); } public void func2() { System.out.println("Child func2 print."); } } public clase Base { private func1() { System.out.println("Base func1 print."); } public func2() { System.out.println("Base func2 print."); } public static void main(String args[]) { Base b = new child(); b.func1(); b.func2(); ((Child)b).func1(); ((Child)b).func2(); } }
输出结果为:
非法的符号:类 clase
应该是 class Child extends Base
Base func2 print.
Child func2 print.
Child func1 print.
Child func2 print。
解释:
在代码中,我们定义了一个 Base 类和一个 Child 类,Child 类继承自 Base 类。在 main 方法中,我们创建了一个 Child 类的实例,并将其赋值给一个 Base 类型的变量 b。然后我们调用了 b 的 func1 和 func2 方法,这两个方法都是在 Base 类中定义的,因此输出结果为 "Base func2 print."。接着我们强制将 b 转换为 Child 类型,并调用了 Child 类中的 func1 和 func2 方法,输出结果为 "Child func1 print." 和 "Child func2 print."。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)