java语言程序设计 李元朝 第7章 类的继承和多态机制 典型习题

例7-1 以下程序能否通过编译?如果不能,请指出产竽错误的原因,并改正之。

    class StudentsScore
     {
        private static int passPoint=350;
        private String studentId;
        private String name;
        private int score;
        public StudentsScore(String studentId,String name,int score)
            {
            this.studentId=studentId;
            this.name=name;
            this.score=score;
         }
        public int getScore()
            {
            return score;
         }
        public String getName()
             {
            return name;
         }
        }

class StudentTest
  {
     public static void main(String args[])
    {
         StudentsScore student1=new StudentsScore("20010101","Zhang",560);
         if(student1.getScore()-StudentsScore.passPoint>=0)
             System.out.println("Congratulation! "+ student1.getName()+" passes the test!");
         else
             System.out.println("Sorry ! "+student1.getName()+"does not pass the test!");
     }
}

不能
private 成员只能在声明它们的类内访问,而不能在其他们类中访问。

例7-2 设计一个学生类Student,其属性有name(姓名)、age(年龄)和degree(学位)。由Student类派生出本科生类Undergraduate和研究生Graduate,Undergraduage类

          增加属性specialty(专业),研究生类增加属性direcation(研究方向)。每个类都有show()方法,用于输出属性信息.

class Student
{
    String name;            //姓名
    int age;                //年龄
    String degree;            //学位
    Student(String name,int age,String degree)
    {
        this.name=name;
        this.age=age;
        this.degree=degree;
    }
    void show()
     {
        System.out.print("姓名:"+name);
        System.out.print(" 年龄:"+age);
        System.out.print(" 学位:"+degree);
     }

class Undergraduate extends Student
{
    String specialty;                   //专业
    Undergraduate(String name,int age,String degree,String specialty)
    {
        super(name,age,degree);            //调用父类构造方法
        this.specialty=specialty;
    }
    public void show()
      {
        super.show();                    //调用父类方法
        System.out.println(" 专业:"+specialty);
    }
}

class Graduate extends Student
  {
    String direction;            //研究方向
    Graduate(String name,int age,String degree,String direction)
    {
        super(name,age,degree);            //调用父类构造方法
        this.direction=direction;
    }
    void show()
     {
        super.show();                //调用父类方法
        System.out.println(" 研究方向:"+direction);
     }
}

public class StudentClient
{
    public static void main(String args[])
    {
        Undergraduate ungraduate=new Undergraduate("张三",23,"本科","工业自动化");
        ungraduate.show();
        Graduate graduate=new Graduate("李四",27,"硕士","网络技术");
        graduate.show();
    }
}

姓名:张三 年龄:23 学位:本科 专业:工业自动化
姓名:李四 年龄:27 学位:硕士 研究方向:网络技术

 

例7-3 编写一个java应用程序,设计一个汽车类vehicle,属性有车轮个数wheels和车得weight.卡车类truck是vehicle的子类,新加属性有载重量load.面包车类

           Minibus是truck的子类,新加属性有载客数passenger。每个类都有相关数据的输出方法。

class Vehicle
{
    int wheels;            //车轮数
    float weight;         //车重
    Vehicle(int wheels,float weight)
     {
        this.wheels=wheels;
        this.weight=weight;
     }
   
