一、需求描述
展示多部电影的信息。
电影信息包括:电影名称、电影得分、电影票价格。
二、代码实现
2.1 Movie类
package com.itheima.collection;public class Movie {//电影名称private String name;//电影得分private int score;//电影票价格private double price;public Movie() {}public Movie(String name, int score, double price) {this.name = name;this.score = score;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public float getScore() {return score;}public void setScore(int score) {this.score = score;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}
}
2.2 打印电影全部信息
package com.itheima.collection;import java.util.ArrayList;
import java.util.Collection;public class CollectionTest4 {public static void main(String[] args) {Collection<Movie> movies = new ArrayList<>();movies.add(new Movie("肖申克的救赎",9,65.9));movies.add(new Movie("热辣滚筒",10,66.9));movies.add(new Movie("夏洛特烦恼",8,45.9));//遍历集合,打印出每部电影信息for (Movie movie : movies) {System.out.println(movie.getName());System.out.println(movie.getScore());System.out.println(movie.getPrice());System.out.println("-----------------------------");}}
}