4-4 指出以下声明中的错误。 Protected interface Area { Public static int left; Public static final int RIGHT; Public Area() Private double area; Public static abstract double perimeter(); } New Area()
时间: 2024-02-25 19:56:14 浏览: 135
以下是声明中的错误:
1. 接口不能被声明为protected。因此,应该将`protected`改为`public`或`interface`。
2. 接口中的字段应该是常量,因此应该将`Public static int left`改为`public static final int left`。
3. `RIGHT`字段没有初始化,因此应该为其指定一个常量值或在构造函数中初始化。
4. 接口不能拥有构造函数,因此应该删除`Public Area()`构造函数。
5. `area`字段是私有的,因此无法在接口中访问。应该将其移动到实现该接口的类中。
6. 接口中的抽象方法不能是静态的,因此应该删除`static`关键字。
综上所述,修正后的声明应该如下所示:
```
public interface Area {
public static final int left = 0;
public static final int RIGHT = 1;
public abstract double perimeter();
}
```
注意,由于接口中的方法都是抽象的,因此不需要使用`abstract`关键字来修饰。此外,由于接口中的字段都是常量,因此可以省略`public static final`中的任何一个关键字。
相关问题
把public abstract class Employee { protected String name; // 姓名 protected double fixedSalary; // 固定工资 public Employee(String name, double fixedSalary) { this.name = name; this.fixedSalary = fixedSalary; } // 计算月工资的抽象方法 public abstract double calculateSalary(int overtime);}和public class SeniorEngineer extends Employee { private static final double OVERTIME_SUBSIDY = 50.0; public SeniorEngineer(String name) { super(name, 5000.0); } @Override public double calculateSalary(int overtime) { return fixedSalary + overtime * OVERTIME_SUBSIDY; }}public class JuniorEngineer extends Employee { private static final double OVERTIME_SUBSIDY = 30.0; public JuniorEngineer(String name) { super(name, 3000.0); } @Override public double calculateSalary(int overtime) { return fixedSalary + overtime * OVERTIME_SUBSIDY; }}public class Trainee extends Employee { private static final double OVERTIME_SUBSIDY = 20.0; public Trainee(String name) { super(name, 2000.0); } @Override public double calculateSalary(int overtime) { return fixedSalary + overtime * OVERTIME_SUBSIDY; }}和public class Main { public static void main(String[] args) { Employee[] employees = { new SeniorEngineer("张三"), new JuniorEngineer("李四"), new Trainee("王五") }; int[] overtimes = {10, 20, 30}; for (int i = 0; i < employees.length; i++) { double salary = employees[i].calculateSalary(overtimes[i]); System.out.println(String.format("%s的月工资为:%.2f元", employees[i].name, salary)); } }}合成一个程序
public abstract class Employee {
protected String name; // 姓名
protected double fixedSalary; // 固定工资
public Employee(String name, double fixedSalary) {
this.name = name;
this.fixedSalary = fixedSalary;
}
// 计算月工资的抽象方法
public abstract double calculateSalary(int overtime);
}
public class SeniorEngineer extends Employee {
private static final double OVERTIME_SUBSIDY = 50.0;
public SeniorEngineer(String name) {
super(name, 5000.0);
}
@Override
public double calculateSalary(int overtime) {
return fixedSalary + overtime * OVERTIME_SUBSIDY;
}
}
public class JuniorEngineer extends Employee {
private static final double OVERTIME_SUBSIDY = 30.0;
public JuniorEngineer(String name) {
super(name, 3000.0);
}
@Override
public double calculateSalary(int overtime) {
return fixedSalary + overtime * OVERTIME_SUBSIDY;
}
}
public class Trainee extends Employee {
private static final double OVERTIME_SUBSIDY = 20.0;
public Trainee(String name) {
super(name, 2000.0);
}
@Override
public double calculateSalary(int overtime) {
return fixedSalary + overtime * OVERTIME_SUBSIDY;
}
}
public class Main {
public static void main(String[] args) {
Employee[] employees = {
new SeniorEngineer("张三"),
new JuniorEngineer("李四"),
new Trainee("王五")
};
int[] overtimes = {10, 20, 30};
for (int i = 0; i < employees.length; i++) {
double salary = employees[i].calculateSalary(overtimes[i]);
System.out.println(String.format("%s的月工资为:%.2f元", employees[i].name, salary));
}
}
}
public final static void int w(); 错在哪里
这段代码是错误的,因为Java语法中,返回值类型和方法名的位置是互换的,应该是:
```
public static final int w() {
// 方法体
}
```
其中,修饰符的顺序可以任意,但是通常遵循public、protected、private的顺序,static和final可以放在任意位置。而且,方法名后应该跟着一对小括号,而不是int类型的声明。
阅读全文