zoukankan      html  css  js  c++  java
  • 课后作业3(动手动脑)

    1

    程序代码:
    public class InitializeBlockDemo {

    /**
    * @param args
    */
    public static void main(String[] args) {

    InitializeBlockClass obj=new InitializeBlockClass();
    System.out.println(obj.field);

    obj=new InitializeBlockClass(300);
    System.out.println(obj.field);
    }

    }

    class InitializeBlockClass{
    //下面这句在初始化块之前与之后,会影响到field字段的初始值
    //public int field=100;

    {
    field=200;
    }
    public int field=100;
    public InitializeBlockClass(int value){
    this.field=value;
    }
    public InitializeBlockClass(){

    }
    }

    运行截图:

    当把public的初始化放到上面时候则显示的200.

    规律:

    根据前后顺序,按照顺序执行,有构造函数执行构造函数。

    2

    猜想与方法:

    在静态函数中创建一个这个类的对象,然后引用非静态成员。

    程序代码:


    public class text2 {
    public static void main(String arg[]){
    Employee text=new Employee();
    text.clear();
    }

    }
    class Employee
    {
    String name;
    long salary;
    short employee_id;
    static int total_employees;
    static void clear(){
    total_employees=0;System.out.println(total_employees);
    new Employee().salary=0;System.out.println(new Employee().salary);
    }
    }

    运行结果截图:

    https://necydcy.me/
  • 相关阅读:
    poj 2488 A Knight's Journey( dfs )
    poj 2676 Sudoku ( dfs )
    poj 3087 Shuffle'm Up ( map 模拟 )
    poj 1426 Find The Multiple( bfs )
    poj 3126 Prime Path( bfs + 素数)
    Atcoder ARC-063
    Atcoder ARC-062
    Atcoder ARC-061
    Atcoder ARC-060
    Atcoder ARC-058
  • 原文地址:https://www.cnblogs.com/miria-486/p/7687687.html
Copyright © 2011-2022 走看看