如何使一个 class 中的私有 hashmap 对其他 class 可见

How to make private hashmap in one class visible to other class

所以在我的程序中一切都很好,但我读到在 class 中使变量不是私有的是一个很大的错误,因为它会给大程序的其他部分带来问题。

好吧,我尝试将 HashMap airplane 和 flight 设为私有,但我收到错误消息“字段 Airplane.airplane 不可见”,这当然是正确的。

但是我该如何让它在界面中可见 class?

在此先感谢,我还在学习,我在课程中学习了这一部分。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
        
      Scanner imeskanera = new Scanner(System.in);
      Airplane airplane = new Airplane();
      flight flight = new flight();
      
      interface_aerodrom ui = new interface_aerodrom(imeskanera,airplane,flight);
      
      ui.start();
    }
}

/飞机class

import java.util.HashMap;

public class Airplane {
     HashMap<String,Integer>airplane;
    private String id;
    private int capacity;
    
    public Airplane() {
        this.airplane = new HashMap<String,Integer>();
    }
    
    
    public void add(String id, int capacity) {
        this.id = id;
        this.capacity = capacity;
        this.airplane.put(id, capacity);
        
    }
    public String id() {
        return this.id;
    }
    public int capacity() {
        return this.capacity;
    }

    public String airplaneinfo() {
        return this.id + "( " + this.capacity + " )";
    }
}

/界面class

import java.util.Scanner;

public class interface_aerodrom {
    private Scanner imeskanera;
    private Airplane airplane;
    private flight flight;

    
    
    public interface_aerodrom(Scanner scanner, Airplane airplane,flight flight) {
        
        this.imeskanera = scanner;
        this.airplane = airplane;
        this.flight = flight;
    }
    
    public void start() {
        System.out.println("Airport panel\r\n"
                + "--------------------");
        System.out.println();
        
        while(true) {
            System.out.println("Choose operation:\r\n"
                    + "[1] Add airplane\r\n"
                    + "[2] Add flight\r\n"
                    + "[x] Exit");
            
            String input = this.imeskanera.nextLine();
            input = input.toLowerCase();
            input = input.trim();
            
            if(input.equals("x")) {
                flight_service();
                break;
            }
            else if(input.equals("1")) {
                addairplane();
            }
            else if(input.equals("2")){
                addflight();
            }
            
        }
        }
        
        
        
        public void flight_service() {
            System.out.println("Flight service\r\n"
                    + "------------");
            while(true) {
                System.out.println("Choose operation:\r\n"
                        + "[1] Print planes\r\n"
                        + "[2] Print flights\r\n"
                        + "[3] Print plane info\r\n"
                        + "[x] Quit");
                String input = this.imeskanera.nextLine();
                input = input.toLowerCase();
                input = input.trim();
                if(input.equals("quit")){
                    break;
                }
                else if(input.equals("1")) {
                    for(String name : this.airplane.airplane.keySet()) {
                        int numberofseats = this.airplane.airplane.get(name);
                        String list = name + "( " + numberofseats + " )";
                        System.out.println(list);
                        
                    }
                }
                else if(input.equals("2")){
                    for(String name : this.flight.flight.keySet()) {
                        String value = this.flight.flight.get(name);
                        String list = name + value;
                        System.out.println(list);
                }
                }
                else if(input.equals("3")) {
                    System.out.println("Give plane ID: ");
                    String planeid = this.imeskanera.nextLine();
                    if(airplanecontains(planeid)) {
                        int numberofseats = this.airplane.airplane.get(planeid);
                        System.out.println(planeid + "( " + numberofseats + " )" );
                        
                    } else {
                        System.out.println("That plane is not in our database");
                    }
                }
                        
                
            }
        }
    
        public void addairplane() {
            System.out.println("Give plane ID: ");
            String ID = this.imeskanera.nextLine();
            System.out.println("Give plane capacity: ");
            int capacity = Integer.parseInt(this.imeskanera.nextLine());
            this.airplane.add(ID, capacity);
            
}       
        
        public boolean airplanecontains(String ID) {
            if(this.airplane.airplane.containsKey(ID)) {
                return true;
        }else {
            return false;
        }
            
        }
        
        public void addflight() {
            System.out.println("Give plane ID: ");
            String ID = this.imeskanera.nextLine();
            if(airplanecontains(ID)) {
                System.out.println("Give departure airport code: ");
                String departure = this.imeskanera.nextLine();
                System.out.println("Give destination airport code: ");
                String destination = this.imeskanera.nextLine();
                int seats = this.airplane.airplane.get(ID);
                this.flight.flight.put(ID + " ( " + seats + " ) ",departure + "-" + destination);
                
                    }
            else {  
                System.out.println("This plane is not in our database");
            }
                }
                
            
            
            
            }
        

/航班class

import java.util.HashMap;

public class flight {
    HashMap<String,String>flight;
    
    public flight() {
        this.flight = new  HashMap<String,String>();
    }
    public void add(String departure, String destination) {
        this.flight.put(departure, destination);
    }
    
    
}
       

将字段设为私有并不一定意味着您不能共享它。您可以使用 getter 到 return HashMap.

private Map<String,Integer>airplane = new HashMap<>();

public Map<String,Integer> getAirPlaneMap() {
    return airplane;
}

原因是这隐藏了实现细节并允许在不影响 class 用户的情况下进行未来更改。用户不需要知道地图的来源 class。您可以从您自己和用户不知道的地方检索它。

您可能还想确保用户无法更改它。所以您可以执行以下操作:

public Map<String,Integer> getAirPlaneMap() {
    return Collections.unModifiableMap(airplane);
}

以上将阻止用户添加或删除地图元素。但它不会阻止他们更改从地图中检索到的对象,除非该对象也是不可变的。

一般来说,setter 和 getter 是允许用户 set and retrieve values 的最佳方式。通常最好为他们正在检索的可变项目制作防御性副本,以确保在程序执行期间检索到的值对于所有用户都是一致的。