需求:某影院系统需要在后台存储上述三部电影,然后依次展示出来
分析:
-
定义一个电影类,定义一个集合存储对象。
-
创建3个电影对象,封装相关数据,把3个对象存入到集合中去。
-
遍历集合中的3个对象,输出相关信息。
Test类
package com.itheima.d4_collection_object; import java.util.ArrayList; import java.util.Collection; public class TestDemo { public static void main(String[] args) { //1、定义一个电影类 //2、定义一个集合对象存储3部电影 Collection<Movie> movies = new ArrayList<>(); movies.add(new Movie("《你好王文军》",9.5,"王文军")); movies.add(new Movie("《王文军探案》",6.5,"王军")); movies.add(new Movie("《刺杀王文军》",9.9,"王文")); System.out.println(movies); for (Movie movie : movies){ System.out.println("片名:"+movie.getName()); System.out.println("得分:"+movie.getScore()); System.out.println("主演:"+movie.getActor()); System.out.println("-----------------------------"); } } }
Movie类
package com.itheima.d4_collection_object; public class Movie { private String name; private double score; private String actor; public Movie() { } public Movie(String name, double score, String actor) { this.name = name; this.score = score; this.actor = actor; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public String getActor() { return actor; } public void setActor(String actor) { this.actor = actor; } @Override public String toString() { return "Movie{" + "name='" + name + '\'' + ", score=" + score + ", actor='" + actor + '\'' + '}'; } }
堆栈内存分析
标签:String,Movie,actor,案例,score,collection,public,name From: https://www.cnblogs.com/liandaozhanshi/p/17492163.html