java8 interface default method static method and repeated annotation

Interface default and static methods

Default method

interface MyInterface1 {

	default String method1() {
		return "myInterface1 default method";
	}
}

class MyClass{
	public String method1() {
		return "myClass method";
	}
}

/**
 * There are the same methods in the parent class and the interface. The method of the parent class is used by default, that is, class first
 * @author Moyu Duo
 *
 */
class MySubClass1 extends MyClass implements MyInterface1{

}

@Test
public void test1() {
  MySubClass1 mySubClass1=new MySubClass1();
  System.out.println(mySubClass1.method1());//myClass method
}

If the method name of the parent class and the method name in the interface are the same and the parameters are the same, and the method has not been overridden by the child class, the method of the parent class is used by default, that is, class first

interface MyInterface1 {

	default String method1() {
		return "myInterface1 default method";
	}
}

interface MyInterface2 {

	default String method1() {
		return "myInterface2 default method";
	}
}

/**
 * If the interface implemented by a class has a default method with the same name and the same parameter type, it must be overridden in the class
 * @author Moyu Duo
 *
 */
class MySubClass2 implements MyInterface1,MyInterface2{

	@Override
	public String method1() {
		return MyInterface1.super.method1();
	}
	
}

@Test
public void test2() {
  MySubClass2 mySubClass2=new MySubClass2();
  System.out.println(mySubClass2.method1());//myInterface1 default method
}

If the interface implemented by a class has a default method with the same name and the same parameter type, it must be overridden in the class

Static method

interface MyInterface1 {	
	static String method2() {
		return "interface static method";
	}
}

@Test
public void test3() {
  System.out.println(MyInterface1.method2());//interface static method
}

Repeated annotations

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MAnnotation {
	String name() default "";
	int age();
}

public class AnnotataionTest {

	@Test
	public void test() throws Exception {
		Class<AnnotataionTest> clazz=AnnotataionTest.class;
		Method method = clazz.getMethod("good", null);
		MAnnotation annotation = method.getAnnotation(MAnnotation.class);
		System.out.println(annotation.name()+":"+annotation.age());
	}
	
	@MAnnotation(name="tom",age=20)
	public void good() {
		
	}
}

In the past, we used annotations like this. When two identical annotations are to be annotated on a method, an error will be reported. java8 allows one annotation to be used to store the annotations, and one annotation can be annotated repeatedly

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Repeatable(MAnnotations.class)//Use @ Repeatable to annotate stored annotations
public @interface MAnnotation {
	String name() default "";
	int age();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MAnnotations {
	MAnnotation[] value();
}

public class AnnotataionTest {

	@Test
	public void test() throws Exception {
		Class<AnnotataionTest> clazz=AnnotataionTest.class;
		Method method = clazz.getMethod("good");
		MAnnotation[] mAnnotations = method.getAnnotationsByType(MAnnotation.class);
		for (MAnnotation annotation : mAnnotations) {
			System.out.println(annotation.name()+":"+annotation.age());
		}
	}
	
	@MAnnotation(name="tom",age=20)
	@MAnnotation(name="jack",age=25)
	public void good() {
		
	}
}

Tags: Java

Posted on Sat, 04 Apr 2020 00:03:20 -0700 by jmr3460