标签:rgs 部分 generate image method .com com gety 一个
abstract(抽象)修饰符,可以修饰类和方法
1,abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型,抽象类就像当于一类的半成品,需要子类继承并覆盖其中的抽象方法。
2,abstract修饰方法,会使这个方法变成抽象方法,也就是只有声明(定义)而没有实现,实现部分以";"代替。需要子类继承实现(覆盖)。
注意:有抽象方法的类一定是抽象类。但是抽象类中不一定都是抽象方法,也可以全是具体方法。
abstract修饰符在修饰类时必须放在类名前。
abstract修饰方法就是要求其子类覆盖(实现)这个方法。调用时可以以多态方式调用子类覆盖(实现)后的方法,也就是说抽象方法必须在其子类中实现,除非子类本身也是抽象类。
注意:父类是抽象类,其中有抽象方法,那么子类继承父类,并把父类中的所有抽象方法都实现(覆盖)了,子类才有创建对象的实例的能力,否则子类也必须是抽象类。抽象类中可以有构造方法,是子类在构造子类对象时需要调用的父类(抽象类)的构造方法。
举个简单的例子下面有一个抽象类
abstract class E{
public abstract void show();//public abstract 可以省略
}
然后其它类如果继承它通常为了实现它里面的方法
class F extends E{
void show(){
//写具体实现的代码
}
}
最后再主方法里面定义一个父类引用指向子类对象,就会发生多态现象,比如
E e=new F();
e.show();
实际调用了子类里面的show()方法
代码详情
/////////////////////////////////////////////////////////////////////
public abstract class Employee {
protected String name;
protected int number;
protected MyDate birthday;
abstract int earnings();
@Override
public String toString() {
return "Employee [name=" + name + ", number=" + number + ", birthday=" + birthday + "]";
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String name, int number, MyDate birthday) {
super();
this.name = name;
this.number = number;
this.birthday = birthday;
}
}
//////////////////////////////////////////////////////////
public class MyDate {
private int day;
private int month;
private int year;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
public MyDate() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "MyDate [day=" + day + ", month=" + month + ", year=" + year + "]";
}
}
////////////////////////////////////
public class HourEmployyee extends Employee {
private int wage;
private int hour;
@Override
int earnings() {
// TODO Auto-generated method stub
return wage * hour;
}
public HourEmployyee(int wage, int hour) {
super();
this.wage = wage;
this.hour = hour;
}
public HourEmployyee() {
super();
// TODO Auto-generated constructor stub
}
public HourEmployyee(String name, int number, MyDate birthday) {
super(name, number, birthday);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "HourEmployyee" + super.toString();
}
}
///////////////////////////////////////
public class SalariedEmployee extends Employee{
private int weeklySalary;
@Override
int earnings() {
// TODO Auto-generated method stub
return weeklySalary;
}
@Override
public String toString() {
return "SalariedEmployee [weeklySalary=" + weeklySalary +
"name=" + name + ", number=" + number + ", birthday=" +
birthday + "]";
}
public SalariedEmployee() {
super();
// TODO Auto-generated constructor stub
}
public SalariedEmployee(String name, int number, MyDate birthday,int weeklySalary) {
super(name, number, birthday);
this.weeklySalary = weeklySalary;
// TODO Auto-generated constructor stub
}
public SalariedEmployee(int weeklySalary) {
super();
this.weeklySalary = weeklySalary;
}
}
///////////////////////////////////////////
public class PayrollSystem {
public static void main(String[] args) {
Employee [] em = new Employee[3];
em[0] = new SalariedEmployee("tom",1001,new MyDate(1,2,2000),1000);
em[1] = new SalariedEmployee("jack",1001,new MyDate(1,3,2100),4000);
em[2] = new SalariedEmployee("jerry",1001,new MyDate(1,4,2000),5000);
for(int i = 0;i < em.length ; i++){
System.out.println(em[i]);
System.out.println("工资" + em[i].earnings());
}
}
}
//////////////////////////////////////////
标签:rgs 部分 generate image method .com com gety 一个
原文地址:https://www.cnblogs.com/Mrchengs/p/9649646.html