Java基础(七)

八.static关键字

一.static关键字修饰成员

Demo01.java</

package Day08.Demo03;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/21 13:19
 */
public class Demo01 {
    public static void main(String[] args) {
        Student one = new Student("张丹",18);
        one.room = "104教室";
        System.out.println("姓名是:"+one.getName()+
                ",年龄是:"+one.getAge()+
                ",教室是:"+one.room+",学号是:"+one.getId());
        Student two = new Student("李思思",28);
        System.out.println("姓名是:"+two.getName()+
                ",年龄是:"+two.getAge()
                +",教室是:"+two.room+",学号是:"+two.getId());

    }
}

Student.java

package Day08.Demo03;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/21 13:20
 */
public class Student {
    private  int id;//学号
    private String name;
    private  int age;
    static  String room;
    private static int idCoder = 0;//学号计数器,每当new一个新对象的时候,计数器++



    public  Student(){
        idCoder++;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
        this.id=++idCoder;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

二.static关键字修饰方法

注意事项:

1.静态不能直接访问非静态
* 原因:因为在内存当中是【先】有的静态内容,【后】有的非静态内容
*
* 2.静态方法中不能有this* 原因:this代表当前对象,通过谁调用的方法,谁就是当前对象。

Demo02method.java

package Day08.Demo03;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/21 13:37
 */

/*
* 一旦使用static修饰成员方法,那么这就成为了静态方法,
* 静态方法不属于对象,而是属于类。
* 如果没有static关键字,那么必须首先创建对象,然后通过对象才可以使用它。
*
*
* 无论是成员变量,还是成员方法,如果有了static,都推荐使用类名称进行调用。
* 静态变量:类名称.静态方法。
* 静态方法:类名称.静态方法()
*
*
* 注意事项:
* 1.静态不能直接访问非静态
* 原因:因为在内存当中是【先】有的静态内容,【后】有的非静态内容
*
* 2.静态方法中不能有this,
* 原因:this代表当前对象,通过谁调用的方法,谁就是当前对象。
* */
public class Demo02method {
    public static void main(String[] args) {
        MyClass obj = new MyClass();//创建对象
        //然后才可以使用没有static关键字的内容
        obj.method();

        //对于静态方法来说,可以通过对象名进行调用,也可以直接通过类名称来调用。
        obj.methodStatic();
        //正确,不推荐,这种写法在编译后也会被javac翻译成为"类名称。静态方法名 "
        MyClass.methodStatic();//正确。推荐


        //对于本类当中的静态方法,可以省略类名称
        myMethod();
        Demo02method.myMethod();//完全等效
    }
    public static void myMethod(){
        System.out.println("这是自己的方法!");
    }

}

MyClass.java

package Day08.Demo03;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/21 13:35
 */
public class MyClass {

    int num;//成员变量
    static int numStatic;//静态方法
    public void method(){
        System.out.println("这是一个成员方法。");
        //说明成员方法可以访问成员变量
        System.out.println(num);
        //说明成员方法可以访问静态变量
        System.out.println(numStatic);

    }
    public static void methodStatic(){
        System.out.println("这是一个静态方法。");

        //说明静态方法可以访问静态变量
        System.out.println(numStatic);
        //说明静态方法不能直接可以访问非静态【重点】
//        System.out.println(num);//错误写法

        //静态方法中不能使用this关键字。
//        System.out.println(this);//错误写法,
    }
}

三.静态static的内存图

注意事项:

根据类名称访问静态成员变量的时候,全程和对象就没关系,之和类有关系。

Demo03Student.java

package Day08.Demo03;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/23 9:54
 */
public class Demo03Student {
    public static void main(String[] args) {
        //设置一下教室,静态的,使用类名称进行调用
        Student.room = "101教室";
        Student one = new Student("郭靖",36);
        System.out.println("one的姓名:"+one.getName());
        System.out.println("one的年龄:"+one.getAge());
        System.out.println("one的教室:"+Student.room);


        System.out.println("========================");
        Student two = new Student("黄蓉",22);
        System.out.println("two的姓名:"+two.getName());
        System.out.println("two的年龄:"+two.getAge());
        System.out.println("two的教室:"+Student.room);



    }
}

在这里插入图片描述

四.静态代码块

格式:

public class 类名称{
    static{
        //静态代码块内容
    }
}

特点:

当第一次用到本类时,静态代码执行唯一的一次,

静态内容优先于非静态,所以静态代码块比构造方法先执行。

静态代码块的典型用途:

用来一次性对静态成员变量进行赋值。

例子:

Demo04Static.java

package Day08.Demo03;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/23 10:18
 */

/*静态代码块的格式是:

* public class 类名称{
    static{
        //静态代码块内容
    }
    }
    特点:当第一次用到本类时,静态代码执行唯一的一次,静态内容优先于非静态,所以静态代码块比构造方法先执行。
*/
public class Demo04Static {
    public static void main(String[] args) {
        Person one = new Person();
        Person two = new Person();
    }
}

九.Arrays类

一.数组工具Arrays

/**
 * java.util.Array是一个与数组相关的工具类,里面提供了大量的静态方法,用来
 * 实现数组常见的操作
 *
 * 常用的方法:
 * 1.public static  String toString(数组),将参数数组变成字符串,(按照默认格式,【元素1,元素2,元素3……】)
 * 2.public static void sort(数组),按照默认升序(从小到大)对数组的元素进行排序
 *
 *备注:
 * 1.如果是数值,sort默认按照升序从小到大。
 * 2.如果是字符串,sort默认按照字母升序。
 * 3.如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparable接口的支持。
 */

Demo01Arrays.java

package Day08.Demo04;

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年02月27日 12:08
 */

import java.util.Arrays;



public class Demo01Arrays {
    public static void main(String[] args) {
        int [] intArray = {10,20,30};
        //将int[]数组按照默认格式变成字符串
        String intStr = Arrays.toString(intArray);
        System.out.println(intStr);

        int [] array1 = {2,1,3,5,4,6,9,7,10};
        Arrays.sort(array1);
        System.out.println(Arrays.toString(array1));

        String [] array2 = {"bbb","aaa","ccc"};
        Arrays.sort(array2);
        System.out.println(Arrays.toString(array2));
    }
}

二.字符串倒序

题目:请使用Array相关的API,将一个随机字符串中的所有字符升序排列,并保存打印。

ArrayList.java

package Day08.Demo04;

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年02月27日 12:30
 */

import java.util.Arrays;

/**
 * 题目:请使用Array相关的API,将一个随机字符串中的所有字符升序排列,并保存打印
 */
public class ArrayList {
    public static void main(String[] args) {
        String str = "dcftvgbyhjunkmisv";
        //如何进行升序排列,sort
        //必须是一个数组,才能使用Arrays.sort方法
        //String-->数组,用toCharArray

        char[] chars = str.toCharArray();
        Arrays.sort(chars);//对字符串数组进行升序排列

        //需要倒序遍历
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]+" ");
        }
    }
}

