Java之初始化与清理4.1.3--final的用法

package com.javaSE07.unit2;
    //final修饰的成员变量的初始化
    //1.定义属性时用=赋值
    //2.构造方法中用=赋值
    //3.上述两条只能选择一条
    //final修饰的成员变量只能赋值一次
    //注意:有多个构造方法时,必须保证每个构造方法都会赋值
    /*this的使用
    this表示调用所在方法的对象,
    this()或者this(参数)可以调用本类中的其他构造方法,前者表示调用无参数的构造方法
    this()或this(参数)必须放在方法中的第一行,不能调用两行及以上*/
    
    
public class Final {
    //public final double PI=3.14;        //定义属性时用=赋值
    public final double PI;
    //构造方法中赋值,必须保证每个构造方法都赋值
    public Final(double pi){
        PI =pi;
    }
    public Final(){
        //System.out.println(PI);
        this(3.14);        //调用double参数的构造方法,this()必须在第一句
        //this(2.1);    //不能调用两次及以上
        System.out.println("PI="+PI);
    }
    
}
 

 

package com.javaSE07.unit2;

public class FinalTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建一个Final对象
        Final fn = new Final();
        //System.out.println();
    }

}
 

 

package com.aowin.other;

//final的使用
//1.final可以修饰变量、方法和类
//2.final修饰的变量为常量,只能赋值一次
//3.final修饰的方法不能被重写
//4.final修饰的类不能被继承

public class Son /*extends Father*/{  //final修饰的类不能被继承

}

 

package com.aowin.other;

import com.aowin.extendPermission.Father;

public class Daught extends Father {
    //成员变量
    private String name;
    private int age;
    
    {
        System.out.println(city); 
        System.out.println(money); 
        //System.out.println(name);   //默认权限的成员跨包无法继承
        //System.out.println(age);    //private的成员无法继承
    }
}
 

package com.aowin.other;

public final class Father {

}
 

转载于:https://my.oschina.net/Watto/blog/873290

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值