javaSE_day06 学习笔记 —— 对象的造型instanceof、关键字(static,final)

javaSE_day06

1. 对象的造型

父子类之间的对象的强制转换
通过instanceof方法判断

  • 兄弟类不能强制转换
  • (当前类创建对象时)父子间可以强制转换, 编译成功但运行时异常
    eg:
    对象的造型

2. 关键字

2.1 static

修饰变量、方法、静态块
使用:类名 . static 的成员

修饰变量:

  • 成员变量
成员变量修饰范围访问初始化时间
类变量(静态变量)有static修饰对所有对象共享 ,公用和对象无关,由类名访问类加载器加载类的时候初始化
实例变量没有static修饰归对象所有,不是公用

【 static在类加载的时候申请内存,方法不调用时,方法体不执行,违背了static申请内存的时间,所以实例变量没有static修饰】

eg: 由类名调用静态变量:在这里插入图片描述
静态变量的初始化过程:
分配内存空间,设置默认值为0 –> 初始值 –> 非静态代码块中的值 –> 构造方法中的值
static变量在内存中只有一个,存放在方法区

  • 局部变量

修饰方法:

静态方法:直接使用静态成员
eg:

	int x = 1;
    static int y = 2;
    void f(){    }  
    static void f2(){    }

    void test1(){
        x = 2;  //ok
        y = 3;
        f();   //ok
        f2();
    }

    static void test2(){
        x = 2;  //error
        y = 3;
        f();    //error 静态方法不能调用非静态方法
        f2();
    }
  • 静态方法不可以被重写、可以被重载、可以被继承
  • 构造方法不可以定义成静态的
  • 静态方法不可以使用this,super

修饰静态块:

static{ }

  • 写到类体里,在类加载的时,执行一次
    先分配内存,再从上往下执行
    eg:
//分配内存,赋默认值:count1=0,count2=0 --> 执行Test() --> 输出count1=1,count2=1
public class Testprivate static Test tester = new Test();//step 1
    private static int count1;               //step 2
    private static int count2 = 2;           //step 3
    public Test(){                         
        count1++;
        count2++;
        System.out.println("" + count1 + count2); //输出 1 1  
    }
    public static Test getTester(){          //step 5
        return tester;
    }
    
    public static void main(String[] args){
       Test.getTester();
    }

静态块和构造块:
调用顺序
父类的静态块 --> 子类静态块 --> 父类的构造块 --> 父类的构造方法 --> 子类的构造块 --> 子类的构造方法
练习:

class A{
	static D d;
	static {
		System.out.println("A1"); d = new D();
	}
	{
		System.out.println("A2");
	}
	public A(){
		System.out.println("A3");
	}
}

class B extends A{
	static C c = new C();
	static {
		System.out.println(("B1"));
	}
	{
		System.out.println(("B2"));
	}
	public B(){
		System.out.println(("B3"));
	}
}

class C{
	public C(){
		System.out.println(("C"));
	}
}

class D extends C{
	public D(){
		System.out.println(("D"));
	}	
}
public class Test2 {
	public static void main(String[] args) {
		new B(); //输出A1  C  D   C   B1  A2  A3  B2  B3
	}
}

2.2 final

final可以修饰类、变量、方法

修饰变量:

常量

  • 常量名:一般名字的所有字母都大写,如果有多个单词组成,单词之间用下划线_分隔
    final int PRICE = 10;
  • 特点:定义好的值不能再修改
    final修饰的常量没有默认值,创建对象之前要有值。

修饰方法:

  • 不能被重写(保证当前结果的唯一性)
  • 可以被重载
  • 可以被继承

修饰类:

  • 不能被继承
    练习:
class Super{
    public final void m1(){
       System.out.println("m1() in Super");
    }
    public void m1(int i){
       System.out.println("m1(int) in Super");
    }
}
class Sub extends Super{
    public void m1(int i){
       System.out.println("m1(int) in Sub");
    }
    public void m1(double d){
       System.out.println("m1(double) in Sub");
    }
}
public class Test {
    public static void main(String args[]){
       Sub s = new Sub();
       s.m1();  //输出 m1() in Super
       s.m1(10);//输出 m1(int) in Sub
       s.m1(1.5);//输出 m1(double) in Sub
    }
}

final int[] NUM = {1,2,3};
num[0] = 11; //ok,常量值对于数组来说对应的是地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值