1. 面向对象的理解
2. 类和对象
类的介绍:
-
Java 中想要创建对象, 必须先要有类的存在
-
类指的是一组相关属性和行为的集合, 我们将其理解为一张对象的设计图
类和对象的关系
-
Java 中需要根据类, 创建对象
-
一个类, 可以创建出多个对象
类的组成
-
属性--->成员变量:
跟之前定义变量的格式一样, 只不过位置需要放在方法的外面
-
行为--->成员方法:
跟之前定义方法的格式一样, 只不过需要去掉static关键字
对象 (类) 的创建
package com.itheima.oop;
/*
前提须知 : Java当中要想创建对象, 必须现有类的存在
类: 一组相关属性和行为的集合, 将其看做为是对象的设计图
对象: 是根据设计图(类), 创建出来的实体
类和对象的关系:
1) 依赖关系: 需要根据类, 创建对象
2) 数量关系: 根据一个类, 可以创建出多个对象
类的组成:
类的本质 : 就是对事物进行的描述
举例1: 我之前有一个{学生}, 叫做<张三>, 今年<23岁>了, <180的身高>, 平时就喜欢(吃饭)和(学习)...
举例2: 前一阵买了一台<白色>的<海尔>{洗衣机}, 花了我<1999块钱>, 老心疼了, 但是(洗衣服)和(甩干)倒是挺方便...
属性: 在代码中使用成员变量表示, 成员变量跟之前定义变量的格式一样, 只不过位置发生了改变, 类中方法外.
行为: 在代码中使用成员方法表示, 成员方法跟之前定义方法的格式一样, 只不过需要去掉 static 关键字.
*/
public class Student {
// 属性: 姓名, 年龄
String name;
int age;
// 行为: 学习, 吃饭
public void study() {
System.out.println("学生学习...");
}
public void eat() {
System.out.println("学生吃饭...");
}
}
对象的使用 (创建实体)
package com.itheima.oop;
public class StudentTest {
/*
创建Student类的对象进行使用
1. 创建对象的格式
类名 对象名 = new 类名();
2. 使用对象成员变量的格式
对象名.成员变量;
3. 使用对象成员方法的格式
对象名.成员方法();
--------------------------------------------
细节:
1) 打印对象名, 可以看到对象的内存地址
com.itheima.oop.Student@4eec7777
全类名 : 包名 + 类名
2) 成员变量就算没有赋值, 也可以直接用, 使用的是对象的默认值
*/
public static void main(String[] args) {
Student stu1 = new Student();
stu1.name = "张三";
stu1.age = 23;
System.out.println(stu1);
System.out.println(stu1.name);
System.out.println(stu1.age);
stu1.study();
stu1.eat();
System.out.println("---------------------");
Student stu2 = new Student();
stu2.name = "李四";
stu2.age = 24;
System.out.println(stu2);
System.out.println(stu2.name);
System.out.println(stu2.age);
stu2.study();
stu2.eat();
}
}
练习-类的定义与对象的创建和使用
package com.itheima.oop;
public class Phone {
String brand;
String color;
int price;
public void call(String name){
System.out.println("给" + name + "打电话");
}
public void sendMessage(){
System.out.println("群发短信");
}
}
package com.itheima.oop;
public class PhoneTest {
public static void main(String[] args) {
Phone p1 = new Phone();
p1.brand = "小米";
p1.color = "白色";
p1.price = 4999;
System.out.println(p1.brand + "---" +p1.color+"---"+p1.price);
Phone p2 = new Phone();
p2.brand = "华为";
p2.color = "黑色";
p2.price = 6999;
System.out.println(p2.brand + "---" +p2.color+"---"+p2.price);
p1.call("张三");
p1.sendMessage();
p2.call("李四");
p2.sendMessage();
}
}
package com.itheima.oop;
public class Book {
String id;
String name;
double price;
public void show(){
System.out.println(name + "的所有属性信息为:");
System.out.println("编号为:" + id + " 书名为:" + name + " 价格为:" + price);
}
}
package com.itheima.oop;
public class BookTest {
public static void main(String[] args) {
Book b1 = new Book();
b1.id = "001";
b1.name = "三国";
b1.price = 88.88;
Book b2 = new Book();
b2.id = "002";
b2.name = "水浒";
b2.price = 88.88;
Book b3 = new Book();
b3.id =" 003";
b3.name = "富婆通讯录";
b3.price = 10000;
b1.show();
b2.show();
b3.show();
}
}
3. 对象内存图
4.成员变量和局部变量
成员变量和局部变量的区别
5.this 关键字
package com.itheima.mthis;
public class Student {
String name;
public void sayHello(String name) {
System.out.println(name); // 西域狂鸭
System.out.println(this.name); //钢门吹雪
this.method();
}
public void method() {
System.out.println("method...");
}
public void print(){
System.out.println("print方法中打印this关键字 ---> " + this);
}
}
package com.itheima.mthis;
public class ThisDemo {
/*
情况: 成员变量和局部变量重名的情况, Java使用的是就近原则
问题: 非要使用成员变量, 怎么办?
解决: 使用this关键字进行区分
this可以区分局部变量和成员变量的重名问题
-----------------------------------------------
this关键字的作用:
this可以调用本类成员 (变量, 方法)
this.本类成员变量
this.本类成员方法();
this.的省略规则 :
本类成员方法 : 没有前提条件, this.可以直接省略
本类成员变量 : 方法中没有出现重名的变量, this.才可以省略
--------------------------------------------------------
this介绍 : 代表当前类对象的引用(地址)
- 谁来调用我, 我就代表谁
- 哪一个对象调用方法, 方法中的this, 代表的就是哪一个对象.
stu1.print() ----> this ---> stu1的地址
stu2.print() ----> this ---> stu2的地址
*/
public static void main(String[] args) {
Student stu1 = new Student();
System.out.println(stu1);
stu1.print();
Student stu2 = new Student();
System.out.println(stu2);
stu2.print();
}
}
6.构造方法
构造方法概述: 又名构造器
-
构造器
-
初始化一个新建的对象
-
构建、创造对象的时候, 所调用的方法
-
-
格式
1. 方法名与类名相同, 大小写也要 一致
2. 没有返回值类型, 连void都没有
3. 没有具体的返回值 (不能由return带回结果数据)
-
执行时机
1. 创建对象的时候调用, 每创建一次对象, 就会执行一次构造方法
2. 不能手动调用构造方法 (即不能用对象名调用)
构造方法的作用
-
本质作用: 创建对象
-
结合构造方法执行时机: 给对象中的属性 (成员变量) 进行初始化
package com.itheima.constructor;
public class Student {
String name;
int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
package com.itheima.constructor;
public class StudentTest {
/*
构造方法 (构造器) :
创建对象的时候, 要执行的方法
格式:
1. 方法名与类名相同, 大小写也要一致
2. 没有返回值类型, 连void都没有
3. 没有具体的返回值 (不能由return语句带回结果数据)
构造方法的执行时机 :
在创建对象的时候, 被调用执行
每创建一次对象, 就会执行一次构造方法.
构造方法的作用:
1. 本质的作用 : 创建对象
2. 结合执行时机 : 可以创建对象的时候, 给对象中的数据初始化
构造方法的注意事项:
1. 一个类中, 没有编写构造方法, 系统将会提供一个 [默认的] [无参数] 的构造方法
2. 一个类中, 如果手动编写了构造方法, 系统将不会再提供那个默认的无参构造方法
* 建议 : 编写类的时候, 无参构造, 代餐构造, 全部手动给出
3. 构造方法不允许手动调用
*/
public static void main(String[] args) {
Student stu1 = new Student("张三", 23);
System.out.println(stu1.name + "---" + stu1.age);
Student stu2 = new Student("李四", 24);
System.out.println(stu2.name + "---" + stu2.age);
System.out.println(stu1.name + "---" + stu1.age);
Student stu3 = new Student();
}
}
注意事项
1. 构造方法的创建
-
如果没有定义构造方法, 系统将给出一个默认的无参数构造方法
-
如果定义里构造方法, 系统将不再提供默认的构造方法
2.构造方法的重载
-
构造方法也是方法, 允许重载关系的出现
3. 推荐的使用方式
-
无参数构造方法, 和带参数构造方法, 都自己手动给出
构造方法的执行流程
7.封装
-
面向对象的三大特征:
封装、继承、多态
-
封装:
使用类设计对象时, 将需要处理的数据以及处理这些数据的方法, 设计到对象中
-
优点:
1. 更好的维护数据
2. 使用者无需关心内部实现, 只要知道如何使用即可
封装的设计规范
权限修饰符
-
private
-
defalut
-
protected
-
public
package com.itheima.encapsulation;
/*
1. 私有成员变量 (为了保证数据的安全性)
2. 针对私有的成员变量, 提供对应的setXxx和getXxx方法
set : 设置
get : 获取
*/
public class Student {
private int age;
public void setAge(int age) {
if (age >= 1 && age <= 120) {
this.age = age;
}else{
System.out.println("年龄有误, 请检查是否在1~120之间");
}
}
public int getAge(){
return age;
}
}
package com.itheima.encapsulation;
public class StudentTest {
/*
封装的设计规范 : 合理隐藏, 合理暴露
*/
public static void main(String[] args) {
Student stu = new Student();
stu.setAge(230);
int age = stu.getAge();
System.out.println(age);
}
}
标准 JavaBean
-
JavaBean : 实体类, 封装数据的类.
-
实体类的应用场景: 实体类只负责数据存取, 而对数据的处理交给其他类来完成, 以实现数据和数据业务处理相分离.
-
标准的JavaBean:
1. 针对类中的成员变量私有,提供对应的setXxx和getXxx方法
2. 类中提供无参, 带参构造方法.
鼠标右键点击/generate/Getter and Setter (或直接右键PTG)
package com.itheima.domain;
public class Student {
// 1. 成员变量私有化
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
}
面向对象综合案例-模仿电影信息系统
1. 构造Movie类及其属性和相应构造方法
package com.itheima.domain;
public class Movie {
private int id;
private String title;
private String time;
private double score;
private String area;
private String type;
private String director;
private String starring;
public Movie() {
}
public Movie(int id, String title, String time, double score, String area, String type, String director, String starring) {
this.id = id;
this.title = title;
this.time = time;
this.score = score;
this.area = area;
this.type = type;
this.director = director;
this.starring = starring;
}
/**
* 获取
* @return id
*/
public int getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取
* @return title
*/
public String getTitle() {
return title;
}
/**
* 设置
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* 获取
* @return time
*/
public String getTime() {
return time;
}
/**
* 设置
* @param time
*/
public void setTime(String time) {
this.time = time;
}
/**
* 获取
* @return score
*/
public double getScore() {
return score;
}
/**
* 设置
* @param score
*/
public void setScore(double score) {
this.score = score;
}
/**
* 获取
* @return area
*/
public String getArea() {
return area;
}
/**
* 设置
* @param area
*/
public void setArea(String area) {
this.area = area;
}
/**
* 获取
* @return type
*/
public String getType() {
return type;
}
/**
* 设置
* @param type
*/
public void setType(String type) {
this.type = type;
}
/**
* 获取
* @return director
*/
public String getDirector() {
return director;
}
/**
* 设置
* @param director
*/
public void setDirector(String director) {
this.director = director;
}
/**
* 获取
* @return starring
*/
public String getStarring() {
return starring;
}
/**
* 设置
* @param starring
*/
public void setStarring(String starring) {
this.starring = starring;
}
}
2. 构造电影服务业务逻辑, 实现业务功能
package com.itheima.test;
import com.itheima.domain.Movie;
import java.util.Scanner;
public class MovieService {
private Movie[] movies;
private Scanner sc = new Scanner(System.in);
public MovieService(Movie[] movies) {
this.movies = movies;
}
/**
* 启动电影信息管理系统
*/
public void start() {
lo:
while (true) {
System.out.println("----------电影信息系统----------");
System.out.println("请输入您的选择:");
System.out.println("1. 查询全部电影信息");
System.out.println("2. 根据id查询电影信息");
System.out.println("3. 退出");
int choice = sc.nextInt();
switch (choice){
case 1:
queryMovieInfos();
break;
case 2:
queryMovieInfoById();
break;
case 3:
System.out.println("感谢您的使用, 再见!");
break lo;
default:
System.out.println("您的输入有误, 请检查");
break;
}
}
}
/**
* 此方法根据电影编号, 查询电影详细信息
*/
private void queryMovieInfoById() {
// 1. 键盘录入用户输入的编号
System.out.println("请输入您要查询的电影编号:");
int id = sc.nextInt();
// 2. 遍历数组, 从数组中查询电影信息
for (int i = 0; i < movies.length; i++) {
Movie movie = movies[i];
if(movie.getId() == id){
// 3. 将找到的电影信息, 打印在控制台
System.out.println(movie.getId()+"---"+movie.getTitle()+"---"+movie.getTime()
+"---"+movie.getScore()+"---"+movie.getArea()+"---"+movie.getType()
+"---"+movie.getDirector()+"---"+movie.getStarring());
return;
}
}
// 代码走到这里, 说明没找到
System.out.println("您输入的编号不存在, 请检查!");
}
/**
* 展示系统中全部的电影 (名称, 评分)
*/
private void queryMovieInfos() {
// 1. 遍历数组, 取出每一电影对象
for (int i = 0; i < movies.length; i++) {
Movie movie = movies[i];
// System.out.println(movie); 打印对象名, 会看到内存地址
// 2. 通过电影对象, 调用内部getXxx方法, 获取信息并打印
System.out.println(movie.getTitle() + "---" + movie.getScore());
}
}
}
3. 在主方法中录入电影信息, 并调用各方法实现电影系统功能
package com.itheima.test;
import com.itheima.domain.Movie;
public class Test {
public static void main(String[] args) {
Movie movie1 = new Movie(1, "东八区的先生们", "2022", 2.1, "中国大陆", "剧情 喜剧", "夏睿", "张翰 王晓晨");
Movie movie2 = new Movie(2, "上海堡垒", "2019", 2.9, "中国大陆", "爱情 战争 科幻", "滕华涛", "鹿晗 舒淇");
Movie movie3 = new Movie(3, "纯洁心灵·逐梦演艺圈", "2015", 2.2, "中国大陆", "剧情 喜剧", "毕志飞", "朱一文 李彦漫");
Movie[] movies = {movie1,movie2,movie3};
// 该如何将一个类中的数据, 传递给另一个类
MovieService movieService = new MovieService(movies);
movieService.start();
}
}