hello,小编又来了,有菜有爱写,希望能够帮助到你们!!!
目录
hello,小编又来了,有菜有爱写,希望能够帮助到你们!!!
6. 构造方法中,可以通过this调用其他构造方法来简化代码
1. 面向对象的初步认知
1.1 什么是面向对象
Java是一门纯面向对象的语言(Object Oriented Program,简称OOP),在面向对象的世界里,一切皆为对象。面向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。用面向对象的思想来涉及程序,更符合人们对事物的认知,对于大型程序的设计、扩展以及维护都非常友好。
1.2 面向对象与面向过程
1. 传统洗衣服过程(面向过程)
传统的方式:注重的是洗衣服的过程,少了一个环节可能都不行。
而且不同衣服洗的方式,时间长度,拧干方式都不同,处理起来就比较麻烦。如果将来要洗鞋子,那就是另一种放方式。按照该种方式来写代码,将来扩展或者维护起来会比较麻烦。
2. 现代洗衣服过程(面向对象)
以面向对象方式来进行处理,就不关注洗衣服的过程,具体洗衣机是怎么来洗衣服,如何来甩干的,用户不用去关心,只需要将衣服放进洗衣机,倒入洗衣粉,启动开关即可,通过对象之间的交互来完成的。
面向过程:注重过程!!!
面向对象:不关注过程!!!
注意:面向过程和面相对象并不是一门语言,而是解决问题的方法,没有那个好坏之分,都有其专门的应用场景。
2. 类定义和使用
![](https://img-blog.csdnimg.cn/6a34c7aafd19459c83cf85a0d8fb90f3.png)
![](https://img-blog.csdnimg.cn/27da587d91b64c6ba80f7eafffe8453b.jpeg)
2.1、 简单认识类
类是用来对一个实体(对象)来进行描述的,主要描述该实体(对象)具有哪些属性(外观尺寸等),哪些功能(用来干啥),描述完成后计算机就可以识别了。
比如:洗衣机,它是一个品牌,在 Java 中可以将其看成是一个类别。属性:产品品牌,型号,产品重量,外观尺寸,颜色 ...功能:洗衣,烘干、定时 ....
在Java语言中,如何对上述的洗衣机类来进行定义呢?
2.2、 类的定义格式
// 创建类
class ClassName{
field; // 字段(属性) 或者 成员变量
method; // 行为 或者 成员方法
}
public class WashMachine {
public String brand;
public String type;
public double weight;
public double width;
public double height;
public String color;
public void washMachine(){
System.out.println("洗衣服!");
}
public void dryClothes(){
System.out.println("烘干衣服!");
}
public void setTime(){
System.out.println("定时");
}
}
2.3.1、 定义一个学生类
public class Student{
public String name;
public String gender;
public short age;
public double score;
public void DoClass(){}
public void DoHomework(){}
public void Exam(){}
}
注意事项:
1. 一般一个文件当中只定义一个类2. main 方法所在的类一般要使用 public 修饰 ( 注意: Eclipse 默认会在 public 修饰的类中找 main 方法 )3. public 修饰的类必须要和文件名相同4. 不要轻易去修改 public 修饰的类的名称,如果要修改,通过开发工具修改 ( 给同学演示 ) 。
![](https://img-blog.csdnimg.cn/8f7fdd18c02a410f83a69dd2c08d83b5.png)
3. 类的实例化
3.1、 什么是实例化
定义了一个类,就相当于在计算机中定义了一种新的类型,与int,double类似,只不过int和double是java语言自带的内置类型,而类是用户自定义了一个新的类型,比如上述的:WashMachine类和Student类。它们都是类(一种新定义的类型)有了这些自定义的类型之后,就可以使用这些类来定义实例(或者称为对象)。
用类类型创建对象的过程,称为类的实例化,在java中采用new关键字,配合类名来实例化对象。
class PetDog {
public String name;//名字
public String color;//颜色
// 狗的属性
public void barks() {
System.out.println(name + ": 旺旺旺~~~");
System.out.println(name + " "+color+"的尾巴!!!");
}
// 狗的行为
public void wag() {
System.out.println(name + ": 摇尾巴~~~");
}
public static void main(String[] args) {
PetDog dog1 = new PetDog(); //通过new实例化对象
dog1.name = "啦啦";
dog1.color = "白色";
dog1.barks();
dog1.wag();
System.out.println("---------------------------");
PetDog dog2 = new PetDog();
dog2.name = "哆哆";
dog2.color = "白色";
dog2.barks();
dog2.wag();
}
}
输出:
注意事项 :
new 关键字用于创建一个对象的实例 .使用 . 来访问对象中的属性和方法 .同一个类可以创建多个实例.
3.2 、类和对象的说明
1. 类只是一个模型一样的东西,用来对一个实体进行描述,限定了类有哪些成员.
2. 类是一种自定义的类型,可以用来定义变量.
3. 一个类可以实例化出多个对象,实例化出的对象 占用实际的物理空间,存储类成员变量。
![](https://img-blog.csdnimg.cn/24fea1c41fdc40bb912d603b8e37e8a0.png)
4. this引用
4.1 为什么要有this引用
先看一个日期类的例子:
public class Data {
public int year;
public int month;
public int day;
public void setDay(int y,int m,int d){
year = y;
month = m;
day = d;
}
public void printData(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
public static void main(String[] args) {
Data data = new Data();
data.setDay(2022,11,10);
data.printData();
}
}
public void setDay ( int year , int month , int day ){year = year ;month = month ;day = day ;}
那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?估计自己都搞不清楚了。
2. 三个对象都在调用setData和printData函数,但是这两个函数中没有任何有关对象的说明,setData和printData函数如何知道打印的是那个对象的数据呢?
一切让this引用来揭开这层神秘的面纱。
4.2 什么是this引用
public class Data {
public int year;
public int month;
public int day;
public void setDay(int y,int m,int d){
this.year = y;
this.month = m;
this.day = d;
}
public void printData(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
public static void main(String[] args) {
Data data = new Data();
data.setDay(2022,11,10);
data.printData();
}
}
注意: this 引用的是调用成员方法的对象 。
public static void main(String[] args) {
Data data = new Data();
data.setDay(2022,11,10);
data.printData();
}
4.3 this引用的特性
![](https://img-blog.csdnimg.cn/f1cb732e001c4e59b23d2e79f21baff0.png)
5. 对象的构造及初始化
5.1 如何初始化对象
通过前面知识点的学习知道,在Java方法内部定义一个局部变量时,必须要初始化,否则会编译失败。
public static void main(String[] args) {
int a;
System.out.println(a);
}
// Error:(26, 28) java: 可能尚未初始化变量a
public static void main(String[] args) {
Date d = new Date();
d.printDate();
d.setDate(2021,6,9);
d.printDate();
}
// 代码可以正常通过编译
1. 每次对象创建好后调用 SetDate 方法设置具体日期,比较麻烦,那对象该如何初始化?2. 局部变量必须要初始化才能使用,为什么字段声明之后没有给值依然可以使用?
5.2 构造方法
5.2.1 概念
public class Data {
public int year;
public int month;
public int day;
public Data(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
public void printData(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
public static void main(String[] args) {
Data data = new Data(2022,11,10);
data.printData();
}
}
注意:构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。
5.2.2 构造方法的特性
1. 名字必须与类名相同
2. 没有返回值类型,设置为void也不行
public class Data {
public int year;
public int month;
public int day;
//有参数的构造方法
public Data(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
//无参数的构造方法 并且初始化
public Data(){
this.year = 2002;
this.month = 03;
this.day = 19;
}
public void printData(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
public static void main(String[] args) {
//实例化有参数构造方法 并且初始化
Data data = new Data(2022,11,10);
//实例化无参数的构造方法
Data data2 = new Data();
data.printData();
data2.printData();
}
}
public class Data1 {
public int year;
public int month;
public int day;
public void printData(){
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
public static void main(String[] args) {
Data data2 = new Data();
data2.printData();
}
}
注意: 一旦用户定义,编译器则不再生成。
public class Date {
public int year;
public int month;
public int day;
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
System.out.println(year + "-" + month + "-" + day);
}
public static void main(String[] args) {
// 如果编译器会生成,则生成的构造方法一定是无参的
// 则此处创建对象是可以通过编译的
// 但实际情况是:编译期报错
Date d = new Date();
d.printDate();
}
}
/*
Error:(26, 18) java: 无法将类 extend01.Date中的构造器 Date应用到给定类型;
需要: int,int,int
找到: 没有参数
原因: 实际参数列表和形式参数列表长度不同
*/
6. 构造方法中,可以通过this调用其他构造方法来简化代码
public class Date {
public int year;
public int month;
public int day;
// 无参构造方法--内部给各个成员赋值初始值,该部分功能与三个参数的构造方法重复
// 此处可以在无参构造方法中通过this调用带有三个参数的构造方法
// 但是this(1900,1,1);必须是构造方法中第一条语句
public Date(){
//System.out.println(year); 注释取消掉,编译会失败
this(1900, 1, 1);
//this.year = 1900;
//this.month = 1;
//this.day = 1;
}
// 带有三个参数的构造方法
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
注意: this(...) 必须是构造方法中第一条语句不能形成环像下面代码这样!!!
public Date(){
this(1900,1,1);
}
public Date(int year, int month, int day) {
this();
}
/*
无参构造器调用三个参数的构造器,而三个参数构造器有调用无参的构造器,形成构造器的递归调用
编译报错:Error:(19, 12) java: 递归构造器调用
*/
5.3 默认初始化
在上文中提出的第二个问题:为什么局部变量在使用时必须要初始化,而成员变量可以不用呢?
public class Date {
public int year;
public int month;
public int day;
public Date(int year, int month, int day) {
// 成员变量在定义时,并没有给初始值, 为什么就可以使用呢?
System.out.println(this.year);
System.out.println(this.month);
System.out.println(this.day);
}
public static void main(String[] args) {
// 此处a没有初始化,编译时报错:
// Error:(24, 28) java: 可能尚未初始化变量a
// int a;
// System.out.println(a);
Date d = new Date(2021,6,9);
}
}
Date d = new Date(2021,6,9);
数据类型
| 默认值 |
byte | 0 |
char | '\u0000' |
short | 0 |
int | 0 |
long | 0L |
boolean | false |
float | 0.0f |
double | 0.0 |
reference | null |
5.4 就地初始化
在声明成员变量时,就直接给出了初始值。
public class Date {
public int year = 1900;
public int month = 1;
public int day = 1;
public Date(){
}
public Date(int year, int month, int day) {
}
public void print(){
System.out.println(year+"/"+month+"/"+day);
}
public static void main(String[] args) {
//Date d1 = new Date(2021,6,9);
Date d2 = new Date();
// d1.print();
d2.print();
}
}
6. 封装
6.1 封装的概念
面向对象程序三大特性:封装、继承、多态。而类和对象阶段,主要研究的就是封装特性。何为封装呢?简单来说就是套壳屏蔽细节。
![](https://img-blog.csdnimg.cn/fcb4c5b581af4c2b8ac47a8aedc538dd.png)
6.2 访问限定符
Java中主要通过类和访问权限来实现封装:类可以将数据以及封装数据的方法结合在一起,更符合人类对事物的认知,而访问权限用来控制方法或者字段能否直接在类外使用。Java中提供了四种访问限定符:
比如:public :可以理解为一个人的外貌特征,谁都可以看得到default: 对于自己家族中 ( 同一个包中 ) 不是什么秘密,对于其他人来说就是隐私了private :只有自己知道,其他人都不知道
public class Computer {
private String cpu; // cpu
private String memory; // 内存
public String screen; // 屏幕
String brand; // 品牌---->default属性
public Computer(String brand, String cpu, String memory, String screen) {
this.brand = brand;
this.cpu = cpu;
this.memory = memory;
this.screen = screen;
}
public void Boot(){
System.out.println("开机~~~");
}
public void PowerOff(){
System.out.println("关机~~~");
}
public void SurfInternet(){
System.out.println("上网~~~");
}
//public class TestComputer {
public static void main(String[] args) {
Computer p = new Computer("HW", "i7", "8G", "13*14");
System.out.println(p.brand); // default属性:只能被本包中类访问
System.out.println(p.screen); // public属性: 可以任何其他类访问
// System.out.println(p.cpu); // private属性:只能在Computer类中访问,不能被其他类访问
}
}
注意: 一般情况下成员变量设置为 private ,成员方法设置为 public 。
6.3 封装扩展之包
6.3.1 包的概念
![](https://img-blog.csdnimg.cn/c011f85d12f146208a3d4d55a2730ba6.png)
6.3.2 导入包中的类
Java 中已经提供了很多现成的类供我们使用. 例如Date类:可以使用 java.util.Date 导入 java.util 这个包中的 Date类.
public class Test {
public static void main(String[] args) {
java.util.Date date = new java.util.Date();
// 得到一个毫秒级别的时间戳
System.out.println(date.getTime());
}
}
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
// 得到一个毫秒级别的时间戳
System.out.println(date.getTime());
}
}
import java.util.*;
public class Test {
public static void main(String[] args) {
Date date = new Date();
// 得到一个毫秒级别的时间戳
System.out.println(date.getTime());
}
}
import java.util.*;
import java.sql.*;
public class Test {
public static void main(String[] args) {
// util 和 sql 中都存在一个 Date 这样的类, 此时就会出现歧义, 编译出错
Date date = new Date();
System.out.println(date.getTime());
}
}
// 编译出错
Error:(5, 9) java: 对Date的引用不明确
java.sql 中的类 java.sql.Date 和 java.util 中的类 java.util.Date 都匹配
import java.util.*;
import java.sql.*;
public class Test {
public static void main(String[] args) {
java.util.Date date = new java.util.Date();
System.out.println(date.getTime());
}
}
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
double x = 30;
double y = 40;
// 静态导入的方式写起来更方便一些.
// double result = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
double result = sqrt(pow(x, 2) + pow(y, 2));
System.out.println(result);
}
}
注意事项: import 和 C++ 的 #include 差别很大 . C++ 必须 #include 来引入其他文件内容 , 但是 Java 不需要 .import 只是为了写代码的时候更方便 . import 更类似于 C++ 的 namespace 和 using
6.3.3 自定义包
![](https://img-blog.csdnimg.cn/0b2f02cc180f45efba25cfea5266b49c.png)
2. 在弹出的对话框中输入包名,即可
3. 在包中创建类, 右键包名 -> 新建 -> 类, 然后输入类名即可.
4. 此时可以看到我们的磁盘上的目录结构已经被 IDEA 自动创建出来了
5. 同时我们也看到了, 在新创建的 Test.java 文件的最上方, 就出现了一个 package 语句
6.3.4 包的访问权限控制举例
package com.bit.demo1;
public class Computer {
private String cpu; // cpu
private String memory; // 内存
public String screen; // 屏幕
String brand; // 品牌
public Computer(String brand, String cpu, String memory, String screen) {
this.brand = brand;
this.cpu = cpu;
this.memory = memory;
this.screen = screen;
}
public void Boot(){
System.out.println("开机~~~");
}
public void PowerOff(){
System.out.println("关机~~~");
}
public void SurfInternet(){
System.out.println("上网~~~");
}
public static void main(String[] args) {
Computer p = new Computer("HW", "i7", "8G", "13*14");
System.out.println(p.screen);
// System.out.println(p.cpu); // 报错:cup是私有的,不允许被其他类访问
// System.out.println(p.brand); // 报错:brand是default,不允许被其他包中的类访问
}
}
///
package com.bite.demo2;
import com.bite.demo1.Computer;
public class TestComputer {
public static void main(String[] args) {
Computer p = new Computer("HW", "i7", "8G", "13*14");
System.out.println(p.screen);
// System.out.println(p.cpu); // 报错:cup是私有的,不允许被其他类访问
// System.out.println(p.brand); // 报错:brand是default,不允许被其他包中的类访问
}
}
// 注意:如果去掉Computer类之前的public修饰符,代码也会编译失败
6.3.5 常见的包
7. static成员
7.1 再谈学生类
public class Student{
// ...
public static void main(String[] args) {
Student s1 = new Student("Li leilei", "男", 18, 3.8);
Student s2 = new Student("Han MeiMei", "女", 19, 4.0);
Student s3 = new Student("Jim", "男", 18, 2.6);
}
}
7.2 static修饰成员变量
static修饰的成员变量,称为静态成员变量,静态成员变量最大的特性:不属于某个具体的对象,是所有对象所共享的。
【静态成员变量特性】
1. 不属于某个具体的对象,是类的属性,所有对象共享的,不存储在某个对象的空间中
public class Student{
public String name;
public String gender;
public int age;
public double score;
public static String classRoom = "Bit306";
// ...
public static void main(String[] args) {
// 静态成员变量可以直接通过类名访问
System.out.println(Student.classRoom);
Student s1 = new Student("Li leilei", "男", 18, 3.8);
Student s2 = new Student("Han MeiMei", "女", 19, 4.0);
Student s3 = new Student("Jim", "男", 18, 2.6);
// 也可以通过对象访问:但是classRoom是三个对象共享的
System.out.println(s1.classRoom);
System.out.println(s2.classRoom);
System.out.println(s3.classRoom);
}
}
7.3 static修饰成员方法
一般类中的数据成员都设置为private,而成员方法设置为public,那设置之后,Student类中classRoom属性如何在类外访问呢?
public class Student{
private String name;
private String gender;
private int age;
private double score;
private static String classRoom = "Bit306";
// ...
}
public class TestStudent {
public static void main(String[] args) {
System.out.println(Student.classRoom);
}
}
编译失败:
Error:(10, 35) java: classRoom 在 extend01.Student 中是 private 访问控制
public class Student{
// ...
private static String classRoom = "Bit306";
// ...
public static String getClassRoom(){
return classRoom;
}
}
public class TestStudent {
public static void main(String[] args) {
System.out.println(Student.getClassRoom());
}
}
输出: Bit306
【静态方法特性】
public static String getClassRoom(){
System.out.println(this);
return classRoom;
}
// 编译失败:Error:(35, 28) java: 无法从静态上下文中引用非静态 变量 this
public static String getClassRoom(){
age += 1;
return classRoom;
}
// 编译失败:Error:(35, 9) java: 无法从静态上下文中引用非静态 变量 age
public static String getClassRoom(){
doClass();
return classRoom;
}
// 编译报错:Error:(35, 9) java: 无法从静态上下文中引用非静态 方法 doClass()
7.4 static成员变量初始化
注意: 静态成员变量一般不会放在构造方法中来初始化,构造方法中初始化的是与对象相关的实例属性
public class Student{
private String name;
private String gender;
private int age;
private double score;
private static String classRoom = "Bit306";
// ...
}
2. 静态代码块初始化
8. 代码块
8.1 代码块概念以及分类
使用 {} 定义的一段代码称为代码块。根据代码块定义的位置以及关键字,又可分为以下四种:
普通代码块
构造块
8.2 普通代码块
普通代码块:定义在方法中的代码块.
public class Main{
public static void main(String[] args) {
{ //直接使用{}定义,普通方法块
int x = 10 ;
System.out.println("x1 = " +x);
}
int x = 100 ;
System.out.println("x2 = " +x);
}
}
// 执行结果
x1 = 10
x2 = 100
8.3 构造代码块
构造块:定义在类中的代码块(不加修饰符)。也叫:实例代码块。构造代码块一般用于初始化实例成员变量。
public class Student{
//实例成员变量
private String name;
private String sex;
private int age;
private double score;
public Student()
{
System.out.println("I am Student init()!");
}
//实例代码块
{
this.name = "bit";
this.age = 12;
this.sex = "man";
System.out.println("I am instance init()!");
}
public void show(){
System.out.println("name: "+name+" age: "+age+" sex: "+ sex);
}
public static void main(String[] args) {
Student stu = new Student();
stu.show();
}
}
输出
8.4 静态代码块
public class Student{
private String name;
private String gender;
private int age;
private double score;
private static String classRoom;
//实例代码块
{
this.name = "bit";
this.age = 12;
this.gender = "man";
System.out.println("I am instance init()!");
}
// 静态代码块
static {
classRoom = "bit306";
System.out.println("I am static init()!");
}
public Student(){
System.out.println("I am Student init()!");
}
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
注意事项:1)静态代码块不管生成多少个对象,其只会执行一次2)静态成员变量是类的属性,因此是在JVM 加载类时开辟空间并初始化的3)如果一个类中包含多个静态代码块,在编译代码时,编译器会按照定义的先后次序依次执行( 合并 )4) 实例代码块只有在创建对象时才会执行
总结: 内容很多,第一次看不懂,多看几次,上B站搜比特大博哥讲的很详细,希望可以帮助大家,在前进在路上不要放弃!努力会有回报哒!!!