R4-1
阅读以下程序:
下面展示一些 内联代码片。
public class Test extends TT{
public static void main(String args[]){
Test t=new Test("Tom");
}
public Test(String s){
super(s);
System.out.println("How do you do?");
}
public Test(){
this("I am Tom");
}
}
class TT {
public TT(){
System.out.println("What a pleasure!");
}
public TT(String s){
this();
System.out.println("I am "+s);
}
}
程序运行结果为:
What a pleasure!
I am Tom
How do you do?
R4-2
阅读下列程序:
下面展示一些 内联代码片。
class Animal{
Animal(){ System.out.println("我是动物"); }
void shout(){ System.out.println("动物叫"); }
}
class Dog extends Animal{
void shout(){
super.shout();
System.out.println("汪汪");
}
}
class Cat extends Animal{
void shout(){ System.out.println("喵喵"; }
}
public class Test{
public static void main(String[]args){
Animal dog=newDog();
Animal cat=newCat();
dog.shout();
cat.shout();
}
}
按程序执行顺序输出结果:
(1)我是动物
(2)我是动物
(3)动物叫
(4)汪汪
(5)喵喵
R4-3
访问父类的成员可以使用super
关键词。 访问当前对象自身可以使用this
关键词。
R4-4
阅读下列程序:
下面展示一些 内联代码片。
class A{
int i=7;
public A(){
setI(20);
System.out.println("i="+i);
}
public void setI(int i){
this.i=2*i;
}
}
class B extends A{
public B(){
System.out.println("i="+i);
}
public void setI(int i){
this.i=3*i;
}
}
public class Main{
public static void main(String[] args){
new A();
new B();
}
}
程序运行后依次输出:
i=40
i=60
i=60
R4-5
阅读以下程序,写出输出结果。
下面展示一些 内联代码片。
public class AppleMobilePhone extends MobilePhone{
public static void main(String[] args){
new AppleMobilePhone();
}
public AppleMobilePhone(){
System.out.println("This is a Applemobilephone");
}
}
class MobilePhone extends Phone{
public MobilePhone(){
System.out.println("This is a mobilephone");
}
}
class Phone{
public Phone(){
System.out.println("This is a phone");
}
}
程序运行后输出: 从下列选项中选,在空格处填上相应的序号数字(1、2或3)
(1)This is a phone
(2)This is a Apple mobile phone
(3)This is a mobile phone
第一行输出:1
第二行输出:3
第三行输出:2
R4-6
说出下列A类中【代码1】~【代码3】的输出结果
下面展示一些 内联代码片。
class Fish { int weight=1; }
class Lake{
Fish fish;
void setFish(Fish s) { fish=s; }
void foodFish(int m){ fish.weight=fish.weight+m; }
}
public class Main {
public static void main(String args[]){
Fish redFish=new Fish();
System.out.println(redFish.weight);//【代码1】
Lake lake=new Lake();
lake.setFish(redFish);
lake.foodFish(120);
System.out.println(redFish.weight);//【代码2】
System.out.println(lake.fish.weight);//【代码3】
}
}
【代码1】1
【代码2】121
【代码3】121
R4-7
阅读下列程序:
下面展示一些 内联代码片。
public class Main{
public static void main(String[]args){
System.out.println("创建一个Faculty对象");
new Faculty();
}
}
class Faculty extends Employee {
public Faculty(){
System.out.println("完成Faculty的任务");
}
}
class Employee extends Person{
public Employee(){
this("调用Employee的重载构造方法");
System.out.println("完成Employee的任务");
}
public Employee(String s){
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("完成Person的任务");
}
}
从下面选项中进行选择,将相应序号(1、2、3、4、5中选其一)填入空格中:
(1)完成Person的任务
(2)完成Employee的任务
(3)完成Faculty的任务
(4)调用Employee的重载构造方法
(5)创建一个Faculty对象
按程序执行顺序输出结果:
5
1
4
2
3
R4-8
已知代码:
下面展示一些 内联代码片。
class A{
String name="A";
String getName(){
return name;
}
String greeting(){
return "class A";
}
}
class B extends A{
String name="B";
String greeting(){
return "class B";
}
}
public class Main{
public static void main(String args[]){
A a=new A();
A b=new B();
System.out.println(a.greeting()+" has name "+a.getName());
System.out.println(b.greeting()+" has name "+b.getName());
}
}
在下列运行结果的空格中选择填写A或B:
class A has name A
class B has name A



