JavaSE入门篇——类和对象(实例理解)

一、面向对象简述

面向对象是一种现在最为流行的程序设计方法,几乎现在的所有应用都以面向对象为主了,最早的面向对象的概念实际上是由IBM提出的,在70年代的Smaltalk语言之中进行了应用,后来根据面向对象的设计思路,才形成C++,而由C++产生了Java这门面向对象的编程语言。

Java是一门纯面向对象的语言(Object Oriented Program,简称OOP),在面向对象的世界里,一切皆为对象。面向对象是解决问题的一种思想,主要依靠对象之间的交互完成一件事情。用面向对象的思想来涉及程序,更符合人们对事物的认知,对于大型程序的设计、扩展以及维护都非常友好

相比于面向对象,更多的是要进行子模块化的设计,每一个模块都需要单独存在,并且可以被重复利用,所以,面向对象的开发更像是一个具备标准的开发模式。

在面向对象定义之中,也规定了一些基本的特征:

(1)封装:保护内部的操作不被破坏;
(2)继承:在原本的基础之上继续进行扩充;
(3)多态:在一个指定的范围之内进行概念的转换。

二、类与对象的基本概念

类:是抽象的概念集合,表示的是对一个实体对象的描述,类之中定义的是属性和行为方法;

对象:对象是一种个性的表示,表示一个独立的个体,每个对象拥有自己独立的属性,依靠属性来区分不同对象

可以一句话来总结出类和对象的区别:类是对象的模板,对象是类的实例。类只有通过对象才可以使用,而在开发之中应该先产生类,之后再产生对象。类不能直接使用,对象是可以直接使用的。

三、类的定义与使用

在Java中定义类,使用关键字class完成。语法如下:

// 创建类
class ClassName{
    
    
field; // 字段(属性) 或者 成员变量
method; // 行为 或者 成员方法
}

class为定义类的关键字,ClassName为类的名字,{}中为类的主体;
范例,定义一个student类:

class student{
    
    
    String name;
    int age;
    String classroom;
    public void show() {
    
            // 与main方法不同的是没有static
        System.out.println("姓名:" + name + ",年龄:" + age+",班级:"+classroom) ;
    }
}

类定义完成之后,还没有一个具体的对象。如果要使用类,必须创建一个对象,那么由于类属于引用数据类型,所以对象的产生格式(两种格式)如下:

(1)格式一:声明并实例化对象

类名称 对象名称 = new 类名称 () ;

(2)格式二:先声明对象,然后实例化对象:

类名称 对象名称 = null ;
对象名称 = new 类名称 () ;

引用数据类型与基本数据类型最大的不同在于:引用数据类型需要内存的分配和使用。所以,关键字new的主要功能就是分配内存空间,也就是说,只要使用引用数据类型,就要使用关键字new来分配内存空间。

那么调用对象的语法是什么呢?

对象.属性:表示调用类之中的属性;
对象.方法():表示调用类之中的方法。

范例:使用对象操作类:

class student{
    
    
    String name;
    int age;
    String classroom;
    public void show() {
    
            // 没有static
        System.out.println("姓名:" + name + ",年龄:" + age+",班级:"+classroom) ;
    }
}

public class text3 {
    
    
    public static void main(String[] args) {
    
    
        //student student1 = null;
       // student1=new student() ;// 第一种声明并实例化对象
        student student1 = new student() ;// 第二种声明并实例化对象
        student1.name = "张三" ;//操作属性内容
        student1.age = 30 ;//操作属性内容
        student1.classroom="jike" ;//操作属性内容
        student1.show() ;//调用类中的get()方法
    }
}

运行结果如下:
在这里插入图片描述
我们从内存的角度分析类的内存。首先,给出两种内存空间的概念:

(1)堆内存:保存对象的属性内容。堆内存需要用new关键字来分配空间;

(2)栈内存:保存的是堆内存的地址(在这里为了分析方便,可以简单理解为栈内存保存的是对象的名字)。