十.数学类Math

一.常用方法

/**
 * java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作
 * 常用方法:
 * public static double abs(double num),获得绝对值。
 * public static double ceil(double num),向上取整。
 * public static double floor(double num),向下取整。
 * public static long round(double num),四舍五入
 * Math.PI,代表近似的圆周率常量package Day08.Demo04;

*/

Demo03Math.java

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年02月27日 12:38
 */



public class Demo03Math {
    public static void main(String[] args) {
        //获得绝对值
        System.out.println(Math.abs(3.14));
        System.out.println(Math.abs(0));
        System.out.println(Math.abs(78.6));
        System.out.println("=================");
        //向上取整
        System.out.println(Math.ceil(96.8));
        System.out.println(Math.ceil(3.4));
        System.out.println(Math.ceil(8.3));
        System.out.println("=================");
        //向下取整
        System.out.println(Math.floor(89.3));
        System.out.println(Math.floor(78.2));
        System.out.println(Math.floor(41.2));
        System.out.println("=================");
        //绝对值
        System.out.println(Math.round(89.5));
        System.out.println(Math.round(45.23));
        System.out.println(Math.round(12.3));
        //圆周率
        System.out.println(Math.PI);

    }
}

 */

二.练习题

题目:计算在-10.8到5.9之间、绝对值大于6或者小于2.1的整数有多少个?

Demo04MathPractise.java

package Day08.Demo04;

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年02月27日 13:02
 */

/**
 * 题目:计算在-10.8到5.9之间、绝对值大于6或者小于2.1的整数有多少个?
*/
public class Demo04MathPractise {
    //分析:
    //1.已经确定范围,使用循环
    //2.起点位置-10.8应该转化为-10,有两种办法
    // 2.1使用Math.ceil方法,向上取整(即向正方向)
    // 2.2强制类型转换,转化为int型数据
    //3. 每一个数字都是整数,所以步进表达式是num++,这样一次都进行了+1
    //4.如果拿到绝对值,可以使用Math.abs方法
    //5.一旦发现了一个数字。需要运行计数器++进行统计,

    //备注:
    //如果是使用Math.ceil方法,-10.8可以变成-10.0,注意double也是可以进行++的
    public static void main(String[] args) {
        int count = 0;//计算符合要求的数字数量

        double min = -10.8;
        double max = 5.9;
        //强制类型转变
        for (int i = (int)min; i <max ; i++) {
            int abs = Math.abs(i);//绝对值
            if ((abs>=6||abs<2.1)){
                System.out.print(i+" ");
                count++;
            }
        }
        System.out.println();
        System.out.println("共有符合要求的数字个数有"+count+"个");
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值