//静态成员变量->类变量
public static int count;
}
3.2 方法 (method)
show 方法是和 实例相关联的. 如果创建了其他实例, 那么 show 的行为就会发生变化
class Person{
public String name = “niubi”;
public int age=666;
//方法
public void show() {
System.out.println(“我叫” + name + “, 今年” + age + “岁”);
}
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
person.show();
}
}
运行结果:
3.3 static 关键字
a) 修饰属性
class Person{
//普通成员变量
public String name;
public int age;
//静态成员变量->类变量
public static int count;
}
public class TestDemo {
public static void main(String[] args) {
Person person1 = new Person();
person1.count++;
person1.age++;
System.out.println(person1.age);
System.out.println(person1.count);
System.out.println(“========================”);
Person person2 = new Person();
person2.count++;
person2.age++;
System.out.println(person2.age);
System.out.println(person2.count);
}
}
内存解析图:
b)修饰方法
如果在任何方法上应用 static 关键字,此方法称为静态方法。
- 静态方法属于类,而不属于类的对象。
- 可以直接调用静态方法,而无需创建类的实例。
- 静态方法可以访问静态数据成员,并可以更改静态数据成员的值。
class Person{
//类方法
public static void staticFunc(){
System.out.println(“static::func()”);
}
}
public class TestDemo {
public static void main(String[] args) {
Person.staticFunc();
}
}
注意事项
- 普通方法内部,不能定义静态变量
class Person{
public void eat(){
static int size = 0;//err
System.out.println(name+“正在吃饭!”);
}
public static void staticFunc(){
static int size = 10;//err
System.out.println(“static::func()”);
}
}
2.静态内部不能调用普通的方法.
class Person{
public String name;
public int age;
public void eat(){
System.out.println(name+“正在吃饭!”);
}
//类方法
public static void staticFunc(){
eat();//err
System.out.println(“static::func()”);
}
}
引用相关注意事项:
1.
class Person{
public String name;
public int age;
}
public class TestDemo {
public static void main(String[] args) {
Person person = null;//这个引用不指向任何对象
}
2.
class Person{
public String name;
public int age;
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
Person person1 = person;
}
3.
class Person{
public String name;
public int age;
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
person = new Person();
person = new Person();
person = new Person();
person = new Person();
person = new Person();
}
4.
引用一定在栈上面吗?
肯定不是!!!
class Person{
public String name;
public int age;
}
public class TestDemo {
Person person = new Person();
public static void main(String[] args) {
TestDemo testDemo = new TestDemo();
}
}
4. 封装
4.1 private实现封装
private/ public 这两个关键字表示 “访问权限控制” .
- 被 public 修饰的成员变量或者成员方法, 可以直接被类的调用者使用.
- 被 private 修饰的成员变量或者成员方法, 不能被类的调用者使用.
此时只能在当前类当中使用他–封装起来了
class Person{
private String name;
private int age;
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
person.age=18;//err
person.name = “www”;//err
}
}
4.2 getter和setter方法
class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
person.setAge(20);
person.setName(“wwww”);
System.out.println(person.getAge());
System.out.println(person.getName());
}
}
注意事项:
- getName 即为 getter 方法, 表示获取这个成员的值.
- setName 即为 setter 方法, 表示设置这个成员的值.
- 当set方法的形参名字和类中的成员属性的名字一样的时候,如果不使用this, 相当于自赋值. this 表示当前实例 的引用.
- 不是所有的字段都一定要提供 setter / getter 方法, 而是要根据实际情况决定提供哪种方法.
- 在 IDEA 中可以使用 alt + insert (或者 alt + F12) 快速生成 setter / getter 方法. 在 VSCode 中可以使用鼠标右键 菜单 -> 源代码操作 中自动生成 setter / getter 方法.
5. 构造方法
5.1 基本语法
语法规则 :
1.方法名称必须与类名称相同
2.构造方法没有返回值类型声明
3.每一个类中一定至少存在一个构造方法(没有明确定义,则系统自动生成一个无参构造)
new 执行过程:
- 为对象分配内存空间
- 调用对象的构造方法
代码示例:
class Person{
private String name;
private int age;
public Person(){
System.out.println(“Person()==不带参数的构造方法”);
}
public Person(String name){
this.name = name;
System.out.println(“Person(String)==带一个String类型的构造方法”);
}
public Person(String name , int age ){
this.name = name;
this.age = age;
System.out.println(“Person(String,int)==带两个String,int类型的构造方法”);
}
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
System.out.println(“========”);
Person person1 = new Person(“wwww”);
System.out.println(“========”);
Person person2 = new Person(“wwwzz”,18);
}
}
运行结果:
注意事项:
- 如果类中没有提供任何的构造函数,那么编译器会默认生成一个不带有参数的构造函数
- 若当前类中定义了其他的构造方法,那么,编译器就默认的无参构造将不再生成.
- 构造方法之间构成重载. 规则和普通方法的重载一致.
面试相关问题:
this 和 super 有什么区别?
this:
1. this.data 调用当前对象的属性
2. this.func 调用当前对象的方法
3. this() 调用当前对象的其他构造方法
注:this()存放在构造函数当中!
6. 认识代码块
6.1 构造代码块 和 静态代码块
构造块:定义在类中的代码块(不加修饰符)。也叫:实例代码块。构造代码块一般用于初始化实例成员变量。
class Person{
private String name;
private int age;
{
System.out.println(“实例代码块!”);
}
static{
System.out.println(“静态代码块!”);
}
public Person(){
System.out.println(“Person()==不带参数的构造方法”);
}
public Person(String name){
this.name = name;
System.out.println(“Person(String)==带一个String类型的构造方法”);
}
public Person(String name , int age ){
this.name = name;
this.age = age;
System.out.println(“Person(String,int)==带两个String,int类型的构造方法”);
}
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
System.out.println(“========”);
Person person1 = new Person();
}
运行结果:
注意事项:
1. 静态代码块不管生成多少个对象,其只会执行一次,且是最先执行的。
2. 静态代码块执行完毕后, 实例代码块(构造块)执行,再然后是构造函数执行。
静态代码块不用实例化对象,也可以被执行,只执行一次
class Person{
public static int count = 10;
static{
count = 99;
System.out.println(“静态代码块!”);
}
}
public class TestDemo {
public static void main(String[] args) {
System.out.println(Person.count);
}
}
运行结果:
注意:
如果都是静态的,那么和定义的顺序是有关的
如果当count没有初始化时,默认值就是99
7. toString方法
class Person{
//普通成员变量
public String name;
public int age;
//静态成员变量->类变量
public static int count;
public void eat(){
//static int size = 0;
System.out.println(name+“正在吃饭!”);
}
@Override
public String toString() {
return “Person{” +
“name='” + name + ‘’’ +
“, age=” + age +
‘}’;
}
//类方法
public static void staticFunc(){
System.out.println(“static::func()”);
}
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person);
}
分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
Spring Cloud实战
Spring Boot实战
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)
如果当count没有初始化时,默认值就是99
7. toString方法
class Person{
//普通成员变量
public String name;
public int age;
//静态成员变量->类变量
public static int count;
public void eat(){
//static int size = 0;
System.out.println(name+“正在吃饭!”);
}
@Override
public String toString() {
return “Person{” +
“name='” + name + ‘’’ +
“, age=” + age +
‘}’;
}
//类方法
public static void staticFunc(){
System.out.println(“static::func()”);
}
}
public class TestDemo {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person);
}
分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
[外链图片转存中…(img-wbg1GSTY-1721173612221)]
Spring Cloud实战
[外链图片转存中…(img-aWNR5IYW-1721173612222)]
Spring Boot实战
[外链图片转存中…(img-6qmcwS1K-1721173612222)]
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)