JAVA自学教程之(关键字三final:针对extends打破封装性)



JAVA自学教程之(关键字三final:针对extends打破封装性)。

final:
final 可以修饰类、方法、变量

final 修饰的类不可以被继承

final 修饰的方法不可以被覆盖 final 修饰的变量是一个常量,只能被修饰一次 内部类只能访问被final修饰的局部变量
继承的弊端:

如下代码:

 

  1. class father
  2. {
  3.     void show()
  4.     {
  5.         System.out.println(“ni hao “);//父类的方法已经调用了底层系统
  6.     }
  7. }
  8. class son extends father
  9. {
  10.     void show()
  11.     {
  12.         System.out.println(” no “);//子类一覆盖,父类原有的功能挂了
  13.     }
  14. }
  15. public class Main
  16. {
  17.     public static void main(String[] args)
  18.     {
  19.         son s = new son();
  20.         s.show();
  21.     }
  22. }
class father
{
	void show()
	{
		System.out.println("ni hao ");//父类的方法已经调用了底层系统
	}
}
class son extends father
{
	void show()
	{
		System.out.println(" no ");//子类一覆盖,父类原有的功能挂了
	}
}
public class Main
{
	public static void main(String[] args)
	{
		son s = new son();
		s.show();
	}
}

故:继承有弊端,它打破了封装性。
所以针对继承的弊端,采用final关键字,final class father{},那么father类就无法被继承
但是想要继承类,但是不想让其中某些方法被继承,那么就用final修饰方法,那么方法就无法被继承

 

还有final修饰的变量,final x = 9变成一个常量,也就是把该变量固定住了,类似于const int  x = 9;

且只能被赋值一次

 

  1. class father
  2. {
  3.    void show()
  4.     {
  5.        final int x = 9;
  6.        x = 10;//编译失败
  7.         System.out.println(x);
  8.     }
  9. }
class father
{
   void show()
	{
	   final int x = 9;
	   x = 10;//编译失败
		System.out.println(x);
	}
}
  1. class father
  2. {
  3.    void show()
  4. <span style=”white-space:pre”>    </span>{
  5.      final int x;//编译失败,因为没有初始化,对于成员而言,有默认初始化值,而final固定的是显示初始化值
  6.         System.out.println(x);
  7.     }
  8. }
class father
{
   void show()
<span style="white-space:pre">	</span>{
	 final int x;//编译失败,因为没有初始化,对于成员而言,有默认初始化值,而final固定的是显示初始化值 
		System.out.println(x);

	}
}

 

 

  1. class father
  2. {
  3.    void show()
  4.     {
  5.        final int x;
  6.        x = 10;
  7.         System.out.println(x);
  8.         x = 10;//编译失败
  9.     }
  10. }
class father
{
   void show()
	{
	   final int x;
	   x = 10;
		System.out.println(x);
		x = 10;//编译失败
	}
}

用final 修饰的不行变化的变量,在开发中常用

变量的书写规范

java中书写格式:


变量:如果由多个单词所组成,第一单词首字母小写,从第二个单词首字母开始每个单词首字母大写,书写格式和函数一样

常量:所有字母都大写,如果单词不唯一,单词与单词之间用下划线连接 double  MY_PI;

 

如果某个值固定不变,就没必要每个对象都知道,那么这个数据就说明可以是被共享的

 

  1. class father
  2. {
  3.     static final int x = 8;//所以可以用static修饰,成员如果被final了,一般都会加static
  4.    void show()
  5.     {
  6.         System.out.println(x);
  7.     }
  8. }
class father
{
	static final int x = 8;//所以可以用static修饰,成员如果被final了,一般都会加static
   void show()
	{

		System.out.println(x);
	}

}

这样就可以直接被类名使用

  1. class father
  2. {
  3.     static final int x = 8;
  4. }
  5. public class Main
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         System.out.print(father.x);
  10.     }
  11. }
class father
{
	static final int x = 8;

}
public class Main
{
	public static void main(String[] args)
	{

		System.out.print(father.x);
	}
}

如果只在本类中使用

 

 

  1. class father
  2. {
  3.     private static final int x = 8;
  4. }
class father
{
	private static final int x = 8;

}

 

全局常量

 

  1. class father
  2. {
  3.     public static final int x = 8;
  4. }
class father
{
	public static final int x = 8;

}

 

为什么要用final修饰变量,在程序中如果一个数据是固定的,那么直接使用这个数据就可以了,但是这个数据的阅读性差,这个数据还要屡次写,一旦要更改数据,书写起来较麻烦,所以加final 给这个数据起个名字,这个名字就代表了这个数据,书写方便,方便修改

这个链接关于final的介绍写的很好,以上只是个人总结,肯定存在不足之处。