0
点赞
收藏
分享

微信扫一扫

java接口继承 需要注意的一些细节

小a草 2022-04-05 阅读 45
java

1.在实现接口的时候应当写出public访问控制符。因为接口中的方法都默认是public,而现在我实现它(说明我是它子类 继承了这个抽象类),子类的访问控制范围不能小于父类。如果不写public,那就是friendly,那就只能同一个包中才能访问,这不符合“子类访问范围要大于等于父类”的要求。因此以下:

报错原因:cannot reduce the visibility of the inherited method of InterfaceA6

2.

想要New一个interface接口的时候,不能直接new interface,报错信息“can not instantiate the type InterfaceA”。

以及,接口中只能有方法特征而不能有方法的实现。

所以以下:

报错原因:abstract methods do not specify a body

(下一面第一张图我想写一个{}函数体 所以报错了)

 ---------------分割线

它居然嫌我文章质量不佳 

我先随便贴个代码 糊弄一下

 

package BFS;
/**蓝桥杯 迷宫 A2019 我终于tmd写出来了!!!!**/
//import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**A2019迷宫**/
public class maze3 {

	static int count =0;
	static char[][] maze;
	static boolean[][] visited;
	static int n=30;
	static int m=50;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		maze = new char[n][m];
		visited = new boolean[n][m];
		String[] temp = new String[m];
		for(int i = 0; i<n; i++) {
			temp[i] = sc.nextLine();
		}
		sc.close();
		for(int i=0; i<n; i++) {
			maze[i] = temp[i].toCharArray();
		}
		//		System.out.println(Arrays.toString(maze[0]));

		int[][] dir = {{1,0},{0,-1},{0,1},{-1,0}};
		char[] d = {'D','L','R','U'};
		Node start = new Node(0,0);//what?
		//		Node end = new Node(29,49);
		Queue<Node> q = new LinkedList<>();

		char[][] rec = new char[n][m];
		q.add(start);
		visited[0][0] = true;

		while(!q.isEmpty()) {
			int sz = q.size();
			for(int i=1; i<=sz; i++) {
				Node head = q.poll();
				int hx = head.x;
				int hy = head.y;
//				visited[hx][hy] = true;
				
				if(hx == n-1 && hy == m-1) {
//					System.out.println(Arrays.toString(rec));
//					break;
//					return;
//					for(int p=0; p<n; p++) {
//						System.out.println(Arrays.toString(rec[p]));
//					}
					traceback(rec);
					return;
				}

				for(int j=0; j<4; j++) {
					Node next = new Node(hx+dir[j][0],hy+dir[j][1]);
					if(/*visited[next.x][next.y] == false && */test(next.x, next.y)) {
						visited[next.x][next.y] = true;
						q.offer(next);
//						rec.offer(d[j]); 不可以这么写 因为这是分叉路 它把所有的能走的方向
//                                都保存了,应该从终点往起点回溯
						rec[next.x][next.y] = d[j];
					}
				}
			}

		}
		


	}
	
	public static void traceback(char[][] rec) {
		//从终点往前回溯
		StringBuffer sb = new StringBuffer();

		int i=n-1, j=m-1;
		while(!(i == 0 && j == 0)) {
			sb.append(String.valueOf(rec[i][j]));
			char temp = rec[i][j];
			if(temp == 'R') {		
				j--;
			}
			if(temp == 'D') {
				i--;
			}
			if(temp == 'L') {
				j++;
			}
			if(temp == 'U') {
				i++;
			}
		}

		System.out.println(sb.reverse());
	}
	

	public static boolean test(int x, int y) {
		if(x<0 || x>n-1 || y<0 || y>m-1) {//先判断它的下标有无越界
			return false;
		}
		if(maze[x][y] == '1') {
			return false;
		}
		if(visited[x][y] == true) { //最后再写这句判断 为了防止它先出现下标越界的问题
//                否则如果先写这句的话 万一下标越界了 那这句判断首先就无法成立 就会报错
			return false;
		}
		return true;
	}

	public static class Node{
		int x;
		int y;
		Node(int x, int y){
			this.x = x;
			this.y = y;
		}
	}

}
举报

相关推荐

0 条评论