    void show()
     {
        System.out.print("车轮:"+wheels);
        System.out.print(", 车重:"+weight);
     }
}

class Truck extends Vehicle
{
    int  load;        //载重量
    Truck(int wheels,float weight,int load)
     {
        super(wheels,weight);        //调用父类构造方法
        this.load=load;
     }
    public void show1()
     {
        System.out.println("车型:卡车");
        show();                //调用父类方法
        System.out.print(", 载重量:"+load);
    }
}

子车型:面包车
车型:卡车
车轮:4, 车重:3000.0, 载重量:30, 载人:4

class Minibus extends Truck
{
    int passenger;
    Minibus(int wheels,float weight,int load,int passenger)
     {
        super(wheels,weight,load);    //调用父类构造方法
        this.passenger=passenger;
     }
    void show2()
     {
        System.out.println("\n\n子车型:面包车");
        show1();            //调用父类方法
        System.out.println(", 载人:"+passenger);
        }
}

public class VehicleClient
{
    public static void main(String args[])
    {
        Truck truck=new Truck(6,7500,80);        //创建一个Truck类对象
        truck.show1();
        Minibus minibus=new Minibus(4,3000,30,4);  //创建一个Minibus类对象
        minibus.show2();
    }
}

车型:卡车
车轮:6, 车重:7500.0, 载重量:80

子车型:面包车
车型:卡车
车轮:4, 车重:3000.0, 载重量:30, 载人:4

例7-4 编制一个程序,利用方法的多态性,判断输入数据的数据类型是整型、实型、字符型还是逻辑型。

public class DataType
{
    public static void main(String args[])
     {
        String str, str1;
        int i;
        boolean bool;
        double d;
        str=args[0];
        if((str.indexOf("'"))!=-1)    //判断输入的数据是否包含“' ”,决定是否是字符序列
         {
             prints(str);
             return;
         }
        i=str.indexOf(".");        //判断输入的数据是否包含小数点
        if(i==-1)               //不包含小数点,不可能是double型
          {
            str1=str.toUpperCase();        //转换成大写形式
            if(str1.equals("TRUE")||str1.equals("FALSE"))    //是boolean型值
             {
                bool=Boolean.parseBoolean(str);      //转换成boolean型值
                prints(bool);   
             }
            else
             {
                  i=Integer.parseInt(str);       //是int型的,转换成int型值
                  prints(i);
             }   
                   
          }
        else   //包含数点,是double型值
          {
              d=Double.parseDouble(str);
              prints(d);
          }   
      }
    static void prints(int intValue)          //int型
     {
        System.out.println(" 您输入的是整型数 "+intValue);
      }
    static void prints(double doubleValue)          //double型
     {
        System.out.println(" 您输入的是实型数 "+doubleValue);
     }
    static void prints(boolean booleanValue)    //boolean型
     {
        System.out.println(" 您输入的是布尔型量 "+booleanValue);
     }
    static void prints(String strValue)        //字符串
     {
        System.out.println(" 您输入的是字符序列 "+strValue);
     }
}

 
例7-5 查看下列程序并指出其输出结果


class A
{
   public A()
   {
        System.out.println("A is called");
     }
    public A(String x)
     {
        System.out.println("A is called and input string: "+x);
     }
}
 

class B extends A {
        public B()
         {
            super();
            System.out.println("B is called");
         }
        public B(String x)
         {
            super();
            System.out.println("B is called and input string:"+x);
         }
}

class C extends B {
        public C()
        {
            System.out.println("C is called");
         }
        public C(String x)
          {
            super(x);
            System.out.println("C is called and input string:"+x);
          }
}
 

class ExtendTest1 {
        public static void main(String args[])
        {
            new C("how are you?");
         }
}

A is called
B is called and input string:how are you?
C is called and input string:how are you?

例7-6 在下列程序中,子类B可以访问父类A中的哪些成员?为什么?

          可以访问除private以外的所有变量和方法。

例7-7 分析下列程序的功能和运行结果

class Employee {
    String name;
    Employee(String name)
     {
        this.name=name;
     }
    public void showInfo()
      {
        System.out.println("this is superClass call:"+name);
      }
    public void getInfo()
     {
        showInfo();
     }
}


class Manager {
    String name;                //注意与父类同名的成员变量
    String department;
    Manager(String n)
     {
        super(n);
     }
    public void showInfo1()
     {
        System.out.println("this is subClass call:"+name);
        System.out.println(department);
     }

class CoverMethod {
    public static void main(String args[])
     {
        Manager aa=new Manager("李四");
        aa.name="张三";
        aa.department="经理室";
        aa.getInfo();
        aa.showInfo1();
    }
}

运行结果:

this is superclass call:李四

this is subclass call:张三

posted @ 2012-12-22 16:18  elite_2012  阅读(747)  评论(0编辑  收藏  举报