迷失北京

BlogJava 联系 聚合 管理
  60 Posts :: 0 Stories :: 13 Comments :: 0 Trackbacks

在面试可能经常被问到,接口有什么作用?相对于类而言,为何需要用类实现接口等等诸如此类的问题。如果不仔细想想,有时候还会卡住。这篇文章将简单介绍下接口的作用。 
对于接口而言,就是用来标记类的一个产物。每个接口都对应有一个或多个实现它的类,否则这个接口就成为冗余代码。因为管理接口比管理类要简单挺多,接口就由此体现了抽象的观点。换句话说,接口就是没有属性和行为实现的类。类实现多个接口,可以解决类不能继承多个类的机制。那么接口该怎么使用呢? 
一 接口的作用 
下面有两个方面: 
1. Java多态接口动态加载实例 
这种实现方式,大家在很多场合下都可以见到。下面介绍一个Sample: 
1) 接口类: 

public interface PersonService {
	/**
	 * 计算每月的工资
	 *  sal:工资基数
	 *  type:员工等级
	 */
	public double calcuMonthlySalary(double sal, int type);
	
}

  

2) 实现类: 

public class ManagerServiceBean implements PersonService {
	/**
	 * 
	 */
	public ManagerServiceBean() {
	}
	/* (non-Javadoc)
	 * @see com.java.test.Interface.PersonService#calcuMonthlySalary(double, int)
 */
	public double calcuMonthlySalary(double sal, int type) {
				System.out.println("ManagerServiceBean: "+(sal*type*2));
		
return sal*type*2;
	}
}
public class EmployeeServiceBean implements PersonService {
	/**
	 * 
	 */
	public EmployeeServiceBean() {
	}
	/* (non-Javadoc)
	 * @see com.java.test.Interface.PersonService#calcuMonthlySalary(double, int)
	 */
	public double calcuMonthlySalary(double sal, int type) {
		System.out.println("EmployeeServiceBean: "+(sal*type*2));
		return sal*type*1.5;
	}
}
}

  

3) 测试类: 

public class SalaryManageTest {
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		try{
//这里写得不智能,还需要传入传输
			PersonService personService = (PersonService)Class.forName(args[0]).newInstance();
			if (args[1] != null && args[1] != "" &&
					args[2] != null && args[2] != ""){
				int sal = Integer.valueOf(args[1]).intValue();
				int type = Integer.valueOf(args[2]).intValue();
	
//这里可以根据传入参数的不同,调用不同的实现类,
			//如果再增加一类新的成员,我们也只要新增一个类,无需修改方法
				personService.calcuMonthlySalary(sal, type);
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

  

这里,通过接口在获取类对象的时候,个人觉得写得不是很好。。但也许大家用过Ejb的人都应该知道,客户端获取Ejb bean的时候,就是通过这种方式获取:

Java代码

PersonService personService = (PersonService)context.lookup(“personServiceBean/remote”);

  

2. Java接口作为参数传递 
下面列出一个Sample: 
1) 接口,实现类同上。 
2) Person实体类: 

public class Person {
	private double sal;
	private int type;
	
	/**
	 * 
	 */
	public Person() {
	}
	/**
	 * @param sal
	 * @param type
	 */
	public Person(double sal, int type) {
		this.sal = sal;
		this.type = type;
	}
	/**
	 * @return the sal
	 */
	public double getSal() {
		return sal;
	}
	/**
	 * @param sal the sal to set
	 */
	public void setSal(double sal) {
		this.sal = sal;
	}
	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}
	/**
	 * @param type the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}
}

  

3) 接口作为参数传入类: 

 public class SalaryManager {
	/**
	 * 
	 */
	public SalaryManager() {
	}
	
	public void getMonthlySalary(PersonService personService, Person person){
		personService.calcuMonthlySalary(person.getSal(), person.getType());
	}
}

  

4) 测试类: 

public class SalaryManageTest {
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		try{
			ManagerServiceBean managerServiceBean = new ManagerServiceBean();
			EmployeeServiceBean employeeServiceBean = new EmployeeServiceBean();
			SalaryManager salaryManager = new SalaryManager();
		//call 接口作为传入参数的实现类方法	
salaryManager.getMonthlySalary(managerServiceBean, new Person(1000, 7));
			salaryManager.getMonthlySalary(employeeServiceBean, new Person(1000, 4));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

  

二  接口与抽象类的区别 
   我个人认为有以下几点: 
   1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系。但是,一个类却可以实现多个interface。这弥补了类的多继承问题。 

  2.在abstract class 中可以有自己的数据成员,也可以有非abstarct的成员方法,而在interface中,只能够有静态的不能被修改的数据成员(也就是必须是static final的,不过在 interface中一般不定义数据成员),所有的成员方法都是abstract的。 

  3.abstract class和interface所反映出的设计理念不同。其实abstract class表示的是"is-a"关系,interface表示的是"like-a"关系。 注意:当一个行为与一个类的所属行为方法不相关联时,应该采用接口来实现这个行为,不能使用抽象类,否则违反面向对象的ISP(Interface Segregation Principle)规则和OCP原则. 

  4.实现抽象类和接口的类必须实现其中的所有方法。抽象类中可以有非抽象方法。接口中则不能有实现方法。 

  5.接口中定义的变量默认是public static final 型,且必须给其初值,所以实现类中不能重新定义,也不能改变其值。当然,变量定义也可以是friendly. 

  6.抽象类中的变量默认是 friendly 型,其值可以在子类中重新定义,也可以重新赋值。 

  7.接口中的方法默认都是 public,abstract 类型的。

posted on 2011-02-12 20:11 王康 阅读(154) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: