java - 如何使用main()中的对象数组访问类私有(private)成员?

原文 标签 java class oop object private-members

我有一部带4名私人成员的电影。我想使用Movie Class对象数组访问main()中的这个私有成员。我在电影课中有4名成员,分别是电影名称,电影导演,电影演员,电影收入。我创建了一个参数化的构造函数,该构造函数具有4个参数和一个to-String方法来显示此类成员的详细信息。现在,我想将电影收入从高到低排序,并根据该收入显示电影名称。

class Movie {
    private String movieName;
    private String movieDirector;
    private String movieActor;
    private double movieEarning;

    Movie(String movieName, String movieDirector, String movieActor, double movieEarning) {
        this.movieName = movieName;
        this.movieDirector = movieDirector;
        this.movieActor = movieActor;
        this.movieEarning = movieEarning;
    }

    public String toString() {
        return "\n" + "Movie Name = " + movieName + "\n" + "Movie Director =" + movieDirector + "\n" + "Movie Actor = " + movieActor + "\n" + "Movie Earning = " + movieEarning;
    }
    public void movieData() {
        //Print movie name where director = Rohit Shetty
        if (movieDirector.equals("Rohit Shetty") || movieDirector.equals("rohit shetty"))
            System.out.println("Movie Name=" + movieName);
        //Print director name who earned between 300 - 500
        if (movieEarning > 300 && movieEarning < 500)
            System.out.println("Print Director Names who earns more than 300 crores= " + movieDirector);
        //print moive details where actor = salman khan && director=kabir khan
        if (movieActor.equals("Salman Khan") && movieDirector.equals("kabir khan")) {
            System.out.println("Print Movie Names = " + movieName);
            System.out.println("Print Movie Earning = " + movieEarning);
        }
        //print movie name whose actor name starts with s
        if (movieActor.startsWith("s"))
            System.out.println("Print Actor Name where movie name starts with 's' = " + movieName);

        //print movie name with order of highest earning to lowest earning
    }


}

public class MovieDemo2 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String movieName;
        String movieDirector;
        String movieActor;
        double movieEarning;
        Movie[] movieObj;
        movieObj = new Movie[6];
        for (int i = 1; i < movieObj.length; i++) {
            System.out.print("Enter " + i + " movie name :");
            movieName = sc.next();
            sc.nextLine();
            System.out.print("Enter " + i + " movie director name :");
            movieDirector = sc.next();
            sc.nextLine();
            System.out.print("Enter " + i + " movie actor: ");
            movieActor = sc.next();
            sc.nextLine();
            System.out.print("Enter " + i + " movie earning: ");
            movieEarning = sc.nextDouble();
            sc.nextLine();
            movieObj[i] = new Movie(movieName, movieDirector, movieActor, movieEarning);
            System.out.println(movieObj[i]);

        }
        for (int i = 1; i < movieObj.length; i++) {
            movieObj[i].movieData();
        }
    }

}


我希望输出为当我六次输入电影的收入时,然后我将按照电影的最高收入到最低最低收入的顺序显示电影名称。例如,我输入的值为400,500,200,800,100,那么这给了我一份收入从高到低的电影名称列表。

最佳答案

在这里,我试图演示如何对数组进行排序。我已经用Arrays.sort()了。

请注意,Movie类实现了java.lang.Comparable接口。因此,Movie类必须实现Comparable的方法compareTo(),我在这里已经完成了。

import java.util.Arrays;

public class SortMovies
{
  public static void main(String[] args)
  {
    Movie[] movies = new Movie[6];
    movies[0] = new Movie("Movie 1", "Director 1", "Actor 1", 100.5);
    movies[1] = new Movie("Movie 2", "Director 2", "Actor 2", 300.5);
    movies[2] = new Movie("Movie 3", "Director 3", "Actor 3", 200.5);
    movies[3] = new Movie("Movie 4", "Director 4", "Actor 4", 700.5);
    movies[4] = new Movie("Movie 5", "Director 5", "Actor 5", 500.5);
    movies[5] = new Movie("Movie 6", "Director 6", "Actor 6", 400.5);

    // Assuming that you have built this movies array somehow

    // Sort
    Arrays.sort(movies);

    // Print sorted array
    for (Movie m : movies)
    {
      System.out.println(m);
    }
  }
}

class Movie implements Comparable<Movie>
{
  private String movieName;
  private String movieDirector;
  private String movieActor;
  private double movieEarning;

  Movie(String movieName, String movieDirector, String movieActor, double movieEarning)
  {
    this.movieName = movieName;
    this.movieDirector = movieDirector;
    this.movieActor = movieActor;
    this.movieEarning = movieEarning;
  }

  @Override
  public String toString()
  {
    return "\n" + "Movie Name = " + movieName + "\n" + "Movie Director =" + movieDirector + "\n" + "Movie Actor = " +
        movieActor + "\n" + "Movie Earning = " + movieEarning;
  }

  @Override
  public int compareTo(Movie otherMovie)
  {
    if (this.movieEarning > otherMovie.movieEarning)
    {
      return -1;
    }
    else if (this.movieEarning < otherMovie.movieEarning)
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
}

关于java - 如何使用main()中的对象数组访问类私有(private)成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54502152/

相关文章:

c++ - 覆盖抽象类的属性

php - 我想知道如何在所有新对象的类中都保持相同的库存变量?

java - 将JPanel数组添加到容器

java - 字符串中是否有数字?

c++ - 其他类无法识别的C++类属性(标题,包括公共(public))

c++ - 具有自身无序集的类

java - java.lang.NoClassDefFoundError:Lorg/slf4j/Logger

java - @SessionAttributes提供HttpSessionRequiredException

java - 在主类方法中找不到字符串方法

c++ - 类中 vector 的C++ vector