Day3

多态:同一事物在不同的条件之下,展现的意识形态不一样

程序中的多态

1)  具有继承关系

2)  子类必须重写父类的方法

3)  子类对象赋给父类、父类的引用指向子类、(里氏替换原则)

4)  父类调用方法—透明的调用了子类的方法

本章作业

一、选择题

1.编译运行如下Java代码,输出结果是( B )

class Base{

public void method(){

      System.out.print (“Base method”);

}

}

class Child extends Base{

public void method(){

      System.out.print (“Child method”);

}

}

class sample{

public static void main(String[] args) {

       Base base=newchild();

       base.method()

}

}

A. Base method

B. Child method

C. Base method Child method

D. 编译错误

 

2.编译运行如下Java代码,输出结果是( A )

class Base{

public void method(){

        System.out.print(“Base method”);

}

}

class Child extends Base{

public void methodB(){

    System.out.print (“ChildmethodB”);

}

}

class sample{

public static void main(String[] args) {

       Base base=newChild();

       base.method();

}

}

A. Base method

B. Child methodB

C. Base method Child MethodB

D. 编译错误

 

3.编译运行如下Java代码,输出结果是( D )

class Base{

public void method(){

        System.out.print(“Base method”);

}

}

class Child extends Base{

public void methodB(){

        System.out.print (“ChildmethodB”);

}

}

class sample{

public static void main(String[] args) {

        Base base=newChild();

        base.methodB()

}

}

A.Base method

B.Child methodB

C.Base method Child MethodB

D.编译错误

 

4.编译运行如下Java代码,输出结果是( A )

class Person{

String name=“Person”;

public void shout(){

System.out.print (name)

}

}

class Student extends Person{

String name=“student”;

String school=“school”;

}

class Test{

public static void main(String[] args) {

         Person p=newStudent();

         p.shout();

}

}

A.person

B.student

C.person student

D.编译错误

 

5.下列Java代码中Test类中的四个输出语句的输出结果依次是(C )

class Person{

String name=“Person”;

public void shout(){

System.out.println (name)

}

}

class Student extends Person{

String name=“student”;

String school=“school”;

}

class Test{

public static void main(String[] args) {

        Person p=newStudent();

       System.out.print(p instanceof student)

       System.out.print(p instanceof Person)

       System.out.print(p instanceof Object)

       System.out.print(p instanceof System)

}

}

A.true、false、true、false

B.false、true、false、false

C.true、true、true、编译错误

D.true、true、false、编译错误

 

二、简答题

1.简述多态的概念,子类和父类之间转换是遵循的规则。

多态:是具有表现多种形态的能力的特征。专业说法:同一个实现接口,使用不同的实例而执行不同的操作。
子类转换成父类(向上转换)时的规则:
Ø将一个父类的引用指向一个子类对象,称为向上转型,自动进行类型转换。
Ø此时通过父类引用变量调用的方法是子类覆盖或继承父类的方法,不是父类的方法。
Ø此时通过父类引用变量无法调用子类特有的方法。
父类到子类的转换(向下转型)
将一个指向子类对象的父类引用赋给一个子类的引用,称为向下转型,此时必须进行强制类型转换。

2.请指出如下Java代码中存在的错误,并改正。

class LeTeacher{

   public void giveLesson(){

       System.out.println(“知识点讲解”);

       System.out.println(“总结提问”);

}

}

class LeDBTeacher extends LeTeacher {

   public void giveLesson(){

       System.out.println(“启动Sqlserver”);

       Super.giveLesson();

}

public void sayHi(){

System.out.println(“Hi”);

}

}

class Test{

public static void main(String[] args) {

LeTeacher t = new LeDBTeacher ();

t.sayHi();//

t.giveLesson();

}

}

在LeTeacher类里添加sayHi方法;

 

3.请指出如下Java代码中存在的错误,并解释原因。注释错误语句后的输出结果是什么,并解释原因。

class Person{

String name;

int age;

public void eat(){

System.out.println (“person eating with mouth”);

}

public void sleep(){

System.out.println (“sleeping in night”);

}

}

class Chinese extends Person{

public void eat (){

System.out.println (“Chinese eating rice with mouth bychopsticks”);

}

public void shadowBoxing(){

System.out.println (“practice shadowBoxing every morning”);

}

}

class Test {

   public static voidmain(String[] args) {

       Chinese ch=newChinese();

       ch.eat();

       ch.sleep();

      ch.shandowBoxing();

       Person p=nexChinese();

       p.eat();

       p.sleep();

       p.shandowBoxing();错误

}

}

调用父类的方法..

4.请指出如下Java代码中存在错误,并解释原因,注释错误语句后的输出结果是什么,并解释原因。

abstract class Shape { // 几何图形

   public abstract doublegetArea();

}

Class Square extends Shape{

private double height = 0;//正方形的边长

public Square (double height){

      this.height=height;

}

public double getArea(){

      return (this.height* this.height);

}

}

Class Circle extends Shape{

private double r =0;//圆的半径

private final static double PI=3.14;//圆周率

public Circle (double r){

      this.r=r;

}

public double getArea(){

      return (PI * r *r);

}

}

Class TestShape{

public static void main (String[] args) {

      Shape square=newSquare(3);

      Shape circle=newCircle(2);

     System.out.println(square.getArea());

     System.out.println(circle.getArea());

      Square  sq =(Square) circle();错误

     System.out.println(sq.getArea());

}

}

5.编码创建一个打印机类Printer,定义抽象方法print();创建两个子类——针式打印机类DotMatrixPrinter和喷墨打印机类InkpetPrinter,并在各自类中重写方法print(),编写测试类实现两种打印机打印。再添加一个激光打印机类LaserPrinter,重写方法print()。修改测试类实现该打印机打印。

——提示———————————————————————————————————

利用向上转型,将子类对象赋给父类Printer的引用变量。

———————————————————————————————————————

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值