}
}
7.定义一个类Rectangle代表矩形,其中包括计算面积的方法。再定义一个它的子类Square代表正方形],其中也包含计算面积的方法。编写一程序,测试新建子类对象的属性和方法? class Rectangle{ float length; float width; Rectangle(float len,float wh){ length=len; width=wh; } float getArea(){ return length*width; } }
class Square extends Rectangle{ float length; Square(float len){ super(len,len); length=len; } float getArea(){ return super.getArea(); } }
public class TestRectangle{ public static void main(String[] args){ Square sq=new Square(5.2f); System.out.println(\ } }
8.定义一个Document类,包含成员属性name。从Document派生出Book子类,增加PageCount变量,编写一应用程序,测试定义的类? class Document{ String name; Document(String str){ name=str; } void show(){ System.out.println(name); } }
class Book extends Document{ int PageCount;
16
Book(String name,int n){ super(name); PageCount=n; } void show(){ System.out.print(\ super.show(); System.out.println(\ } }
public class TestDoc{ public static void main(String[] args){ Book book1=new Book(\ book1.show(); } }
9.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。 class Vehicle { int wheels; float weight;
Vehicle(int wh,float we) { wheels=wh; weight=we;
}
int getWheels(){return wheels;} float getWeight(){return weight;} void show() {
System.out.println(“车轮:”+wheels); System.out.println(“车重:”+weight); } }
class Car extends Vehicle {
int loader;
Car(int wheels,float weight,int loader) {super(wheels,weight); this.loader=loader;} }
public void show() {
System.out.println(“车型:小车”); super.show();
System.out.println(“载人:”+loader); }
17
class Truck extends Car {
float payload;
Truck(int wheels,float weight,int loader,float payload) {super(wheels,weight,loader); this.payload=payload;} }
public void show() {
System.out.println(“车型:卡车”); super.show();
System.out.println(“载重量:”+payload); }
public class VehicleClient {
public static void main(String[] args) {
Car car=new Car(4,1500,4); car.show();
Truck truck=new Truck(8,7000,3,25000); truck.show(); } }
10.一个关于等边三角形的类Trival如下,其中的属性包括三角形的bian,方法包括:默认构造方法、为bian指定初值的构造方法、获取三角形面积findArea()。试利用方法覆盖的知识,设计三棱柱体类TriCylinder。(其中findArea()为计算三棱柱体的表面积) class Trival { double bian; Trival() { bian=1.0; } Trival(double b) { bian=b; } double findArea() {
return (3/4)*bian*bian;
} }
答案:
public class TestOverrideMethods
18
{
public static void main(String[] args) {
Cylinder myTriCylinder = new TriCylinder(5.0, 2.0);
System.out.println(\ System.out.println(\ System.out.println(\ myTriCylinder.findArea()); } }
class TriCylinder extends Trival {
private double length;
// Default constructor public Cylinder() {
super(); length = 1.0; }
public Cylinder(double r, double l) {
super(r); length = l; }
// Getter method for length public double getLength() {
return length; }
public double findArea() {
return 2*(3/4)*bian*bian +3*bian*length; } }
11.定义一个类,描述一个家庭,其中包括私有的钱数(属性)、受保护的祖传秘方(方法,在其中写输出语句模
拟即可)、只在家族中能够使用的运输工具(方法,在其中写输出语句进行模拟),公共的门牌号码(属性)。将这个家庭放置在一个包中(一个大院),编写一个该家庭的子类,放置在另一个包中。测试其中几种被可见性修饰符修饰过的属性和方法。 程序一:
package yard;
public class Family{ private double money;
19
void useCar(){ System.out.println(\ } protected void peiFang(){ System.out.println(\ } public int familyNum; } 程序二:
package dalian;
import yard.Family; public class TestVisible{ public static void main(String[] args){ Family f=new Family();
System.out.println(\门牌号码是:\ //System.out.println(\钱数\ //f.useCar(); //f.peiFang();
SunFamily sf=new SunFamily(); //sf.peiFang(); sf.usePeiFang(); } }
class SunFamily extends Family{ public void usePeiFang(){ this.peiFang(); } public void usePlane(){ System.out.println(\ } }
12.定义一个接口CanFly,描述会飞的方法public void fly();
分别定义类飞机和鸟,实现CanFly接口。
定义一个测试类,测试飞机和鸟,在main方法中创建飞机对象和鸟对象,再定义一个makeFly()方法,其中让会飞的事物飞。并在main方法中调用该方法,让飞机和鸟起飞。 class InterfaceSample
{
public static void main(String arg[]) { Plane p1=new Plane(); Bird b1=new Bird(); Dog d1=new Dog(); makeFly(p1); makeFly(b1);
if (d1 instanceof CanFly)//作用四:用instanceof判断是否实现了接口,即是否能飞 { System.out.println(\
20