跳到主要内容Java 类和对象 | 极客日志Javajava
Java 类和对象
Java 类和对象是面向对象编程的基础。类作为对象的模板,通过实例化创建具体对象。涵盖实例化过程、this 引用、构造方法初始化、封装机制(访问修饰符与包)、静态成员及代码块等内容。重点讲解如何定义类、属性与方法,以及内存分配、生命周期和访问控制规则。
人间过客19 浏览 类和对象
- Java 当中一切皆对象
- 对象是什么呢?比如是一个人,手机等
- 怎么描述对象呢?可以用类描述对象,可以理解类为一个模版,用这个模版存储对象的属性
- Java 中只能有一个 public 类,其他为普通类
- 面向对象的核心:找对象、创建对象、使用对象
- 什么是面向对象?面向对象是一种解决问题的思想,主要通过对象之间的交互完成一件事情,不用考虑细节问题,只关注对象本身
- 什么是面向过程?面向过程需要考虑细节和中间的每一步
class Ast {
public String name;
public int age;
public static int a = -10;
public int func() {}
public static int func2() {}
}
public class Main {
public static void main(String[] args) {
Ast p = new Ast();
}
}
每一个类都会产生一个字节码文件
实例化对象
- 什么叫做实例化对象?用一个类类型创建一个对象的过程就叫类的实例化
- 每一个对象都会存对应的变量,而方法存在于方法区,可以用 new 关键字实例化出多个对象,实例化出的对象才实际占用物理空间
通过点访问对象的属性,调用对象的方法public class Test666 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "小七";
dog.color = "红色";
dog.print();
dog.name = "小八";
dog.color = "黄色";
dog.print();
}
}
class Dog {
public String name;
public String color;
public void print() {
System.out.println(name + " ");
}
}
this
- this 用来确定是哪个对象,才好给对象进行赋值
成员方法中有隐藏的 this。局部变量会给局部变量自己赋值,局部变量优先,变量名一样的,不知道是给对象赋值的,所以建议都加上 this。
class Date {
public void setDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
- this 代表当前对象的引用,this 引用的是调用成员方法的对象,this 只能在成员方法中使用
class Date {
public int year;
public int month;
public int day;
public void setDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
public class Main {
public static void main(String[] args) {
Date date1 = new Date();
date1.setDate(1900, 10, 1);
Date date2 = new Date();
date2.setDate(2000, 10, 1);
}
}
构造和初始化
class Date {
int year = 1900;
int month = 10;
int day = 1;
public void print() {
System.out.println("年 " + year + " 月 " + month + " 日 " + day);
}
}
public class Test {
public static void main(String[] args) {
Date date = new Date();
date.print();
}
}
- 成员变量不初始化的时候给定一个默认值,引用类型为 null,boolean 类型为 false,其他基本类型为 0 值
- 构造方法进行初始化
没有返回值,方法名和类名相同
class Student {
public String name;
public int age;
public Student() {
this.name = "鼠标";
this.age = 10;
System.out.println("Student()");
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
}
}
- 显示写了构造方法就不会提供默认构造的方法
- 构造方法只有在实例化对象的时候才会被调用,因此生命周期内只调用一次
- 构造方法有什么作用呢?构造方法对对象中的成员进行初始化,不开辟空间,空间在构造之前就开辟好了
- 调用其他构造方法,只能在构造方法当中写,下图中 this 表示的就是 Student,并且这个构造方法只能出现在该构造方法的第一行,还有就是 this() 这会报错,自己调用自己
构造方法是如何被调用的呢?在进行 new 对象的时候就调用构造方法进行初始化对象的属性
- this 表示当前对象的引用,在分配完内存之后就产生了这个 this,就可以使用这个 this 了,而不是在初始化对象完之后产生
- 调用不能成环
封装
- 什么是封装?封装是数据和操作数据的方法的有机结合,隐藏对象的属性和实现的细节,仅对外公开接口和对象进行交互
访问修饰限定符
- 封装在语法层面:被 private 修饰的成员方法和成员变量只能在当前类中使用
class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.setName("张三");
System.out.println(person.getName());
}
}
包访问权限,也叫 default(什么修饰符也不写的,默认的)
包
- 包:其实是为了更好地组织和管理类的,让类不那么混乱,包可以理解为文件夹
- 包是对类和接口封装的体现,是一种对类和接口等很好的组织方式
- 为了更好的管理类,把多个类收集在一起成为一组,称为软件 包
- 在同一个工程中允许存在相同名称的类,只要处在 不同的包中即可
- 导入包中的类,例如可以导入日期类
public class Main {
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 Main {
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) {
}
}
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 导入包中静态的方法和字段。
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
double x = 30;
double y = 40;
double result = sqrt(pow(x, 2) + pow(y, 2));
System.out.println(result);
}
}
总结:包就是一个文件夹,文件夹下面有很多类,类里面有方法
和 C 语言的头文件不同,C 语言是把所有的头文件导过来生成点 i 文件,java 使用哪个类就导哪个类
自定义包
- 包一般是小写,通常会用公司的域名的颠倒形式(com.baidu.www)
- 在包下面新建一个类
- 包名要和代码路径相匹配。例如创建 com.baidu.www 的包,那么会存在一个对应的路径 com/baidu/www 来存储代码
- 如果一个类没有 package 语句,则该类被放到一个默认包中(例如 src 就是一个默认的包)
static
- 静态成员变量不属于我们的对象,静态成员变量为同一份,是所有对象共享的,是类方法
- 可以通过对象调用,也可以通过类名。静态方法名 (...) 方式调用,更推荐使用后者
- 静态成员变量存在方法区当中
- 生命周期:类加载就有了,类卸载就销毁
class Student {
public static String classRoom = "333";
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
System.out.println(Student.classRoom);
}
}
- 静态方法中不包含 this,this 属于对象的引用,而静态方法不需要对象,能用类名直接访问
- 不能在静态方法中直接访问任何非静态成员变量
代码块
- 使用 {} 定义的一段代码称为代码块
- 普通代码块、构造块、静态块、同步代码块
- 静态代码块
class Student {
public static String classRoom;
}
static { classRoom = "333"; }
public class Main {
public static void main(String[] args) {
System.out.println(Student.classRoom);
}
}
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.gender = "man";
System.out.println("I am instance init()!");
}
public void show() {
System.out.println("name: " + name + " age: " + age + " sex: " + gender);
}
}
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);
}
}
相关免费在线工具
- Keycode 信息
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
- Escape 与 Native 编解码
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
- JavaScript / HTML 格式化
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
- JavaScript 压缩与混淆
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online