在这里插入图片描述
任何情况下,只要看见关键字new,都表示要分配新的堆内存空间,一旦堆内存空间分配了,里面就会有类中定义的属性,并且属性内容都是其对应数据类型的默认值。(除过char默认\u0000',boolean默认false,引用类型默认null外都是默认为0)
在这里插入图片描述
注:程序只声明对象,但并没有实例化对象(只有了栈内存,并没有对应的堆内存空间),则程序可以正常编辑,但是在执行的时候会出现“NullPointerException(空指向异常)”。所以我们要尽可能的避免这种无意义的写法。

接下来我们看一看对象引用分析的传递

class student{
    
    
    String name;
    int age;
    String classroom;
    public void show() {
    
            // 没有static
        System.out.println("姓名:" + name + ",年龄:" + age+",班级:"+classroom) ;
    }
}

public class text3 {
    
    
    public static void main(String[] args) {
    
    
        student student1 = null;
        student1=new student() ;// 声明并实例化对象
        student1.name = "张三" ;//操作属性内容
        student1.age = 30 ;//操作属性内容
        student1.classroom="jike" ;//操作属性内容
        //student1.show() ;//调用类中的get()方法

        student student2 = new student() ;// 声明并实例化对象
        student2.name = "李四" ;//操作属性内容
        student2.age = 25 ;//操作属性内容
        student2.classroom="jike" ;//操作属性内容
        //student2.show() ;//调用类中的get()方法

        student1=student2;
        student1.show() ;
    }
}

输出结果如下:
在这里插入图片描述

这时候student1指向的内容变成student2指向的那片空间,而一开始student1指向的那片空间没有任何对象指向,则变成一块垃圾。

垃圾:指的是在程序开发之中没有任何对象所指向的一块堆内存空间,这块空间就成为垃圾,所有的垃圾将等待GC(垃圾收集器)不定期的进行回收与空间的释放。

四、this引用

我们先读下列代码:

class Date {
    
    
    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 printDate(){
    
    
        System.out.println(year + "/" + month + "/" + day);
    }
    public static void main(String[] args) {
    
    
// 构造三个日期类型的对象 d1 d2 d3
        Date d1 = new Date();
        Date d2 = new Date();
        Date d3 = new Date();
// 对d1,d2,d3的日期设置
        d1.setDay(2020,9,15);
        d2.setDay(2020,9,16);
        d3.setDay(2020,9,17);
// 打印日期中的内容
        d1.printDate();
        d2.printDate();
        d3.printDate();
    }
}

public class text1 {
    
    
    public static void main(String[] args) {
    
    

    }
}

打印正常:

在这里插入图片描述
但是在我们书写类方法时,都会为了可读性而使用的形参名不小心与成员变量名相同:

public void setDay(int year, int month, int day){
    
    
        year = year;
        month = month;
        day = day;
    }

就会导致计算机读不懂函数体中到底是谁给谁赋值? 成员变量给成员变量?参数给参数?参数给成员变量?成员变量参数?估计自己都搞不清楚了。就会造成如下结果:

在这里插入图片描述

三个对象都在调用setDate和printDate函数,但是这两个函数中没有任何有关对象的说明,setDate和printDate函数如何知道打印的是那个对象的数据呢?
在这里插入图片描述
一切让this引用来揭开这层神秘的面纱。

什么是this引用呢?

this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操作,都是通过该引用去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自动完成。

public class Date {
    
    
public int year;
public int month;
public int day;
public void setDay(int year, int month, int day){
    
    
this.year = year;
this.month = month;
this.day = day;
}
public void printDate(){
    
    
System.out.println(this.year + "/" + this.month + "/" + this.day);
}
}

注意:this引用的是调用成员方法的对象

public static void main(String[] args) {
    
    
Date d = new Date();
d.setDay(2020,9,15);
d.printDate();
}

在这里插入图片描述
this引用的特性

  1. this的类型:对应类类型引用,即哪个对象调用就是哪个对象的引用类型
  2. this只能在"成员方法"中使用
  3. 在"成员方法"中,this只能引用当前对象,不能再引用其他对象
  4. this是“成员方法”第一个隐藏的参数,编译器会自动传递,在成员方法执行时,编译器会负责将调用成员方法 对象的引用传递给该成员方法,this负责来接收

五、对象的构造及初始化

我们知道在Java方法内部定义一个局部变量时,必须要初始化,否则会编译失败。我们的类在使用的时候也是,每次对象创建好后调用SetDate方法设置具体日期,比较麻烦。这时候就应人们的需求,有了构造方法

public class Date {
    
    
    public int year;
    public int month;
    public int day;
    
// 构造方法:
// 名字与类名相同,没有返回值类型,设置为void也不行
// 一般情况下使用public修饰
// 在创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次

    public Date(int year, int month, int day){
    
    
        this.year = year;
        this.month = month;
        this.day = day;
        System.out.println("Date(int,int,int)方法被调用了");
       }
 
    public void printDate(){
    
    
        System.out.println(year + "-" + month + "-" + day);
     }
    public static void main(String[] args) {
    
    
// 此处创建了一个Date类型的对象,并没有显式调用构造方法
       Date d = new Date(2021,6,9); // 输出Date(int,int,int)方法被调用了
       d.printDate(); // 2021-6-9
   }
}

注:

1.构造方法可以重载(用户根据自己的需求提供不同参数的构造方法(名字相同,参数列表不同);
2.在类中,没有定义任何构造方法,编译器会默认生成一个不带参数的构造方法,一旦用户定义,编译器则不再生成。

构造方法中,可以通过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;
}
}

