当前位置:编程学习 > JAVA >>

java结果输出,知其所以然

[java] 
代码输出结果是什么?为什么会出现这种结果? 
代码一 
public class ParentTest { 
    public ParentTest(){ 
        System.out.println("Parent Constructor invoked"); 
    } 
    static{ 
        System.out.println("parent static invoked"); 
    } 
     
    { 
        System.out.println("parent invoked"); 
    } 

public class ChildrenTest extends ParentTest{ 
    public ChildrenTest(){ 
        System.out.println("Children Constructor invoked"); 
    } 
    static{ 
        System.out.println("Children static invoked"); 
    } 
     
    { 
        System.out.println("Children invoked"); 
    } 
    public static void main(String[] args) { 
        new ChildrenTest(); 
    } 

代码二:true or false 
public class MyTest1 { 
    private String str; 
    public MyTest1(String str){ 
        this.str=str; 
    } 
    public static void main(String[] args) { 
        MyTest1 m1=new MyTest1("hello"); 
        MyTest1 m2=new MyTest1("hello"); 
        System.out.println(m1.equals(m2)); 
    } 

代码三:true or false 
public class MyTest1 { 
    public static void main(String[] args) { 
    System.out.println(Integer.valueOf(323)==Integer.valueOf(323)); 
    System.out.println(Integer.valueOf(23)==Integer.valueOf(23)); 
    } 

代码四: 
public class MyTest1 { 
    private static int a; 
    public MyTest1(){ 
        System.out.println("a="+(++a)); 
    } 
    public void test(){ 
        int a=5; 
        System.out.println("a="+a); 
    } 
    public void test2(){ 
        a=5; 
        System.out.println("a="+a); 
    } 
    public static void main(String[] args) { 
        new MyTest1(); 
        new MyTest1(); 
        new MyTest1().test(); 
        System.out.println(a); 
        new MyTest1().test2(); 
        System.out.println(a); 
    } 

代码五: 
public class Parent { 
    public Parent(String name){ 
        System.out.println(name); 
    } 

public class Children extends Parent{ 
    public Children(String name){ 
        System.out.println(name); 
    } 
     
    public static void main(String[] args) { 
        new Children("hello"); 
    } 

作者:HarderXin
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,