返回列表 回复 发帖

构造函数不能输出吗

import java.io.*;
class A{
public A()
{
   System.out.print("good");
}

public void printout()
{
  System.out.println("hello,world");
}
}

public class main
{
public static void main(String args[])
{
  keyBoardInput f = new keyBoardInput();
  f.printout();
}
}
为什么执行后生成对象f的时候没有输出good?
收藏本文至
看不懂哈,你有没有构造一个A类的对象,还有KeyBoardInput类的定义在哪里?java.io里面没有这个类吧。
 
import java.io.*;
class A{
public A()
{
   System.out.print("good");
}

public void printout()
{
  System.out.println("hello,world");
}
}

public class main
{
public static void main(String args[])
{
  A f = new A();
  f.printout();
}
}
如果是这样呢?
 
构造方法是可以输入的
如你写了
class A {
public A (){
System..out.println("hello world ");
}
}

那么当你在用 class A new 对象 时   A f = new A();
先会去执行 A (){....} 里的代码 ,然后构造一个A的对象
500年前,我和吾空一行人徙步去西天探险。。。
 
keyBoardInput没有出现啊,要出good是要将A实例化的
 
返回列表