自学内容网 自学内容网

初学Java基础Day15---面相对象之this,static关键字,静态代码块

一,this关键字

1.概念:

        表示本对象

2.理解:

        哪个对象调用该方法,该方法里的this就表示该对象

3.作用:

        1.this.属性 : 调用本对象的成员属性

        2.this.方法 : 调用本对象的成员方法

        3.this() : 在构造方法的第一句调用本类另外的构造方法

4.代码实现
//作用2举例:
//代码详细接Day14“模拟银行用户操作余额的功能”中User类

//添加一个转账的方法
public voud transferAccount(String userName,double money){

    //获取本对象的余额,判断是否比转账的金额更多
    if(this.getSurplus() >money){

        //将当前用户余额的money部分转给对方账号
        this.setSurplus(this.getSurplus() -money);//此处this可省略
        System.out.println("已经给"+this.userName+"转账成功");
    }else{
        System.out.println("余额不足");
    }
    
}
//作用3举例:
//代码接上面“模拟银行用户操作余额的功能”中User类

public class User{
    String userName;
    String password;
    String name;
    String phone;
    char sex;
    private double surplus;//余额
    
/*    public User() {
        //在当前构造方法中调用另一个构造方法
        this("默认账号","默认密码","亚当","12388786",'男',0.0);
}
    
    public User(String username,  String name, String phone, char sex, double surplus){
        //在当前构造方法中调用另一个构造方法
        this(usename,"000000",name,phone,sex,surplus)
    }
    
*/

public User(String username, String password, String name, String phone, char sex, double surplus) {
this.username = username;
this.password = password;
this.name = name;
this.phone = phone;
this.sex = sex;
this.surplus = surplus;
}
    
    //设置余额
    public void setSurplus(double surplus){
        
        //额外的功能
double num = (surplus - this.surplus);//现在金额-原来金额
System.out.println(LocalDateTime.now() + " -- " + this.name + "用户操作了金额:" + ((num>0)?"+":"") + num);

        //设置属性
this.surplus = surplus;
    }
    
    //获取余额
    public double getSurplus(){
        
        //额外的功能
System.out.println(LocalDateTime.now() + " -- " + this.name + "用户获取了金额");

        //返回余额
        return surplus;
    }

}

二,分包

基本作用:防止了类的重名问题

项目作用:项目中有众多的类,把相同功能的类放在同一个包中,方便管理

三,static关键字

1.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.str);//bbb
        
  // 注意:静态属性属于类的属性,直接使用类名调用,使用对象调用会出现警告
       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;
}
1.概念:

1.成员属性:每个对象独享一份

2.静态属性:每个对象共享一份

2.静态属性何时创建?

使用到类,JVM会判断方法区中是否有改类的class文件,如果没有,就把改类的class文件加载到方法区,JVM会扫描该类的所有属性,并把静态属性添加到静态区中(步骤:1.开辟空间,2.赋系统默认值)。

3.静态属性何时销毁?

项目结束时,静态属性才会被销毁,所以静态属性的生命周期很长,项目中使用需谨慎

4.静态属性的应用场景

该类所有的对象需要共享的属性,就可以设置为静态

5.注意:

静态属性属于类的属性,直接使用类名调用,使用对象调用会出现警告

2.static 修饰方法
1.成员方法 VS静态方法

成员方法:必须使用对象调用,也就是说调用成员方法之前必须创建对象(开辟空间)

静态方法:属于类的方法直接使用类名调用,纯功能的方法就可以设计成静态方法如(工具类)如: Arrays

2.需求

        模仿Java的Arrays类,编写自己的MyArrays

//目的:1.掌握工具类的概念,2.回顾之前的知识点 3.理解文档注释的作用

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;
}
}
}
}

/**
 * 数组的查找,必须先排序,在查找
 * @param a 目标数组
 * @param key 需要查找的键
 * @return 如果搜索的元素包含在数组中就返回元素的下标; 否则,返回(-插入点-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]){
end = mid-1;
}else if(key > a[mid]){
start = mid+1;
}else{
return mid;
}
}
return -start-1;
}

/**
 * 拷贝数组
 * @param original 目标数组
 * @param newLength 新数组的长度
 * @return 新数组
 */
public static int[] copyOf(int[] original, int newLength){

int copyLength = original.length;
if(copyLength > newLength){
copyLength = newLength;
}

int[] newArr = new int[newLength];

for (int i = 0; i < copyLength; i++) {
newArr[i] = original[i];
}
return newArr;
}

/**
 * 拷贝区间数组
 * @param original 目标数组
 * @param from 开始下标-包含
 * @param to 结束下标 - 排他
 * @return 新数组
 */
public static int[] copyOfRange(int[] original, int from, int to){

int newLength = to-from;
int[] newArr = new int[newLength];

int index = 0;
for (int i = from; i < to; i++) {
newArr[index++] = original[i];
}
return newArr;
}

/**
 * 替换全部元素
 * @param a 目标数组
 * @param val 替换的值
 */
public static void fill(int[] a, int val){
fill(a, 0, a.length, val);
}

/**
 * 替换区间元素
 * @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 a 目标数组
 * @return 转换后的字符串
 */
public static String toString(int[] a){
String str = "[";
for (int element : a) {
if(str.length() != 1){
str += ",";
}
str += element;
}
str += "]";

return str;
}

}

四,导出,导入Jar包 导出API

五,静态代码块

都在代码里.....

//实验
public class Test01{

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

public class A{
    
    //静态属性比成员属性更早的在内存中存在
    String str1; //成员属性
   static  String str2; //静态属性
    
    //静态代码块:class文件加载到方法区是调用 
    //作用:操作静态属性,不能操作成员属性
    static{
         System.out.println("静态代码块");
    }
    
    //代码块:创建对象时优先于构造方法调用
    //作用:也可以操作成员属性和静态属性(即可以初始化属性)
    {
        System.out.println("代码块");
    }
    
    //构造方法:创建对象时调用构造方法
    //作用:操作成员属性和静态属性(即可以初始化属性)
    public A(){

        str1 = "aaa"; //底层实现:this.str1 = "aaa";
        str2 = "bbb"; //底层实现:A.str2 ="bbb";
        System.out.println("构造方法--"+str1+"---"+str2);
    }
}

经验:

1.创建对象时,在构造方法中初始化数据;

2.项目中可以在静态代码块中初始化静态属性


原文地址:https://blog.csdn.net/2401_87491344/article/details/142798760

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!