《java学习笔记》之面向对象--this和static

this和static


一.this

1.this是什么?

this在java中是一个关键字,表示当前对象。

从内存上来看 this保存当前对象的内存地址,指向自身。this存储在堆内存当中对象的内部。

2.注意点:

2.1. this只能使用在实例方法中。谁调用这个实例方法,this就是谁

2.2. this不能用在静态方法中!因为静态方法是类级别的方法,不存在当前对象。

2.3. this关键字大部分情况下可以省略,

但是在实例方法中,或者构造方法中,为了区分局部变量和实例变量,这种情况下:this. 是不能省略的。

3.代码中使用this.

package caopeng.javase.thisTest;

//在代码中使用this

public class Test {

public static void main(String[] args) {

Student student1= new Student(“小明”);

Student student2 = new Student(“小红”);

//谁调用readBook() this就是谁

student1.readBook();//小明is reading books

student2.readBook();//小红is reading books

}

}

class Student{

//属性

String name;

//构造方法

public Student(){

}

public Student(String name){

//这里的this不能省

this.name = name;

}

//实例方法

public void readBook(){

//this可以省略

System.out.println(this.name + “is reading books”);

}

//静态方法

public static void sit(){

//会报错

//System.out.println(this.name + “sit”);

}

}

代码中使用this()

/*

需求:

1、定义一个日期类,可以表示年月日信息。

2、需求中要求:

如果调用无参数构造方法,默认创建的日期为:1970年1月1日。

当然,除了调用无参数构造方法之外,也可以调用有参数的构造方法来创建日期对象。

*/

public class Test02 {

public static void main(String[] args) {

//无参构造

DateTest dateTest1 = new DateTest();

DateTest dateTest2 =new DateTest(2020,4,6);

//打印日期信息

dateTest1.printDate();//1970 1 1

dateTest2.printDate();//2020 4 6

}

}

class DateTest{

private int year;

private int mouth;

private int day;

//构造方法

public DateTest(){

//this()可以调用其他的构造方法

//必须放在第一行!!!

this(1970,1,1);

}

public DateTest(int year,int mouth,int day){

//this不能省

this.year = year;

this.mouth =mouth;

this.day =day;

}

//实例方法打印日期

public void printDate(){

System.out.println(this.year + " " + this.mouth + " "+ this.day);

}

}

二.static

1.static是什么?

1.1 static是一个关键字,表示静态的

1.2 所有static关键字修饰的都是类相关的,类级别的。

1.3 访问static修饰的 采用“ 类名.”的方式访问

1.4. 变量根据位置所在分为:

局部变量和成员变量

成员变量根据有没有static修饰分为

静态变量和实例变量

实例相关的:必须先new对象,再用“引用.”的方式访问

静态相关的:直接通过“类名.”的方式访问

2.代码实现

2.1 什么时候定义为静态变量,什么时候定义为实例变量呢?

如果某个属性在这个类中,所有对象都相同,那么就可定义为静态变量,可以减少内存的损耗,这个时候静态变量是存储在方法区,不是像实例变量一份一份存储在堆内存当中

public
class StaticTest02 {

public static void main(String[] args) {

Chinese chinese1 =new Chinese();

Chinese chinese2 =new Chinese(123,“???”);

System.out.println(chinese1.id);//0

System.out.println(chinese1.country);//中国

System.out.println(chinese2.id);//123

//不管你构造方法赋了什么值,都是原来的

System.out.println(chinese2.country);//中国

//不建议使用 引用. 的方式去访问静态变量,会对别的人造成误解

//建议使用 类名. 的方式

System.out.println(Chinese.country);//中国

}

}

//

class Chinese{

//每个人id不同,所以要实例变量

int id;

//中国人的国籍都是中国,所以可以定义为静态变量

//这个类的这个属性都一样

//可以减少内存开销

static String country = “中国”;

//无参构造

public Chinese(){

}

//有参构造

public Chinese(int id,String country){

this.id =id;

//country是静态变量,不能用this

//this.country

}

}

2.2什么时候定义成实例方法,什么时候定义成静态方法?

如果是要访问实例变量定义为实例方法,例如:set和get方法

如果是作为一个工具方法那么定义为静态方法,因为不同new对象,可以直接使用,方便

2.3静态代码块

public class StaticTest04 {

//静态代码块在类加载时期执行,在main方法之前

//静态代码块也遵循自上而下的顺序执行

static{

System.out.println(“1”);

}

static {

System.out.println(“2”);

}

public static void main(String[] args) {

System.out.println(“main begin”);

}

static{

System.out.println(“3”);

}

//1

//2

//3
//main begin
}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值