static关键字+静态代码块+继承及Arrays

static关键字+静态代码块+继承及Arrays

一、static关键字

1.含义:静态的

2.作用:

1.静态属性
2.静态方法
3.静态代码

3.static属性

1.含义:该属性属于类属性,不属于任何的对象
2.静态变量的生命周期:
创建:类加载到方法区时,系统会扫描该类的静态变量,并把静态变量加载到静态区中
销毁:程序结束时销毁
3.应用场景:该类所有的对象都共享的变量,就使用静态属性
4.注意:static修饰属性,该属性属于类的属性,直接用类名调用
public class Test01 {
	 
	public static void main(String[] args) {
		
		A a1 = new A();
		A a2 = new A();
		
		a1.str1 = "aaa";
		a2.str1 = "bbb";
		System.out.println(a1.str1);//aaa
		System.out.println(a2.str1);//bbb
		
		//a1.str2 = "xxx";
		//a2.str2 = "yyy";
		//System.out.println(a1.str2);//yyy
		//System.out.println(a2.str2);//yyy
		
		A.str2 = "xxx";
		A.str2 = "yyy";
		System.out.println(A.str2);//yyy
		System.out.println(A.str2);//yyy
	}
}
public class A {
	//属性/全局变量:成员变量 和 静态变量
	
	//成员变量
	String str1;
	
	//静态变量
	static String str2;
}

二、Arrays工具类包

import com.qf.arrays.MyArrays;
public class Test01 {
	
	public static void main(String[] args) {
		int[] is = {35,81,18,25,9,10,11,12,13};
		
		//排序 - 9,10,11,12,13,18,25,35,81
		MyArrays.sort(is);
		
		//搜索 - (注意:必须先排序,再查找,因为底层使用的是二分法查找)
		//返回元素在数组中的下标;如果元素不在数组中则返回 -插入点-1
		int index = MyArrays.binarySearch(is,35);
		System.out.println("搜索元素的下标为:" + index);
		
		//拷贝数组/扩容数组 
		int[] newIs1 = MyArrays.copyOf(is,is.length*2);
		
		//拷贝数组(开始下标-包含,结束下标-不包含)
		int[] newIs2 = MyArrays.copyOfRange(newIs1,3,6);
		
		//替换
		MyArrays.fill(newIs2,888);
		
		//将数组转换为字符串
		String str = MyArrays.toString(newIs2);
		System.out.println(str);
	}
}

/**
  * 史上最牛逼的数组工具类
  * @author 高雷
  * @version 1.0
  */
public class MyArrays {
	/**
	 * 按照数字顺序排序指定的数组
	 * @param  a 按照数字顺序排列指定数组
	 */
	public static void sort(int[] a) {
		for (int i = 0; i < a.length-1; i++) {
			for (int j = 0; j < a.length-1-i; j++) {
				if (a[j] > a[j+1]) {
					int temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}
			}
			
		}
	}
	
	/**
	 * 使用二叉搜索算法搜索指定的int数组的指定值。
	 * @param a 要搜索的数组 
	 * @param key 要搜索的值 
	 * @return 搜索键的索引,如果它包含在数组中; 否则, (-(insertion point) - 1) 
	 */
	public static int binarySearch(int[] a,int key) {
		
		int start = 0;
		int end = a.length-1;
		while (start <= end) {
			int mid = (start + end)/2;
			if (key > a[mid]) {
				start = mid +1;
			}else if(key < a[mid]){
				end = mid -1;
			}else{
				return mid;
			}
			
		}
		return - start-1;
	}
	
	/**
	 * 将指定的int值分配给指定的int数组的每个元素。
	 * @param a 要填充的数组 
	 * @param val 要存储在数组的所有元素中的值 
	 */
	public static void fill(int[] a,int val){
		for (int i = 0; i < a.length; i++) {
			a[i] = val;
		}
	}
	
	/**
	 * 将指定的int值分配给指定的int数组的指定范围的每个元素。 
	 * @param a 要填充的数组 
	 * @param fromIndex 要用指定值填充的第一个元素(包括)的索引 
	 * @param toIndex 要用指定值填充的最后一个元素(排除)的索引 
	 * @param val 要存储在数组的所有元素中的值 
	 */
	public static void fill(int[] a,int fromIndex,int toIndex,int val){
		for (int i = fromIndex; i < toIndex; i++) {
			a[i] = val;
		}
	}
	
	/**
	 * 复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
	 * @param original 要复制的数组 
	 * @param newLength 要返回的副本的长度
	 * @return 原始数组的副本,被截断或用零填充以获得指定的长度
	 */
	public static int[] copyOf(int[] original,int newLength){
		int[] is = new int[newLength];
		for (int i = 0; i < original.length; i++) {
			is[i] = original[i];
		}
		return is;
	}
	
