补充:成员变量和局部变量的区别
面向对象综合案例
需求:
- 展示系统中的全部电影(每部电影展示:名称、价格)。
- 允许用户根据电影编号(id)查询出某个电影的详细信息。
设计一个电影类
public class Movie { private int id; private String name; private double price; private double score; private String director; private String actor; private String info; public Movie(int id, String name, double price, double score, String director, String actor, String info) { this.id = id; this.name = name; this.price = price; this.score = score; this.director = director; this.actor = actor; this.info = info; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public String getDirector() { return director; } public void setDirector(String director) { this.director = director; } public String getActor() { return actor; } public void setActor(String actor) { this.actor = actor; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
IDEA快捷操作
快速生成有参构造器:
快速生成成员变量赋值取值的方法:
设计一个电影操作类
1.展示电影全部信息
public class MovieOperator { public MovieOperator(){ } private Movie[] movies; //建立一个对象数组用来存放多部电影的信息 public MovieOperator(Movie[] movies){ this.movies = movies; } //1.展示电影全部信息 public void printAllMovie(){ System.out.println("-------系统全部电影信息如下-------"); for(int i = 0;i < movies.length;i++){ Movie m = movies[i]; //临时建立一个电影对象用来取到对象数组里面的数据 System.out.println("编号:" + m.getId()); System.out.println("名称:" + m.getName()); System.out.println("价格:" + m.getPrice()); System.out.println("----------------------" ); } } }
2.根据电影的编号查询电影的详细信息并展示
//2.根据电影的编号查询电影的详细信息并展示 public void searchMovieById(int id){ for(int i = 0; i < movies.length;i++){ Movie m = movies[i]; if(m.getId() == id){ System.out.println("该电影详情如下:"); System.out.println("编号:" + m.getId()); System.out.println("名称:" + m.getName()); System.out.println("价格:" + m.getPrice()); System.out.println("得分:" + m.getScore()); System.out.println("导演:" + m.getDirector()); System.out.println("主演:" + m.getActor()); System.out.println("其他信息:" + m.getInfo()); return; //找到了电影信息,就可以直接返回了 } } System.out.println("没有该电影信息"); } }
准备电影数据
随意发挥
public class MovieTest { public static void main(String[] args) { Movie[] movies = new Movie[4]; //存放四部电影的信息 movies[0] = new Movie(1,"水门桥",38.9,9.8,"徐克","吴京","12万人想看"); movies[1] = new Movie(2,"深海",55.5,7.2,"田晓鹏","苏鑫、王亭文","上映第10天,总票房破5亿元"); movies[2] = new Movie(3,"出拳吧",39,7.8,"唐晓白","田雨","3.5万人想看"); movies[3] = new Movie(4,"一点就到家",35,8.7,"许宏宇","刘昊然","10.8万人想看"); } }
业务处理
创建一个电影操作类的对象,接收电影数据,并对其进行业务处理
public class MovieTest { public static void main(String[] args) { Movie[] movies = new Movie[4]; //存放四部电影的信息 movies[0] = new Movie(1,"水门桥",38.9,9.8,"徐克","吴京","12万人想看"); movies[1] = new Movie(2,"深海",55.5,7.2,"田晓鹏","苏鑫、王亭文","上映第10天,总票房破5亿元"); movies[2] = new Movie(3,"出拳吧",39,7.8,"唐晓白","田雨","3.5万人想看"); movies[3] = new Movie(4,"一点就到家",35,8.7,"许宏宇","刘昊然","10.8万人想看"); //创建一个电影操作类的对象,接收电影数据,并对其进行业务处理 MovieOperator operator = new MovieOperator(movies); Scanner sc = new Scanner(System.in); while(true){ System.out.println("===电影信息系统==="); System.out.println("1.查询全部电影信息"); System.out.println("2.根据ID查询某个电影的详细信息显示"); System.out.println("请您输入操作命令:"); int command = sc.nextInt(); switch (command){ case 1: operator.printAllMovie(); break; case 2: System.out.println("请您输入查询的电影id:"); int id = sc.nextInt(); operator.searchMovieById(id); break; default: System.out.println("您输入的命令有问题,请重新输入"); } } } }
运行结果
END