WordPress数据库错误: [Got error 28 from storage engine]
SHOW FULL COLUMNS FROM `wp_options`

WordPress数据库错误: [Got error 28 from storage engine]
SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category', 'post_tag', 'post_format') AND tr.object_id IN (361) ORDER BY t.name ASC

不学Java8你就Out了!——Java8的default方法详解 – Crazy knowledge base
Crazy knowledge base

不学Java8你就Out了!——Java8的default方法详解

Java 8新增了default方法,它可以在接口添加新功能特性,而且还不影响接口的实现类。下面我们通过例子来说明这一点。

public class MyClass implements InterfaceA {
	public static void  main(String[] args){
	}
	
	@Override
	public void saySomething() {
		// TODO Auto-generated method stub
	}
}

interface InterfaceA{
	public void saySomething();
}

上面的代码显示了MyClass类实现了InterfacesA接口的saySomething()方法。现在我们为InterfacesA接口新增一个sayHi()方法。这么做的话,MyClass类是无法通过编译的,除非我们提供了sayHi()的实现方法。
Default方法是非常有用的,通过在接口定义的方法的访问修饰符前加上关键字default,那么实现类就无需提供该方法的实现了。比如:

public class MyClass implements InterfaceA {
	public static void main(String[] args){
	}
	
	@Override
	public void saySomething() {
		// TODO Auto-generated method stub
	}
}

interface InterfaceA{
	public void saySomething();
	default public void sayHi(){
		System.out.println("Hi");
	}
}

要注意,我们必须提供所有的default方法的实现。因此,default方法使我们的代码更加灵活,在接口中也可以写方法实现了。实现的方法会作为默认的方法实现。

那么,多接口存在冲突该怎么办?

由于Java类可以实现多个接口,那么就可能存在这样的情况:两个或多个接口都有一个同名的default接口方法,从而造成冲突。因为Java虚拟机在程序运行时,并不清楚你要使用哪一个default方法。这会导致编译错误。

让我们来看看下面的例子。

public class MyClass implements InterfaceA,  InterfaceB {
	public static void main(String[] args){
		MyClass mc = new MyClass();
		mc.sayHi();
	}
	
	@Override
	public void saySomething() {
		// TODO Auto-generated method stub
	}
}

interface InterfaceA{
	public void saySomething();
	default public void sayHi(){
		System.out.println("Hi from InterfaceA");
	}
}

interface InterfaceB{
	default public void sayHi(){
		System.out.println("Hi from InterfaceB");
	}
}

它是通不过编译的,会报以下错误:

“Duplicate default methods named sayHi with the parameters () and () are inherited from the types InterfaceB and InterfaceA.”

除非在MyClass类中重写了sayHi()方法:

public class MyClass implements InterfaceA, InterfaceB {
	public static void main(String[] args){
		MyClass mc = new MyClass();
		mc.sayHi();
	}
	
	@Override
	public void saySomething() {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void sayHi(){
		System.out.println("implemetation of sayHi() in MyClass");
	}
}

interface InterfaceA{
	public void saySomething();
	default public void sayHi(){
		System.out.println("Hi from InterfaceA");
	}
}

interface InterfaceB{
	default public void sayHi(){
		System.out.println("Hi from InterfaceB");
	}
}

如果想指定调用哪一个接口的sayHi()方法,我们可以这么做:

public class MyClass implements InterfaceA, InterfaceB {
	public static void main(String[] args){
		MyClass mc = new MyClass();
		mc.sayHi();
	}
	
	@Override
	public void saySomething() {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void sayHi(){
		InterfaceA.super.sayHi();
	}
}

interface  InterfaceA{
	public void saySomething();
	default public void  sayHi(){
		System.out.println("Hi from InterfaceA");
	}
}

interface InterfaceB{
	default public void sayHi(){
		System.out.println("Hi from InterfaceB");
	}
}

答案是不是很简单呢?

不学Java8你就Out了!——Java8的default方法详解

WordPress数据库错误: [Got error 28 from storage engine]
SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (292) ORDER BY t.name ASC

WordPress数据库错误: [Got error 28 from storage engine]
SELECT t.*, tt.* FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('category') AND tr.object_id IN (378) ORDER BY t.name ASC

滚动到顶部