课堂动手动脑

一、动手动脑

  

错误原因:因为编写者在写这个 类是已经自己写了 自己的无参构造函数,所以在主函数里边中引用的的是有参的构造函数。当没有构造函数时编译器会给一个默认的构造函数但是如果当编写者给了就会覆盖这个运用编写者的构造函数。

二、动手动脑2

package jxlPacakge;

class Root
{
    static{
        System.out.println("Root的静态初始化块");
    }
    {
        System.out.println("Root的普通初始化块");
    }
    public Root()
    {
        System.out.println("Root的无参数的构造器");
    }
}
class Mid extends Root
{
    static{
        System.out.println("Mid的静态初始化块");
    }
    {
        System.out.println("Mid的普通初始化块");
    }
    public Mid()
    {
        System.out.println("Mid的无参数的构造器");
    }
    public Mid(String msg)
    {
        //通过this调用同一类中重载的构造器
        this();
        System.out.println("Mid的带参数构造器,其参数值:" + msg);
    }
}
class Leaf extends Mid
{
    static{
        System.out.println("Leaf的静态初始化块");
    }
    {
        System.out.println("Leaf的普通初始化块");
    }    
    public Leaf()
    {
        //通过super调用父类中有一个字符串参数的构造器
        super("Java初始化顺序演示");
        System.out.println("执行Leaf的构造器");
    }

}

public class TestStaticInitializeBlock
{
    public static void main(String[] args) 
    {
        new Leaf();
        

    }
}

总结:1、静态初始化块是由static修饰的初始化块,只在类加载时执行一次,而且当一个静态初始化块没有执行完成时不会执行下一个静态初始化块。

2、初始化块在每次初始化实例对象的时候都执行一次,可以给任意变量赋值

3、构造函数每次执行都调用。

4、调用子类时会先调用其父类。

三、课后作业

使用类的静态字段和构造函数,可以跟踪某个类所创建对象的个数。请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?

package jxlPacakge;

import java.util.Scanner;

public class test1016 {
    static int sum=0;
    public test1016()
{        sum++;
    }
    public static void main(String[] args) {
        Scanner sr=new Scanner(System.in);
        test1016 a[]=new test1016[100];
        int i=sr.nextInt();
        for(int j=0;j<i;j++)
        {
            a[i]=new test1016();
        }
        System.out.println(sum);
    }
}

实例:

使用上页幻灯片中定义的类,以下代码输出结果是什么?

  输出结果:

 总结:执行类成员定义时指定的默认值或类的初始化块哪一个在后就是哪一个。

2.当有构造函数来时就按照构造函数里的值来赋值。

五验证

public class test1 {
int x=4;
static int y=3;
public static void fangfa()
{System.out.println("x的值为:"+new test1().x);
System.out.println("y的值为:"+y);
    }
public static void main(String[] args) {
    test1.fangfa();
    System.out.println("x的值"+new test1().x);
}
}

结果:

package jxlPacakge;
public class StrangeIntegerBehavior 
{

    
    public static void main(String[] args)
    {

        
        Integer i1=100;
       
        Integer j1=100;
        
        System.out.println(i1==j1);

        
        Integer i2=129;
        
        Integer j2=129;
        
        System.out.println(i2==j2);
    
    }


}

 在看jdk文件

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

 

原文地址:https://www.cnblogs.com/guziteng1/p/11684213.html