1. 建立一个动物的层次结构,以抽象类Animal为根,Cat、Spider和Fish 动物类实现接口Pet。使用接口和抽象类技术完成。类结构如图1所示。
完成:
① 创建Animal类,它是所有动物类的抽象父类。
② 创建Spider类,继承Animal类。
③ 创建Pet接口。
④ 创建Cat类,继承Animal类并实现Pet接口。
⑤ 创建Fish类,继承Animal类并实现Pet接口。
⑥ 使用AnimalsTest类测试代码。
(1)shiyan4_1_Animal
package shiyan4;
public class shiyan4_1_Animal {
public int legs;
public void walk() {
System.out.println("this animal's walk.");
}
public void eat() {
System.out.println("this animal's eat.");
}
}
(2)shiyan4_1_Cat
package shiyan4;
public class shiyan4_1_Cat extends shiyan4_1_Animal implements Pet{
public String name;
public void getName() {
System.out.println("this Cat's getName.");
}
public void setName(String name) {
this.name=name;
System.out.println("this Cat's setName."+" and "+"this cat's name is:"+this.name);
}
public void play() {
System.out.println("this Cat's play.");
}
public void eat() {
System.out.println("this Cat's eat.");
}
public void Cat(String name) {
System.out.println("this Cat's Cat(String name).");
}
public void Cat() {
System.out.println("this Cat's Cat(String name).");
}
}
(3)shiyan4_1_Fish
package shiyan4;
public class shiyan4_1_Fish extends shiyan4_1_Animal implements Pet{
public String name;
public void getName() {
System.out.println("this Fish's getName."+" and "+"this fish's name is:"+name);
}
public void setName(String name) {
this.name=name;
System.out.println("this Fish's setName.");
}
public void play() {
System.out.println("this Fish's play.");
}
public void eat() {
System.out.println("this Fish's eat.");
}
public void walk() {
System.out.println("this Fish's walk.");
}
public void Fish() {
System.out.println("this Fish's Fish.");
}
}
(4)shiyan4_1_Spider
package shiyan4;
public class shiyan4_1_Spider extends shiyan4_1_Animal implements Pet{
public void getName() {
System.out.println("this Spider's getName.");
}
public void setName(String name) {
System.out.println("this Spider's setName."+" and "+"this spider's name is:"+name);
}
public void play() {
System.out.println("this Spider's play.");
}
public void eat() {
System.out.println("this Spider's eat.");
}
public void Spider() {
System.out.println("this Spider's Spider.");
}
public void newOperation() {
System.out.println("this Spider's newOperation.");
}
}
(5)shiyan4_1_AnimalTest
package shiyan4;
interface Pet {
void getName();
void setName(String name);
void play();
}
public class shiyan4_1_AnimalTest {
public static void main(String[] args) {
Pet cat;//申明接口变量
cat=new shiyan4_1_Cat();
cat.getName();
cat.setName("小猫");
Pet spider;
spider=new shiyan4_1_Spider();
spider.play();
spider.setName("蜘蛛");
spider.getName();
Pet fish;
fish=new shiyan4_1_Fish();
fish.play();
fish.setName("小鱼");
fish.getName();
}
}
2.建立一个抽象Shape类,有Circle(圆形)和Rect(矩形)子类。Shape类有zhouchang()和area()两种抽象方法。Rect类有cha()方法用于比较长宽的差,若长大于宽输出“长比宽大”,否则输出“宽比长大”。(正方形)Squ为Rect子类,在Squ类中,重写cha()方法,并输出“长等于宽”
要求:
① 要提供Circle和Rect类重写父类Shape的zhouchang()和area()方法。
② Circle类要有静态常量PI。
③ 为Circle类,Rect类,Squ类提供构造方法,为属性赋初值。
④ 编写测试类,测试上述程序。
(1)shiyan4_2_Shape
package shiyan4;
public abstract class shiyan4_2_Shape {
public double PI=3.1415926;
//在定义方法时,可以只给出方法头而不给出方法体即方法的实现细节,
//这样的方法称为抽象方法
public abstract void zhouchang() ;
public abstract void area();
//定义抽象类是用来在定义新类时来继承,当子类继承抽象类时,
//子类必须实现抽象类中的所有抽象方法,否则子类也要声明为抽象类;
}
(2)shiyan4_2_Circle
package shiyan4;
public class shiyan4_2_Circle extends shiyan4_2_Shape {
public double r;
public void zhouchang() {
double zhouchang=2*this.PI*this.r;
System.out.println("周长:"+zhouchang);
}
public void area() {
double area=2*this.PI*this.r;
System.out.println("面积"+area);
}
}
(3)shiyan4_2_Rect
package shiyan4;
public class shiyan4_2_Rect extends shiyan4_2_Shape {
public double chang;
public double kuan;
public void zhouchang() {
double zhouchang=2*(this.kuan+this.chang);
System.out.println("周长:"+zhouchang);
}
public void area() {
double area=this.chang*this.kuan;
System.out.println("面积"+area);
}
public void cha() {
// 用于比较长宽的差,若长大于宽输出“长比宽大”,否则输出“宽比长大”
if (this.chang < this.kuan) {
System.out.println("宽比长大");
} else {
System.out.println("长比宽大");
}
}
}
(3)shiyan4_2_Squ
package shiyan4;
public class shiyan4_2_Squ extends shiyan4_2_Rect{
public void cha() {
//输出长等于宽
if (this.chang == this.kuan) {
System.out.println("长等于宽");
}
}
}
(4)shiyan4_2_ShapeTest
package shiyan4;
public class shiyan4_2_ShapeTest {
public static void main(String[] args) {
shiyan4_2_Circle shape1 = new shiyan4_2_Circle();
shape1.r = 12.0;
shape1.zhouchang();
shape1.area();
shiyan4_2_Rect shape2 = new shiyan4_2_Rect();
shape2.chang = 3.0;
shape2.kuan = 4.0;
shape2.zhouchang();
shape2.area();
}
}
3. 一个运输公司从网上得到订单,订单上标有货物重量和运输里程,该公司可以使用三种运输工具:卡车,火车,飞机。
编写运输接口,声明三个接口常量分别表示运输工具:
int PLANE=1;
int TRAIN=2;
int TRUCK=3;
同时声明一个计算费用的方法,函数的参数是重量和里程。
定义卡车类,火车类,飞机类分别实现运输接口,计算运费的方法如下:
① 卡车:运费=重量*距离*120。当距离大于1000(km)或重量大于60(t)的时候拒载,返回-1。
② 火车:当距离在900(km)内(包含)时,运费=重量*距离*250,大于900(km)运费=重量*距离*300。
③ 飞机:当距离大于500(km)时,运费=重量*距离*750,否则拒载,返回-1。
(1)shiyan4_3_transport
package shiyan4;
public interface shiyan4_3_transport {
public static final int Truck = 1;
public static final int Train = 2;
public static final int Plane = 3;
public double price(double weight, double distance);
}
(2)shiyan4_3_plane
package shiyan4;
public class shiyan4_3_plane implements shiyan4_3_transport {
public double price(double weight, double distance) {
double price;
if(distance>500) {
price=weight*distance*750;
}
else {
price=-1;
}
return price;
}
}
(3)shiyan4_3_train
package shiyan4;
public class shiyan4_3_train implements shiyan4_3_transport{
public double price(double weight, double distance) {
double price;
if(distance<900) {
price=weight*distance*250;
}
else {
price=weight*distance*300;
}
return price;
}
}
(4)shiyan4_3_truck
package shiyan4;
public class shiyan4_3_truck implements shiyan4_3_transport{
public double price(double weight, double distance) {
double price;
if(distance>1000 || weight>60) {
price=-1;
System.out.println("distance为:"+distance+"weight为:"+weight);
}
else {
price=weight*distance*120;
}
return price;
}
}
(5)shiyan4_3_transportTest
package shiyan4;
public class shiyan4_3_transportTest {
public static void main(String [] args) {
shiyan4_3_truck transport1=new shiyan4_3_truck();
double price1=transport1.price(50, 250);
System.out.println("运输类型"+transport1.Truck);
System.out.println("运输价格"+price1);
shiyan4_3_train transport2=new shiyan4_3_train();
double price2=transport2.price(50, 250);
System.out.println("运输类型"+transport2.Train);
System.out.println("运输价格"+price2);
shiyan4_3_plane transport3=new shiyan4_3_plane();
double price3=transport3.price(10, 1150);
System.out.println("运输类型"+transport3.Plane);
System.out.println("运输价格"+price3);
}
}
知识点
1. 抽象类的使用规则如下:
abstract修饰符可以修饰类和方法;
抽象类不能实例化对象,只能什么引用变量;
含有抽象方法的类必须声明为抽象类;
定义抽象类的主要目的是在定义新类时继承该抽象类。通过继承,由其子类来发挥作用,抽象类实现了代码的重用和规划作用。当子类继承抽象类时,必须实现抽象类中的所有抽象方法,否则子类也要声明为抽象类。
2. 定义接口使用关键字interface,接口是抽象方法和常量值定义的集合,不包含变量和方法的实现。
3. 定义接口的主要目的就是让不同的类来实现,使接口起到中间桥梁的作用。接口的成员都是公有的。
4. 接口和抽象类本质上是相似的,都是抽象的概念。只是接口比抽象类更抽象,无法去具体描述一个对象,所以接口和抽象类一样不能实例化对象,只能声明引用变量。
5. 接口回调:
把实现某一接口的类创建的对象的引用赋给该接口声明的变量中,该接口变量就可以调用被类实现的接口方法。接口回调类似于上转型对象调用子类的重写方法。
6. 接口的多态性:
接口变量能指向任意类的对象,即使对象间没有任何关系,只要这些对象的类实现了该接口。
7. 抽象类与接口的共同点和区别。
共同点:
都包含抽象方法;
都不能被实例化;
都是引用数据类型。
区别:
一个类只能继承一个抽象类,而一个类可以实现多个接口;
接口中的成员的访问权限都是public,而抽象类中成员的访问权限和普通类一样;
抽象类中可以什么成员变量,成员变量的值可以被更改,接口中只能什么常量;
抽象类中可以声明抽象方法、普通成员方法以及构造方法,接口中只能声明抽象方法。
如何获取马志强、张然、编著的Java核心技术电子书或课后习题答案?
wx搜索“校园聚之”,回复“Java课后题”,“Java电子书”即可领取
(完)