1 class A 2 { 3 final int f=10; 4 double d; 5 abstract void method(); 6 }
时间: 2024-03-25 07:36:07 浏览: 12
这段代码定义了一个抽象类A,包含一个final修饰的int类型变量f、一个double类型变量d和一个抽象方法method()。final修饰的变量f表示该变量的值不能被修改,因此在类A中定义时必须进行初始化。
需要注意的是,抽象类中的抽象方法没有具体的实现,因此不能直接创建抽象类的实例。如果要使用抽象类中的方法,必须通过继承该抽象类并实现其中的抽象方法来创建一个子类。例如:
```
abstract class A {
final int f = 10;
double d;
abstract void method();
}
class B extends A {
void method() {
System.out.println("This is a method in B");
}
}
public class E {
public static void main(String args[]) {
A a = new B();
a.method(); // 输出"This is a method in B"
}
}
```
在这个例子中,B类继承了A类,并实现了其中的抽象方法method()。在主函数中,创建了一个B类的实例并将其向上转型为A类型,通过A类型的变量来调用method()方法,输出了一段信息。
相关问题
把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));
}
}
}
设计一个抽象类名字为 Person,它的子类为Student和 Employee;person类有name以及toString方法;student有 status属性(freshman, sophomore, junior, or senior),定义status 为常量;Employee类 有office, salary;分别定义各个类的有参构造方法,Person(String name)、Student(String name,int status)、Employee(String name,String office,double salary),注意需要在子类构造方法第一句调用父类有参构造方法,为子类也实现toString方法。编写测试类定义一个数组存放每个非抽象类对象(Student和 Employee),然后利用多态调用toString方法打印各个对象的所有属性信息。(为了减少书写量,只需要为name属性完成get和set方法,其他属性不用写)
以下是根据您的要求设计的Person抽象类及其子类Student和Employee的Java代码:
```java
abstract class Person {
protected String name;
public Person(String name) {
this.name = name;
}
public abstract String toString();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Student extends Person {
private int status;
public static final int FRESHMAN = 1;
public static final int SOPHOMORE = 2;
public static final int JUNIOR = 3;
public static final int SENIOR = 4;
public Student(String name, int status) {
super(name);
this.status = status;
}
@Override
public String toString() {
return "学生姓名: " + name + ", 学生状态: " + getStatusName();
}
private String getStatusName() {
switch (status) {
case FRESHMAN:
return "一年级";
case SOPHOMORE:
return "二年级";
case JUNIOR:
return "三年级";
case SENIOR:
return "四年级";
default:
return "未知";
}
}
}
class Employee extends Person {
private String office;
private double salary;
public Employee(String name, String office, double salary) {
super(name);
this.office = office;
this.salary = salary;
}
@Override
public String toString() {
return "员工姓名: " + name + ", 员工办公室: " + office + ", 员工薪水: " + salary;
}
}
public class Test {
public static void main(String[] args) {
Person[] people = new Person[2];
people[0] = new Student("张三", Student.SOPHOMORE);
people[1] = new Employee("李四", "A101", 5000.0);
for (Person person : people) {
System.out.println(person.toString());
}
}
}
```
在这个程序中,定义了抽象类Person,其中包含了name属性和toString方法。Student类和Employee类分别继承自Person类,并添加了各自的属性和相关方法。在子类的构造方法中,通过super关键字调用了父类的有参构造方法。
测试类Test创建了一个Person类型的数组,用于存放Student和Employee对象。通过多态性,遍历数组并调用toString方法打印出每个对象的所有属性信息。
希望这个程序满足了您的需求!如果还有其他问题,请随时提问。