zoukankan      html  css  js  c++  java
  • JAVA基础关键字 final/static/this/super

    一、      关键字final

    1)      final修饰类

    在定义类时加上final修饰符,说明该类是最终类,不能被继承,即不能当父类

    2)      final修饰变量

    final修饰的变量是最终变量,即常量。不能修改其值,在定义时必须赋值

    3) final修饰方法

    final修饰的方法是最终方法,父类包含最终方法时,子类不能覆盖该方法。

    二、      关键字static

    1) static修饰类

    static修饰的类称为静态类。

    静态类作为类的静态成员存在于某个类中。静态成员类可以不创建父类对象直接创建静态类的对象。

    2) static修饰变量

    类中static修饰的变量称为静态变量(也称类变量)。

    类的成员变量分为类变量和实例变量。实例变量属于具体的对象,类变量属于类,不同对象的实例变量被分配到不同的内存空间,而对于类的所有对象来说,类变量共享同一个内存空间。

    程序执行时,类的字节码被加载到内存中,类没有创建对象时,类变量已经被分配内存,实例变量在创建具体对象时才分配内存空间。

    3) static修饰方法

    类的成员方法分为类方法和实例方法,用static修饰的方法是类方法,否则是实例方法。实例方法只有生成对象时才分配内存。

    类方法可以通过类名直接调用,实例方法只能同过类对象调用。

    note:类方法中不能使用类的实例变量,只能使用类变量。

     

    三、      关键字this

    this关键字指类本身。

    四、      关键字super

    super关键字指父类。在子类中使用被覆盖的父类的成员变量和方法。

     

     

     

     

    class father
    {
    //final
    	public final double PI = 3.14;//定义常量PI
    	//public final int fi;   //错误,常量必须赋值。
    	public final void final_method_f()
    	{
    		System.out.println("父类:final方法不能重载");
    	}
    //static
    	static int static_in;
    	static void static_method()
    	{
    		System.out.println("父类:static方法可以通过类名直接调用");
    	}
    	static class static_InnerClass
    	{
    		void method()
    		{
    			System.out.println("method_father_inner:静态内部类可以不创建父类对象直接创建内部类对象");
    		}
    	}	
    }
    class sun extends father
    {
    	public final double PI = 6;//父类的final变量可以重载。
    	/*
    	public final void final_method_f()
    	{
    		//父类的final方法不能重载。
    	}
    	*/
    	static int static_in ;//static变量可以重载
    	static void static_method()//static方法可以重载
    	{
    		System.out.println("子类:static方法可以通过类名直接调用");
    	}
    	static class static_InnerClass//static静态内部类可以重载
    	{
    		void method_sun()
    		{
    			System.out.println("method_sun_inner:静态内部类可以不创建父类对象直接创建内部类对象");
    		}
    	}
    	void mehtod()
    	{
    		System.out.println("PI="+PI);
    		System.out.println("super.PI="+super.PI);
    	}
    }
    public class keyword
    {
    	public static void main(String args[])
    	{
    		//静态内部类可以不创建父类对象直接创建内部类对象
    		father.static_InnerClass obj= new father.static_InnerClass();
    		obj.method();
    		sun.static_InnerClass obj_s=new sun.static_InnerClass();
    		obj_s.method_sun();
    		//静态方法可以通过类名直接调用
    		father.static_method();
    		sun.static_method();
    		//静态变量可以通过类名直接调用
    		father.static_in=10;
    		sun.static_in=20;
    		System.out.println("father.static_in="+father.static_in);
    		System.out.println("sun.static_in="+sun.static_in);
    		
    		father test_father=new father();
    		test_father.final_method_f();
    		sun test_sun=new sun();
    		test_sun.final_method_f();
    		test_sun.mehtod();
    		
    	}
    
    }
    

      


     

    输出:

    method_father_inner:静态内部类可以不创建父类对象直接创建内部类对象
    method_sun_inner
    :静态内部类可以不创建父类对象直接创建内部类对象
    父类:static方法可以通过类名直接调用
    子类:static方法可以通过类名直接调用
    father.static_in=10
    sun.static_in=20
    父类:final方法不能重载
    父类:final方法不能重载
    PI=6.0
    super.PI=3.14

     

     

     

    final修饰的类不能继承、方法不能重载,变量(final修饰的变量是常量)可以重载

    static修饰的变量、方法、内部类,都可以通过类名直接调用

    static修饰的变量、方法、内部类,都可以重载。

     

     

     

  • 相关阅读:
    Leetcode264. Ugly Number II丑数2
    Leetcode221. Maximal Square最大正方形
    223. Rectangle Area
    Leetcode216. Combination Sum III组合总数3
    Leetcode200. Number of Islands岛屿的个数
    Leetcode199. Binary Tree Right Side View二叉树的右视图
    Leetcode179. Largest Number最大数
    从零开始学Linux系统(二)之基本操作指令
    从零开始学Linux系统(四)之Vi/Vim操作指令
    MySQL基础原创笔记(二)
  • 原文地址:https://www.cnblogs.com/ezhong/p/2171483.html
Copyright © 2011-2022 走看看