关于java语言当中this关键字:
1.this==这个
2.this是一个引用,this是一个变量,this变量中保存的地址指向自身,this存储在JVA堆内存Java对象内部。
3.创建100个java对象,每一个对象都有this,也就是有100个不同的this
4.this可以出现在“实例方法”当中,this指向当前正在执行的这个动作的对象(this指代当前对象)
5.this在多少情况下可以省略不写
this可以使用在哪里?
1.可以使用在实例方法中,代表当前对象【语法格式:this.】
2.可以使用在构造方法当中,通过当前的构造方法调用其他的构造方法【语法格式:this(实参)】
重点:this()这个方法只能使用在构造方法第一行
例1:
public class Customer {
//姓名
String name;//实例变量
//构造方法
public Customer() { //实例方法
}
//不带有static关键字的一个方法
//顾客购物的行为
//每一个顾客购物的东西都不一样
//所以购物这个行为是对象级别行为
//重点:没有static关键字的方法被称为"实例方法" 访问: "引用."需要new
//重点:没有static关键字的变量被称为"实例变量"
//当一个动作执行过程需要对象参与的,不用带static关键字
public void shopping() {
//System.out.println(c1.name+"在购物"); 此次应该用到引用 但是c1不可取c2也不可去
System.out.println(name+"在购物");
System.out.println(this.name+"在购物");//结果一样
//c1去访问shopping是 this直接变成c1 c2去访问shopping是 this直接变成c2
//this可以省略不写
}
}
public class CustomerTest {
public static void main(String[] args) {
//创建Customer 对象
Customer c1 = new Customer();
c1.name = "张1";
c1.shopping();
//再创建一个Customer 对象
Customer c2 = new Customer();
c2.name = "胡2";
}
}
例二:
public class User {
private int id;
private String name;
//构造方法
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
/*
public void setId(int a) {
id = a;
}*/
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ThisTest03 {
public static void main(String[] args) {
User u1 = new User(10011,"牛牛");
System.out.println(u1.getId());
System.out.println(u1.getName());
}
}
例3:
/*
* 自定义日期类型
*
* 需求:当程序员调用以下无参的构造函数的时候,默认日期是1970-1-1
*/
public class Date {
private int year;
private int month;
private int day;
//构造函数
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public Date() {
//this(2000,1,24); //this();只能使用在构造方法第一行
this.year = 1970;
this.month = 1;
this.day = 1;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
//对外提供应该方法可以将日期打印输出到控制台
public void print() {
System.out.println(this.year+"年"+this.month+"月"+this.day+"日");
}
}
public class DateTest {
public static void main(String[] args) {
//日期对象1
Date d = new Date();
d.print();
//日期对象2
Date d2 = new Date(1999,10,25);
d2.print();
}
}
**
关于java语言当中static关键字:
1.static == 静态;
2.static修饰的是静态方法
3.所有static修饰的元素都称为是静态的,都可以使用类名.的方式使用,也可以使用引用.的方式访问
public class Test {
public static void main(String[] args) {
Test.doSome();
doSome();
Test t = new Test();
t.doSome();
//引用是空
t = null;
t.doSome(); //这里不会出现空指针异常。
//带有static的方法,其实既可以采用类名的方式访问,也可以采用引用的方式访问
//但是即使采用引用的方式访问,实际上执行的时候和引用指向的对象无关。
}
public static void doSome() {
System.out.println("do some 1 !!!!");
}
}
例1:中国人类
什么时候成员变量声明为实例变量?
- -所有对象都有这个属性,但是这个属性值会随着对象的变化而变化
什么时候成员变量声明为静态变量?
- -所有对象都有这个属性,并且这个属性的值是一样的,建议定义为静态变量。
public class Chinese {
//实例变量
private String id;
private String name;
//private String country;
//国际【所有对象国籍一样,这种特征属于类级别的特征,可以提升为整个模板的特征,在变量前使用static关键字修饰】
//静态变量,存储在方法区内存中
static String country ;
//显然实例变量的特点:无论创建多少个对象,每个对象都有不同的id name country
//缺点: 中国人对象 国籍空间反复使用
public Chinese(String id, String name) {
super();
this.id = id;
this.name = name;
this.country = "中国";
}
public void print() {
System.out.println(this.id +" "+this.name+" "+Chinese.country );
}
}
public class ChineseTest {
public static void main(String[] args) {
//创建中国人对象1
Chinese zhangsan = new Chinese("14270123456789","张三");
zhangsan.print();
//创建中国人对象1
Chinese lisi = new Chinese("14270123456789","李四");
lisi.print();
System.out.println(Chinese.country);
System.out.println(zhangsan.country);
}
}
可以使用static关键字来定义静态代码快
语法结构:
static {
Java语句;
}
2.静态代码块在类加载时执行,并且只执行一次
3.可以编写多个,并且遵循自上而下的方法执行
4.静态代码块的作用?
- -和具体的需求有关,完成日志的记录
- -特殊时刻:类加载时刻
5.通常在静态代码上,初始化设置
public class StaticTest01 {
static {
System.out.println("类加载。。");//在main方法前执行
}
public static void main(String[] args) {
System.out.println("mian 开始执行");
}
}
关于Java中的final关键字
关于Java语言当中final关键字:
-
1.final==最终的,不可变的;
-
2.final修饰的类无法被继承
-
3.final修饰的方法无法被覆盖
-
4.final修饰的实例变量,必须手动赋值,不能采用系统默认值
-
5.final修饰的引用,一旦指向某种对象之后,不能指向其他类,指向的对象,无法被Java回收器回收;
public class FinalTest01 {
public static void main(String[] args) {
final int k = 100;
//k=20;
final int m;
m= 1;
//m= 2; 不可二次赋值
}
}
/*
* 实例变量
*/
public class FinalTest02 {
//实例变量使用final关键字必须手动赋值
final int age = 10;
final int num; //使用构造方法默认赋值
public FinalTest02(){
this.num = 20;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
//返回到中国人类
国籍是中国 不仅可以使用static也可以使用final修饰
当final和static关键字联合使用,被称为“常量”。
常量的定义语法格式:
public static final 类型 常量名 = 值;
java 中要求所有常量名全部大写,每个单词之间使用下划线连接
public static final String GUO_JI = “中国”;