java面对对象chp6习题_【精选】chap6习题练习.pdf

【精选】chap6习题练习

Chp6 面向对象三大特性

Key Point

● 封装/数据隐藏

● 继承的基本语法

● 访问修饰符

● 对象创建过程

● super 关键字

● 方法覆盖

● 多态的基本语法和使用

● instanceof

● 多态用在参数和返回值上

练习

1. (继承、this 和super 关键字)有以下代码

class Super{

public Super(){

System.out.println("Super()");

}

public Super(String str) {

System.out.println("Super(String)");

}

}

class Sub extends Super{

public Sub(){

System.out.println("Sub()");

}

public Sub(int i){

this();

System.out.println("Sub(int)");

}

public Sub(String str){

super(str);

System.out.println("Sub(String)");

}

}

public class TestSuperSub{

public static void main(String args[]){

Sub s1 = new Sub();

Sub s2 = new Sub(10);

Sub s3 = new Sub("hello");

}

}

写出该程序运行的结果。

2. (super)看下面代码,写出程序运行的结果

class Super{

public void m1(){

System.out.println("m1() in Super" );

}

}

public void m2(){

System.out.println("m2() in Super" );

}

}

class Sub extends Super{

public void m1(){

System.out.println("m1() in Sub");

super.m1();

}

}

public class TestSuperSub{

public static void main(String args[]){

Sub s = new Sub();

s.m1();

s.m2();

}

}

3. (多态)有如下代码

class Super{

public void method(){

System.out.println("method() in Super");

}

public void method(int i){

System.out.println("method(int) in Super");

}

}

class Sub extends Super{

public void method(){

System.out.println("method() in Sub");

}

public void method(String str){

System.out.println("method(String) in Sub");

}

}

public class TestSuperSub{

public static void main(String args []){

Super s = new Sub();

s.method(10);

s.method();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值