您的位置:首页 > 编程语言 > Java开发

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

2013-04-11 14:00 495 查看
1.子类能够继承父灰的哪些成员变量和方法?

子类只能继承父类非私有的成员变量和方法。

2.重载与覆盖有什么不同?

方法重载指在一个类中创建了多个方法。它们具有相同的方法名,但参数的个数不同、或参数的数据类型不同,或两者都不同

覆盖是指子类中定义的方法与父类方法名相同,子类在调用这个同名方法时,默认是调用它自已定义的方法,而将从父类那里继承来的方法进行覆盖,如

果一定要调用父类的同名方法,可以使用super关键字来指定。

3.编写一个程序实现方法的重载。


class Tiff {
public Tiff(){ };
double calculate(float r)
{
return Math.PI*r*r;
}
double calculate(float r,float h)
{
return Math.PI*r*r*h;
}
public static void main(String args[])
{
Tiff tiff=new Tiff();
System.out.println("Area="+tiff.calculate(12.6f));
System.out.println("Volume="+tiff.calculate(12.6f,37.8f));
}
}


4.编写一个程序实现方法的覆盖



class CCircle {
protected double radius;
public CCircle(double r)
{
radius=r;
}
public void show()
{
System.out.println("Radius="+radius);
}
}

class CCoin {
private int value;
public CCoin(double r, int v)
{
super(r);
value=v;
}
public void show()
{
System.out.println("Radius="+radius+" Value="+value);
}
public static void main(String[] args)
{
CCircle circle=new CCircle(2.0);
CCoin coin=new CCoin(3.0,5);
circle.show();
coin.show();
}
}


5.编写一个使用this和super的程序


class Parent {
protected int x;
public Parent(int i)
{
x=i;
}
int getX() { return x; }
void show()
{
System.out.println("x="+x);
}
}

class Son {
protected int x;
public Son(int j)
{
super(3); //调用父类的构造方法
this.x=j; //引用子类中的成员变量x
}
void show()
{
System.out.println("super.x="+super.x+" "+"this.x="+x);
//分别输出父类和子类的成员变量x
}

public static void main(String args[])
{
Son son=new Son(5);
son.show();
}
}


6.final成员变量和方法有什么特点?

用它可以修饰类及类中的成员变量和成员方法。用final修饰的类不能被继承,用final修饰的成员方法不能被覆盖,用final修饰的成员变理不能被修改

7.已有一个交通工具类vehicle,其中的属性包括:速度speed、类别kind、颜色color;方法包括设置速度、设置颜色、取得类别、取得颜色。设计一个小车类


car,继承自vehicle。car中增加了属性:座位数passenger,增加了设置和获取座位数的方法,创建car的对象,为其设置新速度和颜色,并显示其状态(所有属性)
class Vehicle {
String color;
String kind;
int speed;
Vehicle() //默认构造方法
{
color="";
kind="";
speed=0;
}
public void setColor(String color1)
{
color=color1;
}
public void setSpeed(int speed1)
{
speed=speed1;
}
public void setKind(String kind1)
{
kind=kind1;
}
public String getColor()
{
return color;
}
public String getKind()
{
return kind;
}
public int getSpeed()
{
return speed;
}
}

class Car extends Vehicle {
int passenger;
public Car() //默认构造函数
{
super();
passenger=0;
}
public void setPassenger(int passenger)
{
this.passenger=passenger;
}
public int getPassenger()
{
return passenger;
}
public static void main(String[] args)
{
Car Benz=new Car();
Benz.setColor("Yellow");
Benz.setKind("Roadster");
Benz.setSpeed(120);
Benz.setPassenger(4);
System.out.println("Benz: ");
System.out.println("Color "+Benz.getColor());
System.out.print("Speed (km/h)");
System.out.println(Benz.getSpeed());
System.out.println("Kind "+Benz.getKind());
System.out.print("Passenger ");
System.out.println(Benz.getPassenger());
}
}


8.设计一个圆类circle,具有属性:圆心坐标x和y及圆半径r,除具有设置及获取属性的方法外,还具有计算周长的方法permeter()和计算面积的方法area().

再设计一个圆柱体类cylinder,cylinder继承自circle,增加了属性:高度h,增加了设置和获取h的方法、计算表面积的方法area()和计算体积的方法volume

().创建car的类对角,显示其所有属性,计算并显示其面积和体积。



class Circle {
double x;
double y;
double r;
Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public void setX(double x)
{
this.x=x;
}
public void setY(double y)
{
this.y=y;
}
public void setR (double r)
{
this.r=r;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public double getR ()
{
return r;
}
public double area()
{
return r*r*3.1416;
}
public double perimeter()
{
return 2*r*3.1416;
}
public void show()
{
System.out.print("x="+x+", y="+y+", Radius="+r);
}
}
class Cylinder extends Circle {
double h;
Cylinder(double x,double y,double r,double h)
{
super(x,y,r);
this.h=h;
}
public void setH (double h)
{
this.h=h;
}
public double getH ()
{
return h;
}
public double area() //计算圆柱体的表面积,覆盖了父类的area()函数
{
return perimeter()*h+super.area()*2;
}
public double volume() //计算圆柱体的体积
{
return super.area()*h;
}

public static void main(String[] args)
{
Cylinder cylinder=new Cylinder(1,1,4,8);
cylinder.show();
System.out.println(", Height="+cylinder.getH ());
System.out.println("Area="+cylinder.area());
System.out.println("Volume="+cylinder.volume());
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