目录
一、面对对象快速入门
二、深刻认识面对对象
三、对象在计算机中的执行原理
四、类和对象的一些注意事项
五、this
六、构造器
七、封装
八、实体javaBean
8.1 演示自动生成get set方法
8.2 自动生成构造器
8.3 数据和业务相分离案例
九、面对对象变成综合案例
9.1 设计一个movie实体类
9.2 设计MovieOperator操作类
9.3 TestMovie准备电影数据(main)
十、补充知识:成员变量和局部变量的区别
前言:java是面对对象编程,本内容是入门开始
构造器是一种特殊的方法,特殊在方法名与所在类名一样,而且不能写返回值类型
构造器可以是多个
1、原类
2、右击
3、选择
4、可配合shift
5、完成
1、原类
2、选择
3、下图红色箭头表示选择有参数或者无参数的构造器
4、结果
1、目录结构
2、代码Test、Student代码和StudentOperator代码
3、运行结果
4、代码
Test
package com.runa.object;
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("春天的菠菜");
s1.setScore(100);
System.out.println(s1.getName());
System.out.println(s1.getScore());
StudentOperation operation = new StudentOperation(s1);
operation.printPass();
Student s2 = new Student("lisa",59);
StudentOperation operation2 = new StudentOperation(s2);
operation2.printPass();
}
}
Student
package com.runa.object;
public class Student {
// 私有的成员变量,每个成员变量都提供set 和get方法
private String name;
private double score;
// 无参数的构造方法
public Student() {
}
// 有参数的构造方法
public Student(String name, double score) {
this.name = name;
this.score = score;
}
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;
}
}
StudentOperator
package com.runa.object;
public class StudentOperation {
private Student student;
//无参数的构造方法
public StudentOperation() {
}
//有参数的构造方法
public StudentOperation(Student student) {
this.student = student;
}
//操作方法
public void printPass(){
if(student.getScore() >= 60){
System.out.println(student.getName() +"学生成绩及格");
}else{
System.out.println(student.getName() +"学生成绩不及格");
}
}
}
package com.runa.object;
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.runa.object;
public class MovieOperator {
private Movie[] Movies;
//无参数的构造方法
public MovieOperator() {
}
//有参数的构造方法
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("----------------");
}
}
/**
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());
System.out.println("----------------");
return;
}
}
System.out.println("对不起,没有该电影信息");
}
}
package com.runa.object;
import java.util.Scanner;
public class TestMovie {
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万人想看");
// Movie m1 = new Movie(1,"水门桥",38.9,9.8,"徐克","吴京","12万人想看");
// Movie m2 = new Movie(2,"出拳吧",39,7.8,"唐晓白","田雨","3.5万人想看");
// Movie m3 = new Movie(3,"月球陨落",42,7.9,"罗兰","贝瑞","17.9万人想看");
// Movie m4 = new Movie(4,"一点就到家",35,8.7,"许宏宇","刘昊然","10.8万人想看");
// movies[0] = m1;
// movies[1] = m2;
// movies[2] = m3;
// movies[3] = m4;
// 创建电影操作类对象,接收电影数据,操作业务
// MovieOperator operator = new MovieOperator(movies);
// operator.printAllMovies();
// operator.searchMovieById(1);
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.printAllMovies();
break;
case 2:
//根据id查询某个电影的全部信息
System.out.println("请您输入查询的电影id");
int id = sc.nextInt();
operator.searchMovieById(id);
break;
default:
System.out.println("**************************************");
System.out.println("********您输入的命令有问题!**************");
System.out.println("**************************************");
}
}
}
}