您当前的位置: 首页 >  Java

星夜孤帆

暂无认证

  • 1浏览

    0关注

    626博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java学习笔记(17)-static和final

星夜孤帆 发布时间:2018-05-22 00:30:34 ,浏览量:1

static final      static:静态的,只能在类内部使用,可以修饰:属性,方法,内部类,静态代码块     静态修饰的成员:属于类级别的成员,是全体类实例共享的资源     静态属性:只有一份,全体实例共享,类似于全局变量     静态方法:属于类的方法,使用类名直接调用,不需要创建对象,而且静态方法中没有隐含参数this,     不能访问当前对象资源    静态代码块:在类加载期间执行,只执行一次,可以用于资源的加载    final:最终的,可以修饰:类,方法,变量(属性,局部变量)    final的类:不能再继承    final的方法:不能再重写     final的方法和类,阻止了动态代理模式!     动态代理模式广泛的应用在:Spring Hibernate Struts2     企业编程规范:不允许使用final的方法和类!     final的变量:final变量只能初始化一次,不能再修改!常量:  public static final double PI = 3.14;

package day03;
/*
 * 静态变量:静态变量只有“一份”
 * 1 静态变量在类加载期间初始化,存在方法区中
 * 2 是全体对象共享的一份变量
 * 3 经常使用类名访问静态变量
 */
public class Demo07 {
	public static void main(String[] args) {
		Cat c1 = new Cat(5);
		Cat c2 = new Cat(6);
		System.out.println(c1.age+","+c2.age+","+Cat.numOfCats);
	}
}
class Cat{
	int age;//实例变量,每个对象有一份
	static int numOfCats = 0;//静态变量,只有一份
	public  Cat(int age){
		this.age = age;
		numOfCats++; 
	}
}

package day03;



/*
 * 静态方法
 * 静态方法使用类名调用,是属于类的方法
 * 静态方法中没有this变量,不能访问当前对象
 * 静态方法一般用于与当前对象无关的工具方法
 * 如:Math.sqrt() Arrays.sort()
 */
public class Demo08 {
	public static void main(String[] args) {
		Point p1 = new Point(3,4);
		Point p2 = new Point(6,8);
		//在对象上调用方法,当前对象隐含传递给隐含参数this
		System.out.println(p1.distance(p2));//distance(p1,p2)
		double d = Point.distance(p1,p2);
		//静态方法调用时候不传递隐含的当前对象参数
	}
}
class Point{
	int x;
	int y;
	public Point(int x,int y){
		this.x = x;
		this.y = y;
	}
	//静态方法中没有隐含参数this!在静态方法中没有this
	//在静态方法中不能访问this的属性和方法!
	public static double distance(Point p1,Point p2){
		int a = p1.x - p2.x;
		int b = p1.y - p2.y;
		return Math.sqrt(a*a+b*b);
	}
		//计算当前点(this)到另外一个点(other)的距离
	public double distance(/*Point this*/Point other){
		int a = this.x - other.x;
		int b = this.y - other.y;
		double c = Math.sqrt(a*a+b*b);
		return c;
	}
	
}

package day03;
/*
 * final类不能继承子类型了, 可以防止子类对父类型的修改
 * 工作中很少使用final的类
 */
public class Demo09 {
	public static void main(String[] args){
		
	}
}
final class Coo{}
//class Uoo extends Coo{}//编译错误final类不能继承子类
//class MyString extends String{}//编译错误
//class MyInt extends Integer{}
//class MyMath extends Math{}
package day03;
/*
 * final 的方法不能被子类重写了
 */
public class Demo10 {
	public static void main(String[] args) {
	}
}
class A{
	public final void test(){
	}
}
class B extends A{
	//public void test(){}//编译错误,子类不能重写父类final方法
}
package day03;
/*
 * true false null 不是关键字!是字面量
 * 关键字:if else while for class public
 * final 修饰的变量
 * final的变量只能初始化(赋值一次),不能再修改!
 */
public class Demo11 {
	public static void main(String[] args){
		//final修饰的局部变量
		final int a;
		a = 5;//初始化!
		//a = 8;//编译错误
		int b = 5;
		b = 100;
		b++;
//		this.test(2,3);//非静态方法用对象来掉,而现在再静态方法里没有this
		Demo11.test(5, 6);
//		test(5,6);//同一个类里,类名可以省略,静态只能调静态的
	}
	public  static void test(final int a,int b){
		//a++;//编译错误,不能再修改
		System.out.println(a);
		b++;
	}
}

package day03;
/*
 * final表示初始化不能再修改
 * static表示只有一个
 * static final 共同修饰的叫常量
 * 常量:PI是直接数的代名词,是名字
 * 字面量:直接写出数值3.1415926535897
 * 宏观说:字面量和常量都成为常量!
 */
public class Demo12 {
	public static final double PI = 3.1415926535897;
	public static void main(String[] args) {
		Dog d1 = new Dog();
		Dog d2 = new Dog();
		//d1.id = 8;
		System.out.println(d1.id+","+d2.id+","+Dog.numOfDogs);
	}
}
class Dog{
	final int id;//实例变量,每个对象一份,不能再次修改
	static int numOfDogs=0;//静态,只有一份
	public Dog(){
		id = numOfDogs++;
	}
}
package day04;


public class Demo02 {
	public static void main(String[] args) {
		Foo f = new Koo();
	}
}
class Foo{
	int a = 1;
	public Foo(){this.test();}
	private void test(){
		//私有方法不能继承,也不能重写
		System.out.println("Foo test() "+a);
	}
}
class Koo extends Foo{
	int b = 2;
	public void test(){
		System.out.println("Koo test()"+a+", " +b);
	}
}
package day04;

public class Demo03 {
	public static void main(String[] args) {
		new Goo();
		new Goo();
	}
}
class Goo{
	{//代码块,在创建对象时候执行!类似于构造器的作用
		System.out.println("HI");
	}
	static{//静态代码块,在类的加载期间执行,只执行一次
		System.out.println("loading Goo.class");
	}
}
package day04;
import java.util.Arrays;
/*
 * final变量只能初始化一次,不能再修改!
 */
public class Demo04 {

	public static void main(String[] args) {
		final String s = "ABC";
//		s = "A";
		final String[] ary = {"a","b"};
		//ary:数组变量,ary[0]数组元素
		ary[0] ="Tom";//数组元素可以修改
//		ary = null ;数组变量不能修改
		System.out.println(Arrays.toString(ary));
		final Hoo h = new Hoo();
		h.a = 8;
		//h.b = 9;
		//h = null;
	}
}
class Hoo{
	int a = 4;
	final int b = 8;
}
关注
打赏
1636984416
查看更多评论
立即登录/注册

微信扫码登录

0.0403s