	/**
	 * 将指定数组的指定范围复制到新数组中。
	 * @param original 要复制的数组 
	 * @param from 要复制的范围的初始索引(包括)
	 * @param to 要复制的范围的最终索引,排他。 (该索引可能位于数组之外) 
	 * @return 一个包含原始数组的指定范围的新数组,用零截取或填充以获得所需的长度
	 */
	public static int[] copyOfRange(int[] original, int from, int to){
		int[] is = new int[to-from];
		
		int index = 0;
		for (int i = from; i < to; i++) {
			is[index] = original[i];
			index++;
		}
		
		return is;
	}
	
	/**
	 * 返回指定数组的内容的字符串表示形式。
	 * @param a 要返回其字符串表示形式的数组 
	 * @return 一个字符串表示 a 
	 */
	public static String toString(int[] a){
		String str = "[";
		for (int i = 0; i < a.length; i++) {
			str += a[i];
			if (i != a.length-1) {
				str += ",";
			}
		}
		str += "]";
		return str;
	}
	
}

三、静态代码块

1.构造方法的应用场景:初始化成员变量和静态变量

2.代码块:很少用

3.静态代码块:初始化静态变量

4.静态代码块为什么不能操作成员变量:

因为静态代码块是类加载到方法区时调用的,这时对象还没有创建
public class Test01 {
	 
	public static void main(String[] args) {
		A a1 = new A();
		A a2 = new A();
	}
}
public class A {
	
	String str1;//成员属性
	static String str2;//静态属性
	
	//静态代码块:类加载到方法区时调用
	//只能初始化静态属性
	static{
		str2 = "用真心待人";
		System.out.println("A类的静态代码块--" + str2);
	}
	
	//代码块:创建对象时优先于构造方法调用
	//可以初始化成员属性和静态属性
	{
		str1 = "xxx";
		str2 = "yyy";
		System.out.println("A类的代码块" + str1 + "--" + str2);
	}
	
	//构造方法:创建对象时调用
	//可以初始化成员属性和静态属性
	public A(){
		str1 = "aaa";
		str2 = "bbb";
		System.out.println("A类的构造方法" + str1 + "--" + str2);
	}
}

四、继承

1.含义:子类继承父类的属性和方法

2.好处:解决代码的冗余

3.应用场景:分析多个类时发现,多个类有相同的属性和方法,就把相同的属性和方法抽取到父类中

需求:编写中国人和日本人的类

人类:

  •    	属性:姓名、性别、年龄
    
  •    	方法:吃饭饭、睡觉觉
    

中国人类 继承 人类:

  •    	属性:身份证
    
  •    	方法:打太极
    

日本人类 继承 人类:

  •    	属性:年号
    
  •    	方法:拍电影
    
public class Test01 {

		public static void main(String[] args) {
			
			Chinese c = new Chinese();
			
			//设置父类的属性
			c.name = "张三丰";
			c.sex = '男';
			c.age = 120;
			
			//设置子类的属性
			c.id = "123456789";
			
			//获取父类的属性
			System.out.println(c.name);
			System.out.println(c.sex);
			System.out.println(c.age);
			
			//获取子类的属性
			System.out.println(c.id);
			
			//调用父类的方法
			c.eat();
			c.sleep();
			
			//调用子类的方法
			c.playTaiji();
			System.out.println("----------------------------");
			
			Japanese j = new Japanese();
			
			//设置父类的属性
			j.name = "波多";
			j.sex = '女';
			j.age = 18;
			
			//设置子类的属性
			j.yearNum = "昭和";
			
			//获取子类的属性
			j.yearNum = "昭和";
			
			//获取父类的属性
			System.out.println(j.name);
			System.out.println(j.sex);
			System.out.println(j.age);
			
			//获取子类的属性
			System.out.println(j.yearNum);
			
			//调用父类的方法
			j.eat();
			j.sleep();
			
		    //调用子类的方法
			j.playVideo();
		}
}

//父类
public class Person {
	
	String name;
	char sex;
	int age;
	
	public void eat(){
		System.out.println("吃饭饭");
	}
	
	public void sleep(){
		System.out.println("睡觉觉");
	}
}
//            子类	      继承	   父类
public class Chinese extends Person {

		String id;
		
		public void playTaiji(){
			System.out.println("打太极");
		}
}

public class Japanese extends Person {

		String yearNum;
		
		public void playVideo(){
			System.out.println("拍视频");
		}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨霖先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值