创建两个对象打印小明小红的总成绩与平均成绩
package com.itheima.object;
public class Text {
public static void main(String[] args) {
//创建一个学生对象,封装小红的数据
Student s1=new Student();
s1.name="小红";
s1.math=100;
s1.chinese=96;
s1.printTotalScore();
s1.printAverageScore();
//再创建一个学生对象,封装小明的数据
Student s2=new Student();
s2.name="小明";
s2.chinese=59;
s2.math=90;
s2.printTotalScore();
s2.printAverageScore();
}
}
package com.itheima.object;
public class Student {
String name;
double chinese;
double math;
public void printTotalScore(){
System.out.println(name+"的总成绩是:"+(chinese+math));
}
public void printAverageScore(){
System.out.println(name+"的平均成绩是:"+(chinese+math)/2.0);
}
}
对象在计算机中的执行原理:
Student s1=new Student();
每次new Student(),就是在堆内存中开辟一块内存区域代表一个学生
s1变量里面记住的是学生对象的地址
1.如何识别引用类型的变量?
Student s1=new Student();
s1变量中存储的是对象的地址,因此变量s1也称为引用类型的变量
注意:
1.类名建议用英文单词,首字母大写,满足驼峰模式,且要有意义,比如Student、Car
2.类中定义的变量也称为成员变量,类中定义的方法也称为成员方法
3.成员变量本身存在默认值,定义成员变量的时候一般来说不需要赋初始值
4.一个代码文件中,可以写多个class类,但只能用一个public修饰,且public修饰的类名必须成为代码文件名
5.对象与对象之间的数据不会相互影响,但多个变量指向同一个对象时就会相互影响了
多个变量指向同一个对象:
package com.itheima.object;
public class Text2 {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "xiaoming";
Student s2 = s1;
s2.name="xiaohong";
System.out.println(s1.name);
}
}
将s1赋值给了s2,此时s1与s2是同一个地址,在给s2赋值“xiaohong”时,该地址为被赋值为“xiaohong”,所以在输出s1的name时输出为xiaohong
6.如果某个对象没有一个变量引用它,则该对象无法被操作了,该对象会成为所谓的垃圾对象,但是Java存在自动垃圾回收机制,会自动清除掉垃圾对象,不怕占用内存
this就是一个变量,可以用在方法中,来拿到当前对象;哪个对象调用方法,this就指向哪个对象,也就是拿到哪个对象
package com.itheima.thisdemo;
public class Student {
public void printThis(){
System.out.println(this);
}
}
package com.itheima.thisdemo;
public class Test {
public static void main(String[] args) {
Student s1=new Student();
System.out.println(s1);
s1.printThis();
System.out.println("------------------");
Student s2=new Student();
System.out.println(s2);
s2.printThis();
}
}
this应用场景:当对象的成员变量与方法内部变量的名称一样时,解决变量名字冲突问题
package com.itheima.thisdemo;
public class Student {
double score;
public void printThis(){
System.out.println(this);
}
public void printPass(double score){
if (this.score>score){
System.out.println("恭喜您被录取");
}else {
System.out.println("落选了");
}
}
}
package com.itheima.thisdemo;
public class Test {
public static void main(String[] args) {
Student s1=new Student();
System.out.println(s1);
s1.printThis();
System.out.println("------------------");
Student s2=new Student();
System.out.println(s2);
s2.printThis();
Student s3=new Student();
s3.score=325;
s3.printPass(250);
}
}
构造器特点:创建对象时,对象会去调用构造器
Student s=new Student();
package com.itheima.constructor;
public class Test {
public static void main(String[] args) {
Student s1=new Student();
System.out.println("-----------------");
Student s2=new Student("xiaoming",99);
}
}
package com.itheima.constructor;
public class Student {
//无参构造器
public Student(){
System.out.println("无参构造器被触发执行了");
}
//有参构造器
public Student(String name,double score){
System.out.println("有参数构造器被触发执行了");
}
}
构造器的常见应用场景
1.创建对象时,完成对对象成员变量的初始化赋值
package com.itheima.constructor;
public class Test {
public static void main(String[] args) {
Student s2=new Student("xiaoming",99);
System.out.println(s2.name);
System.out.println(s2.score);
}
}
package com.itheima.constructor;
public class Student {
String name;
double score;
//无参构造器
public Student(){
System.out.println("无参构造器被触发执行了");
}
//有参构造器
public Student(String name,double score){
System.out.println("有参数构造器被触发执行了");
this.name=name;
this.score=score;
}
}
注意事项:
类在设计时,如果不写构造器,JAVA会为类自动生成一个无参构造器
一旦定义了有参构造器,Java就不会帮我们的类自动生成无参构造器了,此时建议自己手写一个无参构造器出来
如:
package com.itheima.constructor;
public class Test {
public static void main(String[] args) {
Student s2=new Student("xiaoming",99);
System.out.println(s2.name);
System.out.println(s2.score);
Teacher t=new Teacher();
}
}
package com.itheima.constructor;
public class Teacher {
public Teacher(String name){
}
}
此时会报错,因为已经构造了有参构造器,不难通过new Teacher()调用无参构造器
1.封装:用类设计对象处理某一个事物的数据时,应该把要处理的数据,以及处理这些数据的方法,设计到一个对象中去。
2.封装的设计规范:合理隐藏,合理暴露
3.代码层面如何控对象的成员公开或隐藏?
4.公开成员,可以使用public进行修饰
隐藏成员,使用private(私有)进行修饰
一种特殊形式的类,这个类中的成员变量都要私有,并且要对外提供相应的getxxx,setxxx方法,必须有无参数构造器
应用场景:实体类只负责数据存取,而对数据的处理交给其他类来完成,以实现数据和数据业务相分离
实体类:
package com.itheima.javabean;
public class Student {
//私有化成员变量,并为每个成员变量都提供get set方法
private String name;
private double score;
//必须为类提供一个公开的无参构造器
public Student(String name, double score) {
this.name = name;
this.score = score;
}//有参构造器
public Student() {
}//无参构造器
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;
}
}
操作类:
package com.itheima.javabean;
public class StudentOperator {
private Student student;
public StudentOperator(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.itheima.javabean;
public class Test {
public static void main(String[] args) {
Student s1=new Student();
s1.setName("xiaohong");
s1.setScore(99);
System.out.println(s1.getName());
System.out.println(s1.getScore());
StudentOperator operator=new StudentOperator(s1);
operator.printPass();
}
}
需求:
1.展示系统中的全部电影(每部电影显示:名称,价格)。
2.允许用户根据电影编号(id)查询出某个电影的详细信息。
实体类:
package com.itheima.moviedemo;
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
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 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;
}
public Movie(int id, String name, double price, String director, String actor, String info,Double score) {
this.id = id;
this.name = name;
this.price = price;
this.director = director;
this.actor = actor;
this.info = info;
this.score=score;
}
}
操作类:
package com.itheima.moviedemo;
public class MovieOperator {
private Movie[] movies;
public MovieOperator(Movie[] movies){
this.movies=movies;
}
//展示系统全部电影信息
public void printAllMovies(){
System.out.println("-----系统全部电影信息如下:----");
for (int i=0;i
主类:
package com.itheima.moviedemo;
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,"徐克","吴京","12万人想看",9.8);
movies[1]=new Movie(2,"出拳吧",39,"唐晓白","田雨","3.5万人想看",7.8);
movies[2]=new Movie(3,"月球陨落",42,"罗兰","贝瑞","17.9万人想看",7.9);
movies[3]=new Movie(4,"一点就到家",35,"许宏宇","刘昊然","10.8万人想看",8.7);
MovieOperator operator=new MovieOperator(movies);
while (true) {
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:
System.out.println("请您输入查询的电影id");
int id= sc.nextInt();
operator.searchMovieById(id);
break;
default:
System.out.println("您输入的命令有误");
}
}
}
}
区别 | 成员变量 | 局部变量 |
---|---|---|
类中位置不同 | 类中,方法外 | 常见于方法中 |
初始化值不同 | 有默认值,不需要初始化赋值 | 没有默认值,使用之前必须完成赋值 |
内存位置不同 | 堆内存 | 栈内存 |
作用域不同 | 整个对象 | 在所归属的大括号中 |
生命周期不同 | 与对象共存亡 | 随着方法的调用而生,随着方法的运行而结亡 |