Java 练习题 15.1 抽象类与接口
关注公众号野心与家回复"11.23"获取原文件
第一部分:抽象类
1、定义一个Shape类:
含有属性:半径radius
包含1个方法:用于求体积的cubage();
其中cubage方法是抽象的。
从Shape类派生出三个子类:
球类(Globe)、圆柱类(Cylinder),圆锥类(Cone),分别实现了cubage方法。
进行测试,当给出需要的数值时,求出对应的形状的体积。
方法一:
package com.shangjiti.aoian;
public class No1 {
public static void main(String[] args) {
Globe g=new Globe(3.0);
System.out.println("半径为3.0的圆的体积为:"+g.cubage());
Cylinder c=new Cylinder(3.0,3.0);
System.out.println("半径为3.0,高度为3.0的圆锥的体积为:"+c.cubage());
Cone cone=new Cone(3.0,3.0);
System.out.println("半径为3.0,高度为3.0的圆柱的体积为:"+cone.cubage());
}
}
abstract class Shape{
double radius;
public abstract double cubage();
}
class Globe extends Shape{
public Globe(double r) {
this.radius=r;
}
public double cubage() {
return Math.PI*Math.pow(radius, 3)*(double)4/3;
}
}
class Cylinder extends Shape{
double height;
public Cylinder(double r,double h) {
this.radius=r;
this.height=h;
}
public double cubage(){
return Math.PI*Math.pow(radius, 2)*((double)1/3)*height;
}
}
class Cone extends Shape{
double height;
public Cone(double r,double h) {
this.radius=r;
this.height=r;
}
public double cubage() {
return Math.PI*Math.pow(radius, 2)*height;
}
}
方法二:
package com.shangjiti.aoian;
public class No1 {
public static void main(String[] args) {
Globe g=new Globe(3.0);
Cylinder c=new Cylinder(3.0,3.0);
Cone cone=new Cone(3.0,3.0);
g.cubage();
c.cubage();
cone.cubage();
}
}
abstract class Shape{
double radius;
public abstract void cubage();
}
class Globe extends Shape{
public Globe(double r) {
this.radius=r;
}
public void cubage() {
System.out.println("半径为"+radius+"的圆的体积为:"+Math.PI*Math.pow(radius, 3)*(double)4/3);
}
}
class Cylinder extends Shape{
double height;
public Cylinder(double r,double h) {
this.radius=r;
this.height=h;
}
public void cubage(){
System.out.println("半径为"+radius+",高度为"+height+"的圆锥的体积为:"+Math.PI*Math.pow(radius, 2)*((double)1/3)*height);
}
}
class Cone extends Shape{
double height;
public Cone(double r,double h) {
this.radius=r;
this.height=r;
}
public void cubage() {
System.out.println("半径为"+radius+",高度为"+height+"的圆柱的体积为:"+Math.PI*Math.pow(radius, 2)*height);
}
}
2、定义一个抽象类机动车Motovercal,里面有车牌号no,类型type,价格price属性,里面有一个show()方法是抽象方法,定义一个轿车Car类,他有特有的属性颜色color,有一个公共汽车Bus,他有特有属性座位数seatCount,实现如图功能:
package com.shangjiti.aoian;
public class No2 {
public static void main(String[] args) {
Car c=new Car("A00001","jeep",200000,"绿色");
Bus b=new Bus("A00002","金龙",250000,30);
c.show();
b.show();
}
}
abstract class Motovercal {
String no;
String type;
double price;
abstract void show();
}
class Car extends Motovercal{
String color;
public Car(String no,String type,double price,String color) {
this.no=no;
this.type=type;
this.price=price;
this.color=color;
}
void show() {
System.out.println("本车车牌号为:"+no+",类型为:"+type+",价格为:"+price+",颜色为:"+color);
}
}
class Bus extends Motovercal{
int seatCount;
public Bus(String no,String type,double price,int seatCount) {
this.no=no;
this.type=type;
this.price=price;
this.seatCount=seatCount;
}
void show() {
System.out.println("本车车牌号为:"+no+",类型为:"+type+",价格为:"+price+",拥有:"+seatCount+"个座位");
}
}
第二部分:接口
1.实现接口
定义一个接口Usb,里面有工作方法work(),定义一个Usb鼠标UsbMouse和UsbKeyborder键盘,让两个Usb设备继承Usb接口,运行结果如下:
接口interface
package com.shangjiti.aoian;
public class No3 {
public static void main(String[] args) {
UsbMouse m=new UsbMouse();
m.work();
UsbKeyborder k=new UsbKeyborder();
k.work();
}
}
interface Usb{
void work(); //public abstract 可省略
}
class UsbMouse implements Usb{
public void work() {
System.out.println("鼠标开始工作了! 你可以移动鼠标了!");
}
}
class UsbKeyborder implements Usb{
public void work() {
System.out.println("键盘开始工作了! 你可以敲字了!");
}
}
2、按要求编写Java程序:
(1)编写一个接口:InterfaceA,只含有一个方法int method(int n);
(2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算1到n的和;
(3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算n的阶乘(n!);
(4)编写测试类。
package com.shangjiti.aoian;
public class No4 {
public static void main(String[] args) {
ClassA a=new ClassA();
ClassB b=new ClassB();
System.out.println(a.method(5));
System.out.println(b.method(5));
}
}
interface InterfaceA{
public abstract int method(int n);//public abstract 可省略
}
class ClassA implements InterfaceA{
public int method(int n) {
int sum = 0;
for(int i=1;i<=n;i++)
sum=sum+i;
return sum;
// if(n==1)
// return 1;
// else
// return n+method(n-1);
}
}
class ClassB implements InterfaceA{
public int method(int n) {
int p=1;
for(int i=1;i<=n;i++)
p=p*i;
return p;
// if(n==1)
// return 1;
// else
// return n*method(n-1);
}
第三部分:继承类并实现接口
马继承Animal,还能实现能飞的能力,简称飞马,运行结果如下:
package com.shangjiti.aoian;
public class No5 {
public static void main(String[] args) {
Horse h=new Horse();
h.fly();
}
}
abstract class Animal{
String name;
}
interface Flyable{
public abstract void fly();
}
class Horse extends Animal implements Flyable{
public void fly() {
System.out.println("小马嘟嘟,是一只会飞的飞马");
}
}