Java in the final with the static keyword

A summary .final
1.final modified Class: represents the class can not be inherited
final not modified interface Interface
2.final modification methods: indicates that the method can not be overridden by class (i.e., the parent class final subclass can override can be inherited)
final not modified constructor
3.final modification field properties: that they can not be modified property values first initialized
final properties can be initialized directly in the constructor or initialization
if the attribute is directly initialized, the value of other functions (including constructors) can not be modified
4. final modification function parameter
argument function can not be modified
5.final modified function local variables
can not be modified after the first local variable is initialized
public void Hello () {
final String name;
name = "Hi"; OK //
name = "Hello"; // error
}
Use final meaning:
The reason to use the final method, may be out of consideration for two reasons. The first method is "locked" to prevent any inheriting class change its original meaning. When designing the program. If you want a method of behavior remains unchanged during inheritance and can not be overwritten or rewritten, you can take this approach.
The method adopted final second reason is the efficiency of program execution. After a method is set to final, the compiler can put all of that method calls are placed "embedded" in the call.
Two .static summary
1.static modified member function: This function can not use this object member
can not be modified static constructors
static parameters can not be modified function
static local member variable is not modified
2.static modifications member field
when the class is loaded when a virtual machine, first static member fields are initialized in the order field declaration
3.static modified block of statements
when the class is loaded VM, in declaration order has to initialize static member fields and static statement block
The static fields are modified and the method only belong to the class, and all shared objects.
You can not use non-static fields in the static member function and the modified statement block.
Global variables can not be defined directly in JAVA, is achieved through the static
is not const in Java, not directly define constants, is achieved by static final
 
