Java笔记——finalize、static、abstract、final、this关键字

1、对象的生命周期

(1)finalize

package cn.sdut;

public class AA {

 int x=100;
 int y=100;
 
 public AA() {
  x=10;
  y=20;
 }
 
 public AA(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }
 

 
 
 @Override
 protected void finalize() throws Throwable {
  System.out.println("AA finalize......");  
  super.finalize();
 }

 public void mm()
 {
  System.out.println(x+"=="+y);
 }
 
 public static void main(String[] args) {
  AA aa=new AA();
  aa.mm();
  //................
  
  aa=null;
  
  System.gc();
 }
}

//(2)static

package cn.sdut;

public class AA {

 static{  //静态代码块   一个程序中,静态代码块会最先执行
  System.out.println("static block ......");
 }
 
 
 static  int x=100;  //类变量,静态变量
  int y=100;
 
 public AA() {
  
  y=20;
 }
 
 public AA(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }
 
 @Override
 protected void finalize() throws Throwable {
  System.out.println("AA finalize......");  
  super.finalize();
 }

 public static void mm()    //类方法,静态方法
 {
     System.out.println(x+"==");
 }
 
 public static void main(String[] args) {
  AA aa=new AA();
     System.out.println(AA.x);
     x+=20;
     AA bb=new AA();
     System.out.println(AA.x);
     System.out.println(AA.x);
     AA.mm();
 }
}

//2、abstract

//(1)抽象类不能实例化;(2)抽象类里面可以定义抽象方法,也可以定义非抽象方法;(3)含有抽象方法的类一定是抽象类。

package cn.sdut;

public abstract class AA {

 static{
  System.out.println("static block ......");
 }
 
 
 static  int x=100;
  int y=100;
 
 public AA() {
  
  y=20;
 }
 
 public AA(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }
 
 @Override
 protected void finalize() throws Throwable {
  System.out.println("AA finalize......");  
  super.finalize();
 }

 public abstract void mm();
 public void mm2(){};
 
 public static void main(String[] args) {
  
 }
}

3、final

修饰变量,赋值次数只有1次;

修饰方法,子类不能重写(覆盖)方法。

package cn.sdut;

public  class AA {
 
    final int x;
 int y=100;
 
 public AA() {
  x=20;
  y=20;
 }
 
 public AA(int x, int y) {
  super();
  this.x = x;
  this.y = y;
 }


 public final void mm2(){};
 
 public static void main(String[] args) {
  final int a=10;
  
 }
}

4、this

package cn.sdut;

public  class AA {
 
    final int x;
 int y=100;
 int z;
 
 public AA() {
  this(20,20); 
  z=10;  
 }
 
 public AA(int x, int y) {
  this.x = x;
  this.y = y;
 }


 public  void mm2(){
  System.out.println(this.x+"=="+this.y);
 };
 
 public  void mm3(){
  this.mm2();
 };
 
 public static void main(String[] args) {
 }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值