2、class Test{ Test(){} } 下列构造方法中,哪个构造方法可以存在上述Test类中?( )
A: public Test(){};
B: private Test(){};
C: Test(int age){};
D: public Test(){};
A: public Test(){};
B: private Test(){};
C: Test(int age){};
D: public Test(){};
举一反三
- 中国大学MOOC: 如下所示的Test类的Java程序中,共有几个构造方法()。 public class Test{ private int x; public Test(){} public void Test(int i){ this.x=i; } public Test(String str){} }
- 已有如下程序: public class Test{ public Test(int x,int y,int z){.....} } 下面哪些方法是合法的构造方法重载:( ) A: Test(){} B: int Test(){} C: Test(int x,int y,byte z){} D: void Test(int x,int y,int z){}
- (6-6)请阅读程序,写出程序运行结果。 class Test{ static int x=10; int y=99; { y=y+10; } static { x=x+5; } { y=y+10; } static { x=x+5; } public Test() {//构造方法 x=x+5; } { System.out.println(x*y); } } public class Demo11 { public static void main(String[] args) { Test t1=new Test(); Test t2=new Test(); } }
- 阅读以下程序,写出运行结果。 #include using namespace std; class Test { private: int num; public: Test(); Test(int n); }; Test::Test() { cout << "Init defa" << endl; num = 0; } Test::Test(int n) { cout << "Init" << " " << n << endl; num = n; } int main() { Test x[2]; Test y(15); return 0; }
- 下列程序的输出结果是 _____ 。 #include using namespace std; class Test { public: Test() { cnt++; } ~Test() {cnt--; } static int Count() { returncnt;} private: static int cnt; }; int Test::cnt = 0; int main() { cout << Test::Count()<<' '; Test tl, t2; Test* pT3 = new Test; Test* pT4 = new Test; cout << Test::Count()<<' '; delete pT4; delete pT3; cout << Test::Count()<< endl; return 0; }