给出下面的代码段 public class Base{ int x, y; static int z; public Base(int a,int b)
{ x=a; y=b; } } 以下代码错误的是?().
A: Base b=new Base();
b.z=10;
B: Base b=new Base(1,2);
b.z=10;
C: Base.z=10;
D: Base b=new Base(1,2);
b.x=2;
{ x=a; y=b; } } 以下代码错误的是?().
A: Base b=new Base();
b.z=10;
B: Base b=new Base(1,2);
b.z=10;
C: Base.z=10;
D: Base b=new Base(1,2);
b.x=2;
举一反三
- (6-2)定义如下Base类,能在(1)处正确调用Base的构造方法。 class Base{ int x,y; Base(int x){} Base(int x,int y){ //(1)调用Base的构造方法 } }
- 给出以下代码,请问插入以下哪些语句可以使程序编译通过?( )class Base{public Base(int i){}}public class Example extend Base{public static void main (String arg[]){Example e = new Example(10);}Example(int i){Super(i);}Exanple(String s, int i){this(i);//插入代码处}}请选择一个正确答案: A: Example d = new Example(); B: Super(); C: This(“Hello”,10); D: Base b = ne Base(10);
- 以下代码调试结果 class Base {} class Sub extends Base {} public class CEx{ public static void main(String argv[]){ Base b = new Base(); Sub s = (Sub) b; } }
- 有以下程序: #include <iostream> using namespace std; class Base{ public: Base(int x=0) {cout<<x;} }; class Derived : public Base{ public: Derived(int x=0) {cout<<x;} private: Base val; }; int main(){ Derived d(1); return 0; } 程序的输出结果是
- 如何能使程序调用Base类的构造方法输出"base constructor"; class Base{ Base(int i){ System.out.println("base constructor"); } Base(){ } } public class Sup extends Base{ public static void main(String argv[]){ Sup s= new Sup(); //One } Sup() { //Two } public void derived() { //Three } }