【问题标题】:three codes need to be combined in one single code [closed]三个代码需要组合在一个代码中[关闭]
【发布时间】:2015-09-25 11:46:39
【问题描述】:

如下所示,我有三个代码,我不知道如何将它们组合成一个代码,因此我可以运行它并打印出书“9780345917430”是否在列表中

import java.util.ArrayList;

import java.util.List;

public class Book {

    private String isbn;
    private String title;
    private String author;

    public Book(String isbn, String title, String author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
    }

    public String toString() {
        return author + ": " + title;
    }

    public boolean equals(Object x){
        if (x instanceof Book){
            return isbn.equals(((Book) x).isbn);
        } else {
            return false;
        }
    }

}

我有一个清单

List<Book> list = new ArrayList<Book>();

我有一个 if 语句

if (list.contains(new Book("9780345917430", null, null))) {
    System.out.println("Boken finns");
}

我正在考虑将列表放在public class Book { 下,我想将 if 语句放在 main 方法中,但由于public class Book { 而我无法做到这一点的问题涉及public 如何将所有这些组合在一个代码中以便我可以在 Eclipse 中运行它??? 谢谢

【问题讨论】:

    标签: java eclipse class


    【解决方案1】:

    Eclipse 或任何 IDE 都可以一次编译整个项目。只需创建单独的文件,然后运行具有public static void main(String[] args) { 方法的类。

    示例: BookExample.java

    public class BookExample {
        private static List<Book> list = new ArrayList<Book>();
    
        public static void main(String[] args) {
            if (list.contains(new Book("9780345917430", null, null)) {
                System.out.println("Boken finns");
            }
        }
    }
    

    Book.java

    import java.util.ArrayList;
    
    import java.util.List;
    
    public class Book {
    
        private String isbn;
        private String title;
        private String author;
    
        public Book(String isbn, String title, String author) {
            this.isbn = isbn;
            this.title = title;
            this.author = author;
        }
    
        public String toString() {
            return author + ": " + title;
        }
    
        public boolean equals(Object x){
            if (x instanceof Book){
                return isbn.equals(((Book) x).isbn);
            } else {
                return false;
            }
        }
    
    }
    

    【讨论】:

    • 嗨,看来我必须从 public class Book { 中删除 public 否则它将无法正常工作..谢谢
    【解决方案2】:
      import java.util.ArrayList;
    
    import java.util.List;
    
    public class Book {
    
    private String isbn;
    private String title;
    private String author;
    
    public Book(String isbn, String title, String author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
    }
    
    public String toString() {
        return author + ": " + title;
    }
    
    public boolean equals(Object x){
        if (x instanceof Book){
            return isbn.equals(((Book) x).isbn);
        } else {
            return false;
        }
    }
    

    同一个类中的另一种方法

     public static void main(String[] args){
         List<Book> books = new ArrayList<Book>();
         if (list.contains(new Book("9780345917430", null, null))) {
         System.out.println("Boken finns");
       }
    }
    

    【讨论】:

    • 嗨,看来我必须从 public class Book { 中删除 public 否则它将无法正常工作..谢谢
    • 不需要删除,为什么要删除'pubic'修饰符?。
    • 如果我有public class Book { ,代码不会运行我认为是因为我的文件名应该与主方法的名称相同,,在我的情况下,我有两个public class Book { 和@ 987654327@(用于主方法)...所以类名必须与主方法的名称相同,否则代码会混淆,因为它无法确定哪个public class Book { public class BookEx { 是要执行的主方法..谢谢
    • 公共类名和文件名应该相同,'main'方法名和类名不需要相同。实时项目中的另一件事可能有 1000 个类,而在这些类中,大多数都是公开的
    【解决方案3】:

    你可以试试这个

    import java.util.ArrayList;
    
    import java.util.List;
    
    public class Book {
    
        private String isbn;
        private String title;
        private String author;
    
        public static List<Book> list = new ArrayList<Book>();
    
        public Book(String isbn, String title, String author) {
            this.isbn = isbn;
            this.title = title;
            this.author = author;
        }
    
        public String toString() {
            return author + ": " + title;
        }
    
        public boolean equals(Object x){
            if (x instanceof Book){
                return isbn.equals(((Book) x).isbn);
            } else {
                return false;
            }
        }
    
        public static void main(String arg[])
        {
    
        if (list.contains(new Book("9780345917430", null, null))) 
        {
                System.out.println("Boken finns");
        }
        }
    }
    

    【讨论】:

    • 嗨,看来我必须从 public class Book { 中删除 public 否则它将无法正常工作..谢谢
    【解决方案4】:

    你只需要从下面的其他类的主函数中调用它

            public class classname {
            List<Book> list = new ArrayList<Book>();
            public static void main(String[] args) {
                if (list.contains(new Book("9780345917430", null, null))) {
                    System.out.println("Boken finns");
                }
            } 
        }
    

    【讨论】:

    • 嗨,看来我必须从 public class Book { 中删除 public 否则它将无法正常工作..谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多