Java基础学习day05
面向对象编程
1.快速入门
- 先设计对象的模板,也就是对象的设计图:类。
- 通过new关键字,每new一次类就得到一个新的对象 。
2.对象
定义
- 对象本质上是一种特殊的数据结构(可以理解成一张表)。
- class也就是类,也称为对象的设计图(或者对象的模板)。
- 对象是用类new出来的,有了类就可以创建出对象。
public class 类名{ 1、变量,用来说明对象可以处理什么数据 2、方法,描述对象有什么功能,也就是可以对数据进行什么样的处理 ... } 类名 对象名 = new 类名();
面向对象编程
- 万物皆对象,谁的数据谁存储。
3.类的基本语法
构造器
- 构造器的样子
public class Student { /** 构造器 */ public Student() { ... } }
- 构造器的特点
- 创建对象时,对象会去调用构造器。
- 构造器的常见应用场景
- 创建对象时,同时完成对对象成员变量(属性)的初始化赋值。
- 构造器的注意事项
- 类默认就自带了一个无参构造器 。
- 如果为类定义了有参数构造器,类默认的无参数构造器就没有了,此时如果还想用无参数构造器,就必须自己手写一个无参数构造器出来。
this
- 定义
- this就是一个变量, 可以用在方法中 ,用来拿到当前对象;
- 哪个对象调用方法,this就指向哪个对象,也就是拿到哪个对象。
- 应用场景
- 用来解决对象的成员变量与方法内部变量的名称一样时,导致访问冲突问题的。
- 案例
public class Test { public static void main(String[] args) { Student s1 = new Student(); System.out.println(s1); s1.printThis(); Student s2 = new Student(); System.out.println(s2); s2.printThis(); } }
public class Student { public void printThis(){ System.out.println(this); } }
封装
- 面向对象三大特征:
- 封装、继承、多态
- 类就是一种封装。
- 定义
- 就是用类设计对象处理某一个事物的数据时,应该把要处理的数据,以及处理这些数据的方法,设计到一个对象中去。
- 封装的设计要求
- 合理隐藏,合理暴露
- 代码层面控制
- 公开成员,可以使用public(公开)进行修饰。
- 隐藏成员,使用private(私有,隐藏) 进行修饰。
- 案例
// 合理的隐藏 private 私有的 类私有的 如果加了private修饰符 只有在这个类中才能访问 private String name; private int chinese; private int math; // 合理的暴露 给属性一个set方法 用来给属性赋值 public void setName(String name) { this.name = name; } public void setChinese(int chinese) { if (chinese >= 0 && chinese <= 100) { this.chinese = chinese; } else { System.out.println("传入的成绩不合理!"); } } public void setMath(int math) { if (math >= 0 && math <= 100) { this.math = math; } else { System.out.println("传入的成绩不合理!"); } } // get方法: 允许对象来获取属性 public String getName() { return name; } public int getChinese() { return chinese; } public int getMath() { return math; }
Javabean
- 实体类
- 是一种特殊类,类中要满足如下需求
- 类中的成员变量全部私有,并提供public修饰的get/set方法
- 类中需要提供一个无参数构造器, 有参数构造器可选
- 特点
- 仅仅只是一个用来保存数据的java类,可以用它创建对象,保存某个事物的数据。
- 应用场景
- 实体类的对象只负责数据存取,而对数据的处理交给其他类的对象来完成,以实现数据和数据业务处理相分离。
- 案例
public class Student { // 合理的隐藏 private 私有的 类私有的 如果加了private修饰符 只有在这个类中才能访问 private String name; private int chinese; private int math; // 合理的暴露 给属性一个set方法 用来给属性赋值 public void setName(String name) { this.name = name; } public void setChinese(int chinese) { if (chinese >= 0 && chinese <= 100) { this.chinese = chinese; } else { System.out.println("传入的成绩不合理!"); } } public void setMath(int math) { if (math >= 0 && math <= 100) { this.math = math; } else { System.out.println("传入的成绩不合理!"); } } // get方法: 允许对象来获取属性 public String getName() { return name; } public int getChinese() { return chinese; } public int getMath() { return math; } // 无参构造方法 public Student() { System.out.println("无参构造被调用了"); } public Student(String name, int chinese, int math) { System.out.println("有参构造被调用了"); this.name = name; this.chinese = chinese; this.math = math; } }
public class StudentOperator { private Student student; public StudentOperator() { } public StudentOperator(Student student) { this.student = student; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } // 求总成绩 public void printAllScore() { System.out.println(student.getName() + "的总成绩为:" + (student.getChinese() + student.getMath())); } // 求平均分 public void printAvgScore() { System.out.println((student.getName() + "的平均成绩为:" + (student.getChinese() + student.getMath()) / 2)); } }
public class Test { // 目标:掌握javabean public static void main(String[] args) { Student student = new Student(); student.setName("张三"); student.setChinese(100); student.setMath(80); StudentOperator studentOperator = new StudentOperator(student); studentOperator.printAvgScore(); studentOperator.printAllScore(); } }
static
- 定义
- 叫静态,可以修饰成员变量、成员方法。
- static修饰成员变量
- 成员变量按照有无static修饰,分为两种:
- 静态变量(类变量) :有static修饰,属于类,与类一起加载一次, 在计算机里只有一份,会被类的全部对象共享。
- 实例变量(对象的变量) :无static修饰,属于每个对象,每个对象中都有一份。
- 静态变量的应用场景
- 如果某个数据只需要一份,且希望能够被共享(访问、修改),则该数据可以定义成静态变量来记住。
- 案例:
- 系统启动后,要求用户类可以记住自己创建了多少个用户对象了。
public class User { static int count = 0; public User() { count++; } }
public static void main(String[] args) { User user = new User(); new User(); new User(); new User(); new User(); new User(); System.out.println(User.count); }
- 注意事项
- 访问自己类中的类变量,可以省略类名不写。
- 在某个类中访问其他类里的类变量,必须带类名访问 。
- static修饰方法
- 成员方法的分类
- 静态方法:有static修饰的成员方法,属于类。
- 实例方法:无static修饰的成员方法,属于对象。
- 静态方法的应用
- 常见场景
- 做工具类。
- 工具类的定义
- 工具类中的方法都是一些静态方法,每个方法用来完成一个功能,以便供给开发人员直接使用。
- 工具类的好处
- 工具类中的方法都是静态方法,每个类方法都是用来完成一个功能的。
- 提高了代码的复用性;调用方便,提高了开发效率。
- 为什么工具类要用静态方法,而不是实例方法
- 实例方法需要创建对象来调用,此时对象只是为了调用方法,对象占内存,这样会浪费内存。
- 静态方法直接用类名调用即可,调用方便,也能节省内存。
- 工具类没有创建对象的需求,建议将工具类的构造器进行私有。
- static的注意事项
- 静态方法中可以直接访问静态成员,不可以直接访问实例成员。
- 实例方法中既可以直接访问静态成员,也可以直接访问实例成员。
- 实例方法中可以出现this关键字,静态方法中不可以出现this关键字的。