publicclassPet{privateStringname;publicPet(){System.out.print(1);}publicPet(Stringname){System.out.print(2);}}publicclassDogextendsPet{publicDog(){System.out.print(4);}publicDog(Stringname){super(name);System.out.print(3);}}执行newDog(棕熊”);后程序输出是哪项?()A.33B.

题目

publicclassPet{privateStringname;publicPet(){System.out.print(1);}publicPet(Stringname){System.out.print(2);}}publicclassDogextendsPet{publicDog(){System.out.print(4);}publicDog(Stringname){super(name);System.out.print(3);}}执行newDog(棕熊”);后程序输出是哪项?()

A.33

B.13

C.23

D.123


相似考题
更多“publicclassPet{privateStringname;publicPet(){System.out.print(1);}publicPet(Stringname){Sy ”相关问题
  • 第1题:

    publicclassPet{privateStringname;publicPet(Stringname){this.name=name;}publicvoidspeak(){System.out.print(name);}}publicclassDogextendsPet{publicDog(Stringname){super(name);}publicvoidspeak(){super.speak();System.out.print(Dog”);}}执行代码Petpet=newDog(京巴”);pet.speak();后输出的内容是哪项?()

    A.京巴

    B.京巴Dog

    C.null

    D.Dog京巴


    参考答案:B

  • 第2题:

    有如下程序: include using namespace std; class Pet{ char name[10]; public: Pet(c

    有如下程序:

    include<iostream>

    using namespace std;

    class Pet{

    char name[10];

    public:

    Pet(char*name){strcpy(this->name,name);}

    const char*getName()const {return name;}

    virtual void call()const=0;

    };

    class Dog:public Pet{

    public:

    Dog(char*name):Pet(name){}

    void call()const{cout<<"汪汪叫":}

    };

    class Cat:public Pet{

    public:

    Cat(char*name):Pet(name){}

    void call()const{cout<<"喵喵叫";}

    };

    int main(){

    Pet*pet1=new Dog("哈克"),*pet2=new Cat("吉米");

    cout<<pet1->getName();pet1->call();cout<<end1;

    cout<<pet2->getName();pet2->call();cout<<end1;

    return 0;

    }

    程序的输出结果是______。


    正确答案:哈克汪汪叫 吉米喵喵叫
    哈克汪汪叫 吉米喵喵叫 解析:此题考查的是虚函数与多态性。在成员函数的声明前面加上virual关键字,即可把函数声明为虚函数;在C++中,一个基类指针可以用于指向它的派生类对象,而且通过这样的指针调用虚函数时,被调用的是该指针实际所指向的对象类的那个重定义版本。即若基类和派生类中存在一模一样的成员函数,通过该基类指针调用这样的成员函数时,若这个成员函数被定义成虚函数,那么就调用派生类中的;否则就调用基类中的。本题中,在f()函数中,此题中,void call()在基类中被声明为虚函数,在主函数中,语句Pet*pet1=new Dog("哈克"),*pet2=new Cat("吉米");定义了基类的指针per1和pet2,并让它们分别指向派生类对象Dog和Cat。所以通过该指针调用call()时运行的是派生类的版本,分别输出哈克和吉米;而通过该指针调用 getName()运行的是基类的版本,分别输出汪汪叫和喵喵叫。

  • 第3题:

    interface Playable {

    void play();

    }

    interface Bounceable {

    void play();

    }

    interface Rollable extends Playable, Bounceable {

    Ball ball = new Ball("PingPang");

    }

    class Ball implements Rollable {

    private String name;

    public String getName() {

    return name;

    }

    public Ball(String name) {

    this.name = name;

    }

    public void play() {

    ball = new Ball("Football");

    System.out.println(ball.getName());

    }

    }

    这个错误不容易发现。


    正确答案:

     

    错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个

    interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。

    任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static

    final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new

    Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的

    reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final

    的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"

    这里显示有错。

  • 第4题:

    publicclassPlant{privateStringname;publicPlant(Stringname){this.name=name;}publicStringgetName(){returnname;}}publicclassTreeextendsPlant{publicvoidgrowFruit(){}publicvoiddropLeaves(){}}Whichistrue?()

    A.Thecodewillcompilewithoutchanges.

    B.ThecodewillcompileifpublicTree(){Plant();}isaddedtotheTreeclass.

    C.ThecodewillcompileifpublicPlant(){Tree();}isaddedtothePlantclass.

    D.ThecodewillcompileifpublicPlant(){this(”fern”);}isaddedtothePlantclass.

    E.ThecodewillcompileifpublicPlant(){Plant(”fern”);}isaddedtothePlantclass.


    参考答案:D

  • 第5题:

    Java代码查错

    1.

    abstract class Name {

    private String name;

    public abstract boolean isStupidName(String name) {}

    }

    大侠们,这有何错误?


    正确答案:

     

    错。abstract method 必须以分号结尾,且不带花括号。