Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从java程序访问数据时遇到问题_Java_Database_Java 8 - Fatal编程技术网

从java程序访问数据时遇到问题

从java程序访问数据时遇到问题,java,database,java-8,Java,Database,Java 8,我有个愚蠢的问题。我想看看我是否能打印出我这里的餐厅和员工数据,我不记得如何最好地做到这一点 一旦我能弄明白如何做到这一点,我就能够使用它创建方法,但我似乎不记得如何这样做 更新代码 class Main { public static void main(String[] args) { Employee john = new Employee("John","asian",35.00); Employee sam

我有个愚蠢的问题。我想看看我是否能打印出我这里的餐厅和员工数据,我不记得如何最好地做到这一点

一旦我能弄明白如何做到这一点,我就能够使用它创建方法,但我似乎不记得如何这样做

更新代码

class Main {
    public static void main(String[] args) {
        Employee john = new Employee("John","asian",35.00);
        Employee sam = new Employee("Sam","Greek",25.00);
        Employee michael = new Employee("Michael","Italian",50.00);

        Restaurant asian = new Restaurant("Asian","asian",25.00);
        Restaurant greek = new Restaurant("greek","greek",25.00);
        Restaurant italian = new Restaurant("italian","italian",25.00);


    }

    public static class Restaurant {

        private String restaurantName;
        private String cuisine;
        private double price;

        public Restaurant( String restaurantName,
                           String cuisine,
                           double price) {

            this.restaurantName = restaurantName;
            this.cuisine = cuisine;
            this.price = price;
        }

        public String getRestaurantName() {
            return restaurantName;
        }

        public String getCuisine() {
            return cuisine;
        }

        public double getPrice() {
            return price;
        }
    }

    public static class Employee {

        private String employeeName;
        private String cuisine;
        private double budget;

        public Employee(String employeeName,
                        String cuisine,
                        double budget) {

            this.employeeName = employeeName;
            this.cuisine = cuisine;
            this.budget = budget;
        }

        public String getEmployeeName() {
            return employeeName;
        }

        public String getCuisine() {
            return cuisine;
        }

        public double getBudget() {
            return budget;
        }
    }
}

要打印出对象的数据,可以覆盖
toString
方法

在那之后,班级餐厅看起来像这样

public static class Restaurant {

        private String restaurantName;
        private String cuisine;
        private double price;

        public Restaurant( String restaurantName,
                           String cuisine,
                           double price) {
            this.restaurantName = restaurantName;
            this.cuisine = cuisine;
            this.price = price;

        }

        public String getRestaurantName() {
            return restaurantName;
        }

        public String getCuisine() {
            return cuisine;
        }

        public double getPrice() {
            return price;
        }

        @Override
        public String toString() {
            return "Restaurant [restaurantName=" + restaurantName + ", cuisine=" + cuisine + ", price=" + price + "]";
        }
    }
public static class Employee {

        private String employeeName;
        private String cuisine;
        private double budget;

        public Employee(String employeeName,
                        String cuisine,
                        double budget) {

            this.employeeName = employeeName;
            this.cuisine = cuisine;
            this.budget = budget;
        }

        public String getEmployeeName() {
            return employeeName;
        }

        public String getCuisine() {
            return cuisine;
        }

        public double getBudget() {
            return budget;
        }

        @Override
        public String toString() {
            return "Employee [employeeName=" + employeeName + ", cuisine=" + cuisine + ", budget=" + budget + "]";
        }
        
        
    }
这个班的雇员是这样的

public static class Restaurant {

        private String restaurantName;
        private String cuisine;
        private double price;

        public Restaurant( String restaurantName,
                           String cuisine,
                           double price) {
            this.restaurantName = restaurantName;
            this.cuisine = cuisine;
            this.price = price;

        }

        public String getRestaurantName() {
            return restaurantName;
        }

        public String getCuisine() {
            return cuisine;
        }

        public double getPrice() {
            return price;
        }

        @Override
        public String toString() {
            return "Restaurant [restaurantName=" + restaurantName + ", cuisine=" + cuisine + ", price=" + price + "]";
        }
    }
public static class Employee {

        private String employeeName;
        private String cuisine;
        private double budget;

        public Employee(String employeeName,
                        String cuisine,
                        double budget) {

            this.employeeName = employeeName;
            this.cuisine = cuisine;
            this.budget = budget;
        }

        public String getEmployeeName() {
            return employeeName;
        }

        public String getCuisine() {
            return cuisine;
        }

        public double getBudget() {
            return budget;
        }

        @Override
        public String toString() {
            return "Employee [employeeName=" + employeeName + ", cuisine=" + cuisine + ", budget=" + budget + "]";
        }
        
        
    }
然后可以在sysout中打印对象

System.out.println(michael);

必须为每个对象的类使用toString方法()

  • 例如,在Employee类中:
之后,只需在main()方法中使用toString():

在main方法中,还可以使用数组来存储数据并使其更易于访问。然后,为了显示内部的两个对象,可以只使用一个for循环,但我使用了两个for循环来保持输出彼此分离

int numEmployees = 3;
Employee myEmployees[] = new Employee[numEmployees];

myEmployees[0] = new Employee("John","asian",35.00); // Stores John in index 0...
myEmployees[1] = new Employee("Sam","Greek",25.00);
myEmployees[2] = new Employee("Michael","Italian",50.00);

// Displays each object who's index is associated with the value for i
for(int i = 0; i < employees.length; i++)
     System.out.println(myEmployees[i].toString()); 

int numRestaurants = 3;
Restaurant myRestaurants = new Restaurant[numRestaurant]

myRestaurants[0] = new Restaurant("Asian","asian",25.00); // Stores Asain in index 0...
myRestaurants[1] = new Restaurant("greek","greek",25.00);
myRestaurants[2] = new Restaurant("italian","italian",25.00);

// Displays each object who's index is associated with the value for i
for(int i = 0; i < restaurants.length; i++)
     System.out.println(myRestaurants[i].toString());


int numememployees=3;
员工myEmployees[]=新员工[numEmployees];
myEmployees[0]=新员工(“John”,“asian”,35.00);//将John存储在索引0中。。。
myEmployees[1]=新员工(“Sam”,“希腊语”,25.00);
myEmployees[2]=新员工(“迈克尔”,“意大利人”,50.00);
//显示与i的值关联的每个对象的索引
对于(int i=0;i
要做到这一点,您只需要知道三件事:1)您想要打印哪些信息?2) 如何使用
员工
餐厅
上的getter提取出要打印的内容;3)一旦你拿到了资料,如何打印出来。您遇到了什么困难?这取决于您希望如何打印它,您是否考虑过重写Object类的toString()方法@KevinAnderson我已经在类中实现了toString()方法,但是我需要能够打印出信息。最后,我想打印出一份基于预算和价格的建议。但是现在,我只想确保我可以在命令行中打印信息。
System.out.println(…)
响了吗?@KevinAnderson我记得,但出于某种原因,我会对我应该从里面调用什么进行思考。