六、static成员

假设三个同学是同一个班的,那么他们上课肯定是在同一个教室,那既然在同一个教室,那能否给类中再加一个成员变量,来保存同学上课时的教室呢?答案是不行的。
之前在Student类中定义的成员变量,每个对象中都会包含一份(称之为实例变量),因为需要使用这些信息来描述具体的学生。而现在要表示学生上课的教室,这个教室的属性并不需要每个学生对象中都存储一份,而是需要让所有的学生来共享。

在Java中,被static修饰的成员,称之为静态成员,也可以称为类成员静态成员变量最大的特性:不属于某个具体的对象,是所有对象所共享的.

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);

注:类变量存储在方法区当中

Java中,被static修饰的成员方法称为静态成员方法,是类的方法,不是某个对象所特有的。静态成员一般是通过静态方法来访问的 。.(具体说明在博主博客封装中具体介绍)

【静态方法特性】

  1. 不属于某个具体的对象,是类方法
  2. 可以通过对象调用,也可以通过类名.静态方法名(…)方式调用,更推荐使用后者
  3. 不能在静态方法中访问任何非静态成员变量
  4. 静态方法中不能调用任何非静态方法,因为非静态方法有this参数,在静态方法中调用时候无法传递this引用

七、 代码块

使用 {} 定义的一段代码称为代码块。根据代码块定义的位置以及关键字,又可分为以下四种:

  • 普通代码块
  • 构造块
  • 静态块
  • 同步代码块(多线程部分详谈,此处不多介绍)

普通代码块:定义在方法中的代码块;

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

构造块:定义在类中的代码块(不加修饰符)。也叫:实例代码块。构造代码块一般用于初始化实例成员变量

public class Student{
    
    
//实例成员变量
    private String name;
    private String gender;
    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 class Main {
    
    
   public static void main(String[] args) {
    
    
   Student stu = new Student();
   stu.show();
  }
}

使用static定义的代码块称为静态代码块。一般用于初始化静态成员变量

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.静态代码块不管生成多少个对象,其只会执行一次 静态成员变量是类的属性,因此是在JVM加载类时开辟空间并初始化的
2.如果一个类中包含多个静态代码块,在编译代码时,编译器会按照定义的先后次序依次执行(合并) 实例代码块只有在创建对象时才会执行

文章结束,内容不断,后续继续!

猜你喜欢

转载自blog.csdn.net/m0_65038072/article/details/127795937