Java大数据开发和安全开发
- (一)Java的变量
- 1.1 成员变量和局部变量的区别
- 1.2 成员变量
- 1.3 局部变量
- 1.4 实体类的案例
(一)Java的变量
1.1 成员变量和局部变量的区别
- 1、类中位置不同:成员变量(类中,方法外)、局部变量(常见于方法中)
- 2、初始化值不同:成员变量(有默认值,不需要初始化赋值),局部变量(没有默认值,使用之前必须完成赋值)
- 3、内存位置不同:成员变量(存在于堆内存),局部变量(内存)
- 4、作用域不同: 成员变量(整个对象),局部变量(在所归属的大括号中)
- 5、生命周期不同:成员变量(与对象同生共死),局部变量(方法调用而生,方法结束而亡)
1.2 成员变量
成员变量在类的里面,但是在方法外面
public class Test2 {
String name; //成员变量
double score;
public void svg(){ //方法
}
}
1.3 局部变量
局部变量是在方法内部定义的
public class Test2 {
String name; //成员变量
double score;
public void svg(){ //方法
int b = 20; //局部变量
}
}
1.4 实体类的案例
面向对象综合案例-模仿电影信息系统
需求
- 展示系统中的全部电影(每部电影展示:名称、价格)。
- 允许用户根据电影编号(id)查询出某个电影的详细信息
设计一个电影类 也是实体类
package com.qianxin.dainying;
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() {
}
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;
}
}
设计一个电影操作类
package com.qianxin.dainying;
public class MovieOperator {
private Movie[] movies;
public MovieOperator(Movie[] movies){
this.movies = movies;
}
/** 1、展示系统全部电影信息 movies =[m1,m2,m3,...]*/
public void printAllMovies() {
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("----------------");
}
}
public void searchMoiveById(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("没有该电影信息~");
}
}
设计主程序
package com.qianxin.dainying;
import java.util.Scanner;
public class Test {
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,"出拳吧",39,7.8,"唐晓白","田雨","3.5万人想看");
movies[2]= new Movie( 3,"月球陨落",42,7.9,"罗兰","贝瑞","17.9万人想看");
movies[3]= new Movie( 4,"一点就到家",35, 8.7, "许宏字","刘吴然","10.8万人");
// 4、创建一部电影操作类的对象,接收电影数据,并对其进行业务处理
MovieOperator operator =new MovieOperator(movies);
operator.printAllMovies();
operator.searchMoiveById(3);
System.out.println("==电影信息系统==");
System.out.println("1、查询全部电影信息");
System.out.println("2、根据id查询某个电影的详细信息展示");
System.out.println("请您输入操作命令:");
Scanner sc=new Scanner(System.in);
int command =sc.nextInt();
switch(command) {
case 1:
// 展示全部电影信息
operator.printAllMovies();
break;
case 2:
// 根据id查询某部电影的详细信息展示
System.out.println("请您输入查询的电影id:");
int id = sc.nextInt();
operator.searchMoiveById(id);
break;
default:
System.out.println("您输入的命令有问题~~");
}
}
}