import java.util.*;
/**
*@displays: java关键字的复习
*@auther: http://www.javaweb.cc
*@version: lv 1.0.0
@time:2010-1-31
**/
public class Test{
public static void main(String[] args){
/********************static的使用*********************/
/*
TestStatic a = new TestStatic();
TestStatic b = new TestStatic();
System.out.println(a.num1);
a.num1 = 20;
System.out.println(b.num1);
TestStatic.num1 = 21;
System.out.println(TestStatic.num1);
TestStatic.doSm();
*/
/*****************************************************/
/ ******************* final use ******************************************************** /
/*
TestFinal fl = new TestFinal();
System.out.println(fl.num2);
System.out.println(fl.num3);
*/
/*
TestFinal fl1 = new TestFinal();
fl1.fun1();
fl1.fun1(1);
fl1.fun1(1,1);
//fl1.fun1(1,1,1);
TestFinal fl2 = new TestExtFinal();
fl2.fun1();//可以访问父类的final方法
fl2.fun1(1);//可以访问父类的final方法
fl2.fun1(1,1);//调用重写的父类方法
//fl2.fun1(1,1,1);//不能访问父类的私有方法
//fun2调用的区别
//fl2.fun2();//TestFinal fl2 = new TestExtFinal();这种方式实例化的类不能调用自己的final方法
TestExtFinal fl3 = new TestExtFinal();
fl3.fun2();//TestExtFinal fl3 = new TestExtFinal();这种方式实例化的类可以调用自己的final方法
*/
/*****************************************************/
/*
Pair<Object> p = new Pair<Object>(23,"zxfonline");
System.out.println(p.first() + " " + p.second());
for(String s : p.stringList()){
System.out.print(s + " ");
}
*/
/**********************abstract的使用*********************/
/*
AbsClass extAbsClass = new ExtAbsClass1();
extAbsClass.fun1();//调用实现了抽象类的抽象方法
extAbsClass.fun3();//调用没有重写的抽象类的普通方法
extAbsClass.fun4();//调用重写了抽象类的普通方法
AbsSonClass extAbsSClass = new ExtAbsClass2();
extAbsSClass.fun1();//调用实现了抽象类的抽象方法
extAbsSClass.fun3();//调用没有重写的抽象类的普通方法
extAbsSClass.fun4();//调用重写了抽象类的普通方法
*/
/*****************************************************/
/*********************interface的使用********************/
ClsIplIntrfc cl = new ClsIplIntrfc();
cl.fun1();
cl.fun3();
/*****************************************************/
}
}
/**
*关键字static --- 只能修饰方法或属性或作为一个语句块且这个语句块只能有变量不能有方法
**/
class TestStatic{
static int num1 =19;
static int num2 ;
TestStatic(){
num2 = 10;
System.out.println("TestStatic Constructor num2 = " + num2);
}
//static定义的变量会优先于任何其它非static变量,不论其出现的顺序如何
static{
num2 = 11;
System.out.println("TestStatic static num2 = " + num2);
//static 语句块中不能有方法
/*
void show(){
System.out.println("TestStatic static num2 = " + ++num2);
}
*/
}
//静态方法直接用--- 类名.方法名
static void doSm(){
System.out.println("static function sm of class B");
}
}
/**
*关键字 final --- 修饰变量时一次赋值不能被修改 修饰类的时候该类不能被继承(太监类)、修饰方法时不能被重写但是可以重载
**/
class TestFinal{
//final修饰的成员变量没有默认值
//一次赋值不能被修改(赋值方法一)
final int num2 =19;
//一次赋值不能被修改(赋值方法二)
final float num3;
TestFinal(){//不写访问修饰符
num3 = 10f;
}
final void fun1(){
System.out.println("TestFinal final function fun1 test");
}
//fun1重载
final void fun1(int num){
System.out.println("TestFinal final function fun1 reload test");
}
//fun1重载
void fun1(int num1,int num2){
System.out.println("TestFinal nomal function fun1 reload test");
}
//fun1重载
private final void fun1(int num,int num2, int num3){
System.out.println("TestFinal final function fun1 reload test");
}
}
final class TestExtFinal extends TestFinal{//可以把修饰TestExtFinal 类的 final 删掉
/*
//不能重写父类方法
final void fun1(){
System.out.println("TestExtFinal extends TestFinal final function's fun1 test");
}
*/
//重写父类方法
void fun1(int num1,int num2){
System.out.println("TestExtFinal extends TestFinal nomal function fun1 rewrite test");
}
final void fun2(){
System.out.println("TestExtFinal final function's fun2 test");
}
}
class Pair<T>{
private final T first;
private final T second;
public Pair(T first ,T second){
this.first = first;
this.second = second;
}
public T first(){
return first;
}
public T second(){
return second;
}
public List<String> stringList(){
return Arrays.asList(String.valueOf(first),String.valueOf(second));
}
}
/**
*关键字 abstract --- 修饰类时不一定有抽象方法,但是有抽象方法的类一定是抽象类,不能修饰变量
*抽象类允许有普通方法和变量 抽象方法不能有方法体且它的实现类必须的实现它的所有抽象方法
*子类只能继承一个抽象类(父类)
**/
abstract class AbsClass{
String str;
//abstract int num;
//抽象方法
abstract void fun1();
//抽象方法
abstract void fun2();
//普通方法
void fun3(){
System.out.println("nomal function fun3");
}
//普通方法
void fun4(){
System.out.println("nomal function fun4");
}
}
class ExtAbsClass1 extends AbsClass{
void fun1(){
System.out.println("Extends abstract class AbsClass's abstract Function fun1");
}
void fun2(){}
//重写抽象类的fun4方法
void fun4(){
System.out.println("Extends abstract class AbsClass's nomal function fun4");
}
}
abstract class AbsSonClass extends AbsClass{
abstract void fun5();
}
class ExtAbsClass2 extends AbsSonClass{
void fun1(){
System.out.println("Extends abstract class AbsClass's abstract Function fun1");
}
void fun2(){}
void fun5(){}
//重写抽象类的fun4方法
void fun4(){
System.out.println("Extends abstract class AbsClass's nomal function fun4");
}
}
/**
*关键字 interface --- 只能修饰类 接口的方法不能有实现体,子类可以实现多个接口, private protected static 不能用来修饰接口
**/
interface Infc{
public static final int num1 = 0;//public static final 三个顺序可以交换 也可以 写一个 写两个 或不写(默认为 public static final 类型)
void fun1();
}
class ClsIplIntrfc implements Infc{
public void fun1(){
System.out.println("实现接口的方法");
}
void fun2(){
System.out.println("zxfonline");
}
void fun3(){
this.fun2();
}
}
发布了965 篇原创文章 · 获赞 11 · 访问量 3万+

Guess you like

Origin blog.csdn.net/xiaoyaGrace/article/details/105285338