1. 程式人生 > 其它 >【題解】洛谷 P7274 草地 | 20211008 模擬賽【最小生成樹 單調棧 LCT】

【題解】洛谷 P7274 草地 | 20211008 模擬賽【最小生成樹 單調棧 LCT】

本質

本質上就是一個寬泛的抽象類

作用


public interface Shape1 {
 int a=1;
	//系統自動增加成為:
	//   public static final int a=1;
	 double length();
  double area();
  //系統自動新增成為:
	// public abstract double area();
}

使用1

public class Trigangle implements Shape1 {

	//Shape 裡面有a,不能與Shape裡面的變數重名
	int a1;
	int a2;
	int a3;
	public Trigangle()
	{
		this(a,a,a);
	}

	public Trigangle(int a1, int a2, int a3) {
		super();
		this.a1=a1;
		this.a2=a2;
		this.a3=a3;
	}

	public double length() {
		return a1+a2+a3;
	}

	public double area() {
		double p=length()/2;
		return Math.sqrt(p*(p-a1)*(p-a2)*(p-a3));
	}

}

使用2


public class ShapeTest {

	public static void main(String[] args) {
		Shape1 shape;
		shape =new Trigangle();//此時介面相當於上轉型物件
		System.out.println(shape.area());//多型
		System.out.println(shape.length());
		shape =new Y();
		System.out.println(shape.area());
		shape= new C();
		System.out.println(shape.area());
	}
}

優點(比較抽象類)

一個類可以繼承一個類,但可以同時實現(繼承)多個介面

public class Child extends Fathter implements Shape1,Interface1{
}

介面可以繼承多個介面

public interface Interface2 extends Interface1,Shape1{
}