Java-类-对象 - 对象内存图 成员变量和局部变量的不同 this关键字 构造方法 封装 权限修饰符 标准javaBean20

package com.demo.css;
public class css01 {
// java要创造对象,必须要有类的存在
// 类:一组相关属性和行为的集合,看做是对象的设计图
// 对象:是根据设计图,创造出来的实体
// 类和对象的关系:
// 1.依赖关系:根据类,创造对象
// 2.数量关系:一个类可以创造多个对象
// 类的形成:
// 类的本质:对事物进行的描述
// 属性:在代码中使用成员变量表示,成员变量跟之前定义的格式一样,只不过位置发生了变化,类方法外
// 行为:在代码中使用成员方法去表示,成员方法和之前定义的方法一样,只不过要去掉static关键字
// 1.创造对象的格式:
// 类名 对象名 = new 类名(); 比如:int arr[] = new int[];
// 2使用对象成员变量的格式
// 对象名.成员变量
// 3使用对象成员方法的格式
// 对象名.成员方法
String name = "张三";
int age = 18;
}
package com.demo.css;
public class css02 {
public static void main(String[] args) {
css01 css = new css01();
System.out.println(css.name);
System.out.println(css.age);
}
}
练习:
package com.demo.css;
public class phone {
String brand;
String color;
int price;
public void call(String name) {
System.out.println("给" + name + "打电话");
}
public void Mess() {
System.out.println("群发短信");
}
}
package com.demo.css;
public class phoneCss {
public static void main(String[] args) {
phone ph = new phone();
ph.brand = "小米";
ph.color = "白色";
ph.price = 99;
System.out.println(ph.brand+"--------"+ph.color+"-----"+ph.price);
phone ph2 = new phone();
ph2.brand = "红米";
ph2.color = "黑色";
ph2.price = 9999;
System.out.println(ph2.brand+"--------"+ph2.color+"-----"+ph2.price);
ph.call("张三");
ph.Mess();
ph2.call("李四");
ph2.Mess();
}
}
图书案例:
package com.demo.css;
public class book {
String id;
String name;
double price;
public void show(String id,String name,double price) {
System.out.println(id+name+price);
}
}
package com.demo.css;
public class Bookcss {
public static void main(String[] args) {
book bk = new book();
bk.id = "001";
bk.name = "三国";
bk.price = 88.00;
bk.show(bk.id,bk.name,bk.price);
book bk2 = new book();
bk2.id = "002";
bk2.name = "水浒";
bk2.price = 88.00;
bk2.show(bk2.id,bk2.name,bk2.price);
book bk3 = new book();
bk3.id = "003";
bk3.name = "富婆通讯录";
bk3.price = 88.00;
bk.show(bk3.id,bk3.name,bk3.price);
}
}
对象内存图:
单个对象内存图:

两个对象内存图:
数据空间都是独立的,互不打扰,也叫对象引用共同图

两个引用指向内存图

成员变量和局部变量的不同:
1类中编写位置的不同:


this关键字:



构造方法:



package com.demo.structure;
public class stu {
// 构造方法:创造对象时,要执行的方法
// 格式:
// 1方法名与类名一致,大小写也一致
// 2.没有返回类型,连void也不能有
// 3.没有具体的返回值()不能return 返回结果
// 执行时机:
// 创建对象时执行
// 每创建一次对象,就会执行一次
// 作用:
// 本质作用:创建对象
// 执行时机:可以创建对象时,给对象的数据初始化
// 注意事项:
// 一个类中,没有编写构造方法,系统会提供一个[默认的][无参数]的构造方法
// 一个类中,手动编写了构造方法,系统不会默认提供那个方法
public static void main(String[] args) {
stuDemo stu = new stuDemo();
System.out.println(stu);
}
}
package com.demo.structure;
public class stuDemo {
public stuDemo(){
System.out.println("我是stuDemo的构造方法");
}
}
封装:



权限修饰符:
private:访问权限很低,只能在同一个类中访问,使用会,在另一个类使用会爆红
(default):同一个类,同一个包中
protected:同一个类,同一个包中,不同包的种类
public:任意位置访问
package com.demo.structure;
public class authority {
//私有成员变量,为了安全性
private int age;
public void setAge(int age) {
if(age>= 1 &&120>=age){
this.age = age;
}else {
System.out.println("输入有误");
}
}
public int getAge() {
return age;
}
}
package com.demo.structure;
public class authorityDome {
public static void main(String[] args) {
/*
权限修饰符:
private:访问权限很低,只能在同一个类中访问,使用会,在另一个类使用会爆红
(default):同一个类,同一个包中
protected:同一个类,同一个包中,不同包的种类
public:任意位置访问
*/
authority sut = new authority();
sut.setAge(199);
int age = sut.getAge();
System.out.println(age);
}
}
标准javaBean:
javaBean:实体类:封装数据的类
搜索pgt,安装

电影的案例:
package com.demo.film;
//1.这个类只对数据做封装,不做业务处理
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;
}
}
package com.demo.test;
//2.这个类做一个电影启动的封装
import com.demo.film.movie;
import java.util.Scanner;
public class movieServe {
private movie movies[];
private Scanner sc = new Scanner(System.in);
public movieServe(movie[] movies) {
this.movies = movies;
System.out.println(this.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:
Querymovieinformation();
break;
case 2:
QuerymovieinforByid();
break;
case 3:
System.out.println("退出");
break lo;
default:
System.out.println("您的输入有误,请检查");
break;
}
}
}
// 根据id查询电影信息
private void QuerymovieinforByid() {
System.out.println("请输入您要查询的电影编号:");
int id = sc.nextInt();
for (int i = 0; i < movies.length; i++) {
movie movie = movies[i];
if (movie.getId() ==id){
System.out.println(movie.getId()+"---"+movie.getTitle()+"---"+movie.getScore()+"---"+movie.getTime()+"---"+movie.getArea()+"---"+movie.getDirector()+movie.getStarring());
return;
}
}
System.out.println("您所输入的编号不存在");
}
// 查询全部电影信息
private void Querymovieinformation() {
for (int i = 0; i < movies.length; i++) {
movie movie = movies[i];
System.out.println(movie.getTitle()+"---"+movie.getScore());
}
}
}
package com.demo.test;
import com.demo.film.movie;
//3.这个类做业务的处理
public class movieTest {
public static void main(String[] args) {
movie movie1 = new movie(1,"上海堡垒2","2023",2.3,"中国大陆","恶搞 科幻","华涛","鹿晗");
movie movie2 = new movie(2,"老八秘制小汉堡","2023",2.3,"中国大陆","恶搞 科幻","华涛","鹿晗");
movie movie3 = new movie(3,"纯洁心灵","2023",2.3,"中国大陆","恶搞 科幻","华涛","鹿晗");
movie movies[] = {movie1,movie2,movie3} ;
movieServe movieServe = new movieServe(movies);
movieServe.start();
}
}
代码改变了我们,也改变了世界

浙公网安备 33010602011771号