目录
补充:成员变量和局部变量的区别
面向对象综合案例
设计一个电影类
IDEA快捷操作
设计一个电影操作类
准备电影数据
业务处理
运行结果
补充:成员变量和局部变量的区别
区别 | 成员变量(对象的属性) | 局部变量 |
---|---|---|
类中位置不同 | 类中,方法外 | 常见于方法中 |
初始化值不同 | 有默认值,不需要初始化赋值 | 没有默认值,使用之前必须完成赋值 |
内存位置不同 | 堆内存 | 栈内存 |
作用域不同 | 整个对象 | 在所归属的大括号中 |
生命周期不同 | 与对象共存亡 | 随着方法的调用而生,随着方法的运行结束而亡 |
面向对象综合案例
需求:
- 展示系统中的全部电影(每部电影展示:名称、价格)。
- 允许用户根据电影编号(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
学习自:黑马程序员——JavaSE课程