Products
GG网络技术分享 2025-11-13 05:08 1
接口和抽象类在Java中dou是为了实现抽象而存在的,但它们有不同的用途和用场景。
实现方式

目的
方法
构造函数
变量
java // 抽象类 Employee abstract class Employee { private String name; private String address; private int number;
public Employee {
this.name = name;
this.address = address;
this.number = number;
}
public abstract double computePay;
public void mailCheck {
System.out.println;
}
public String toString {
return name + " " + address + " " + number;
}
public String getName {
return name;
}
}
// 子类 Salary class Salary extends Employee { private double salary;
public Salary {
super;
setSalary;
}
public void mailCheck {
System.out.println;
System.out.println + " with salary " + salary);
}
public double getSalary {
return salary;
}
public void setSalary {
if {
salary = newSalary;
}
}
public double computePay {
System.out.println);
return salary / 100;
}
public class AbstractDemo { public static void main { Salary s = new Salary; Employee e = new Salary; System.out.println; s.mailCheck; System.out.println; e.mailCheck; } }
通过搞懂接口和抽象类的不一样, 开发者Nenggeng优良地选择适合项目需求的抽象方式,从而搞优良代码的可读性、可维护性和可 性。
Demand feedback