Java笔记(9)——static、静态初始化、单子模式、final关键字和抽象类

本文详细介绍了Java中的关键字static,包括其作用和使用场景。接着讲解了静态初始化的概念,说明了静态代码块在类加载时的执行情况。然后讨论了单子模式,强调了其防止外部直接创建实例的特点。此外,文章还深入探讨了final关键字,阐述了其在变量、方法和类上的应用,以及如何提高代码的安全性和可读性。最后,解释了抽象类和抽象方法的定义及使用限制,指出抽象类不能被实例化,其子类必须重写抽象方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. static关键字

  1. static:静态的,意为全局的,由该关键字标记的变量或者方法由整个类所共享。
  2. 没有对象的实例,可以使用类名.方法名()的形式访问由static修饰的类方法。
  3. static方法内部,只能访问类的static属性,不能访问非static属性。
  4. 因为不需要实例就可以访问static方法,因此,static方法内部也不能有thissuper
/*
* 练习1:编写一个类,实现银行账户的概念,
* 包含的属性有“帐号”、“密码”、“存款余额”、“利率”、“最小余额”,定义封装这些
  属性的方法。账号要自动生成。
  编写主类,使用银行账户类,输入、输出3个储户的上述信息。
  考虑:哪些属性可以设计成static属性。
* Bank.java
* */

public class Account {
    private final int id;
    private final int balance;
    private final String password;

    private static double interestRate;
    private static int minBalance;

    private static int initId = 1000;

    public Account(int balance, String password) {
        id = initId++;
        this.balance = balance;
        this.password = password;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", balance=" + balance +
                ", password='" + password + '\'' +
                '}';
    }
}


public class TestBalance {

    public static void main(String[] args) {
        Account account = new Account(1000, "12345");

        System.out.println(account);

        Account account1 = new Account(2000, "fewrd");

        System.out.println(account1);
    }

}

/*
Account{id=1000, balance=1000, password='12345'}
Account{id=1001, balance=2000, password='fewrd'}
*/

2. 静态初始化

一个类中可以使用不包含在任何方法体中的静态代码块。当类被载入时,静态代码块被执行,且只被执行一次,静态代码块经常用来进行类属性的初始化

public class TestStatic {
    static String name;
    int age;


    // 静态代码块 当类被加载时执行, 只被执行一次
    static {
        System.out.println("静态代码块。。。。。");
    }

    // 类似于不带参数的构造器
    {
        age = 10;
        System.out.println("非静态代码块。。。");
    }

    public TestStatic(){
        System.out.println("构造器。。。。。。");
    }

    static void test(){
        System.out.println(name + "在测试中。。");
    }

    void method(){
        System.out.println(name + "在跑步,年龄是:" + this.age);
    }

    public static void main(String[] args) {
        TestStatic.name  ="Tom";
        TestStatic.test();

        TestStatic testStatic = new TestStatic();
        name = "Jess";
        testStatic.age = 12;
        testStatic.method();

        System.out.println(name);

        TestStatic testStatic1 = new TestStatic();

        TestStatic.name = "Lucy";
        testStatic1.method();

        System.out.println(TestStatic.name);
    }
}

/*
静态代码块。。。。。
Tom在测试中。。
非静态代码块。。。
构造器。。。。。。
Jess在跑步,年龄是:12
Jess
非静态代码块。。。
构造器。。。。。。
Lucy在跑步,年龄是:10
Lucy
*/

3. 单子模式

在类的外部不能通过new构造器的方法创建实例。

public class SingleTon {
    private static SingleTon onlyone = new SingleTon();
    private String name;
    public static SingleTon getString(){
        return onlyone;
    }
    private SingleTon(){ // private 构造器 不能在类的外部创建该类的对象
        
    }
}

public class TestSingle{
    public static void main(String[] args) {
        SingleTon singleTon1 = SingleTon.getString();
        SingleTon singleTon2 = SingleTon.getString();

        if(singleTon1 == singleTon2)
            System.out.println("s1 == s2");
    }
}

/*
s1 == s2
*/

4. final关键字

  1. 使用final标记的变量即成为了常量,只能赋值一次。

  2. final标记的类不能被继承,提高安全性,提高可读性。

  3. final标记的方法不能被子类所重写,增加安全性。

  4. final标记的成员变量,必须在声明的同时,或在每个构造器中显示赋值,然后才能使用。

  • final关键字 表示最终的 可以修饰类 属性 和 方法 表示最终的。
  • final修饰属性 是最终的属性 属性值不能被修改 是最终的属性。
  • final修饰的方法 是最终的方法 该方法不能被 修改 该方法不能被重写。
  • final修饰的类 是最终的类 该类不能被扩展 该类不能被继承。
public class Test {
    final int value;

    public Test(int value) {
        this.value = value;
    }


    public static void main(String[] args) {
        Test test = new Test(2);

        //test.value = 9; final  一旦声明 则无法更改
        
        Test test1 = new Test(3);
    }
}
public class Animal {
    public final void run(){ // final修饰的方法在子类中不可以被重写但是可以继承
        System.out.println("动物会走。。。");
    }
}

5. abstract关键字

abstract来修饰一个类时,这个类是抽象类;用abstract来修饰一个方法时,这个方法叫抽象方法。

抽象方法:只有方法的声明,没有方法的实现。

含有抽象方法的类必声明为抽象类,抽象类不能被实例化,抽象类是用来继承的,抽象类的子类必须重写该抽象类,并提供方法体。

不能用abstract方法来修饰私有方法,构造方法,静态方法。

一个类不能同时被finalabstract修饰。

public abstract class SuperClass{
    abstract void m(); //抽象方法
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值