接口抽象类小结

抽象类

抽象方法必须使用abstract关键字进行修饰。如果一个类含有抽象方法,则称这个类为抽象类,抽象类必须在类前用abstract关键字修饰。因为抽象类中无具体实现的方法,所以不能用抽象类创建对象。

	public abstract class TestAbstractDoor {

		public abstract void open();

		public abstract void close();

	}
	注:只能有方法声明不能出现实现体{}

抽象类与普通类的区别:

  1. 抽象方法必须为public或者protected(因为如果为private,则不能被子类继承,子类便无法实现该方法),缺省情况下默认为public;
  2. 抽象类不能用来创建对象;
  3. 如果一个类继承于一个抽象类,则子类必须实现父类的抽象方法。如果子类没有实现父类的抽象方法,则必须将子类也定义为为abstract类。

在其他方面,抽象类和普通的类并没有区别。

接口

接口,英文称作interface,在软件工程中,接口泛指供别人调用的方法或者函数。从这里,我们可以体会到Java语言设计者的初衷,它是对行为的抽象。

	public interface TestInterface {

	 public  void beforeHandle();
	 public  void dealHandle();
	 public void afterHandle();

}

要点

  1. 接口中的变量会被隐式地指定为public static final变量,并且只能是public static final变量,即只能定义常量;
  2. 方法会被隐式地指定为public abstract方法且只能是public abstract方法,即接口中的方法都是抽象方法
  3. JDK1.8之后可以在接口中定义默认方法以及静态方法

抽象类与接口的区别

  • 语法上

在这里插入图片描述

  • 设计模式上
  1. 抽象类是对一种事物的抽象,即对类抽象,而接口是对行为的抽象。抽象类是对整个类整体进行抽象,包括属性、行为,但是接口却是对类局部(行为)进行抽象;
  2. 设计层面不同,抽象类作为很多子类的父类,它是一种模板式设计。而接口是一种行为规范,它是一种辐射式设计。
异常提示:Servlet.service() for servlet [dispatcher] in context with path [/comsed1] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Bean object must not be null; nested exception is java.lang.IllegalArgumentException: Bean object must not be null] with root cause java.lang.IllegalArgumentException: Bean object must not be null at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:213) at org.springframework.beans.BeanWrapperImpl.setWrappedInstance(BeanWrapperImpl.java:202) at org.springframework.beans.BeanWrapperImpl.<init>(BeanWrapperImpl.java:151) at org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.<init>(DirectFieldAccessFallbackBeanWrapper.java:35) at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.getId(JpaMetamodelEntityInformation.java:117) at org.springframework.data.repository.core.support.AbstractEntityInformation.isNew(AbstractEntityInformation.java:51) at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.isNew(JpaMetamodelEntityInformation.java:196) at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:376) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:405) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:390) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) at com.sun.proxy.$Proxy136.save(Unknown Source) at iot.service.ProductMasterService.modifyProductMaster(ProductMasterService.java:32) at iot.controller.productMasterController.modifyProductMaster(productMasterController.java:95) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 代码部分: @RequestMapping(value = "modifyProductMasterInit") public String modifyProductMasterInit(@RequestParam("productId") String productId,Model model){ // Iterable<ProductMaster> productMasterList = productMasterDAO.findByProductId(productId); // model.addAttribute("productMasterList", productMasterList); ProductMaster productMaster = this.productMasterService.findByProductId(productId); model.addAttribute("productMaster", productMaster); return "productMaster/Pro_Master_modify"; } @RequestMapping(value = "modifyProductMaster") public String modifyProductMaster(ProductMaster productMaster,@RequestParam("productId") String productId){ ProductMaster productMasterOld = this.productMasterService.findByProductId(productId);//经断点调试productMasterOld为空 try { BeanUtils.copyProperties(productMaster, productMasterOld); } catch (Exception e) { e.printStackTrace(); } this.productMasterService.modifyProductMaster(productMasterOld); return "redirect:/productMaster/loadProductMaster"; } Service方法: @Service public class ProductMasterService { @Autowired(required = false) private ProductMasterDAO productMasterDAO; public void addproductMaster(ProductMaster productMaster) { this.productMasterDAO.save(productMaster); } public ProductMaster findByProductId(String productId) { return this.productMasterDAO.findByProductId(productId); } public void modifyProductMaster(ProductMaster productMaster) { this.productMasterDAO.save(productMaster); } } ProductMasterDAO部分: public interface ProductMasterDAO extends PagingAndSortingRepository<ProductMaster, String>,JpaSpecificationExecutor<ProductMaster>{ @Query("SELECT p FROM ProductMaster p WHERE p.productId = ?1") public ProductMaster findByProductId(String productId); }
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页