Java中继承与多态(续篇)
按照Animal类及其子类继承关系UML图,编写代码
class Animal {
private double weight ;
public void eat ( ) {
System. out. println ( "我可以吃任何东西" ) ;
}
}
class Bird extends Animal {
public int numberOfWings ;
public void fly ( ) {
System. out. println ( "我可以飞" ) ;
}
}
class Fish extends Animal {
public int numberOfFins ;
public void swim ( ) {
System. out. println ( "我可以游泳" ) ;
}
}
class Dog extends Animal {
public int numberOfLegs ;
public void walk ( ) {
System. out. println ( "我可以步行" ) ;
}
}
public class AnimalTest {
public static void main ( String[ ] args) {
Bird bird = new Bird ( ) ;
Fish fish = new Fish ( ) ;
Dog dog = new Dog ( ) ;
bird. fly ( ) ;
fish. swim ( ) ;
dog. walk ( ) ;
}
}
定义圆柱类Cylinder,它继承Circle类,要求定义一个圆柱高度height 覆盖getArea()方法求圆柱的表面积,定义getVolume()方法求圆柱的体积 定义默认构造方法和带radius和height两个参数的构造方法
import java. util. Scanner;
class Circle {
private double centerX ;
private double centerY ;
public double radius ;
public Circle ( ) {
this ( 1.0 ) ;
}
public Circle ( double radius) {
this . radius = radius ;
}
public double getArea ( ) {
return Math. PI * radius * radius ;
}
public double getPerimeter ( ) {
return 2 * Math. PI * radius ;
}
public void setRadius ( double radius) {
this . radius = radius ;
}
public double getRadius ( ) {
return radius ;
}
}
public class Cylinder extends Circle {
private double height ;
public Cylinder ( ) { }
public Cylinder ( double radius, double height) {
super ( radius) ;
this . height = height ;
}
@Override
public double getArea ( ) {
return super . getPerimeter ( ) * height ;
}
public double getVolume ( ) {
return super . getArea ( ) * height ;
}
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
System. out. println ( "请输入圆柱底面的半径:" ) ;
double radius = input. nextDouble ( ) ;
System. out. println ( "请输入圆柱的高:" ) ;
double height = input. nextDouble ( ) ;
Cylinder cylinder = new Cylinder ( radius, height) ;
System. out. println ( "圆柱的表面积是:" + cylinder. getArea ( ) ) ;
System. out. println ( "圆柱的体积是:" + cylinder. getPerimeter ( ) ) ;
}
}
class Auto {
private double speed ;
public void start ( ) {
System. out. println ( "汽车被启动了" ) ;
}
public void speedUp ( double speed) {
this . speed = speed ;
System. out. println ( "汽车的速度到达:" + speed) ;
}
public void stop ( ) {
this . speed = 0 ;
System. out. println ( "汽车停下来了" ) ;
}
}
class Bus extends Auto {
private int passenger ;
public Bus ( int passenger) {
this . passenger = passenger ;
}
public void goOn ( int number) {
passenger += number ;
System. out. println ( "车上的乘客一共" + passenger + "人" ) ;
}
public void goOff ( int number) {
passenger -= number ;
System. out. println ( "车上的乘客一共" + passenger + "人" ) ;
}
}
public class BusTest {
public static void main ( String[ ] args) {
Bus bus = new Bus ( 0 ) ;
bus. start ( ) ;
bus. speedUp ( 60 ) ;
bus. stop ( ) ;
bus. goOn ( 10 ) ;
bus. goOff ( 5 ) ;
}
}
定义一个正方形类Square,继承抽象类Shape,覆盖Shape类中的抽象方法getPerimeter()和getArea()
abstract class Shape {
private String name ;
public Shape ( ) { }
public Shape ( String name) {
this . name = name ;
}
public void setName ( String name) {
this . name = name ;
}
public String getName ( ) {
return name ;
}
public abstract double getArea ( ) ;
public abstract double getPerimeter ( ) ;
}
public class Square extends Shape {
private double side ;
public Square ( double side) {
super ( "正方形" ) ;
this . side = side ;
}
@Override
public double getArea ( ) {
return side * side ;
}
@Override
public double getPerimeter ( ) {
return 4 * side;
}
public static void main ( String[ ] args) {
Square square = new Square ( 2.0 ) ;
square. setName ( "正方形" ) ;
System. out. println ( square. getName ( ) + "的面积为:" + square. getArea ( ) ) ;
System. out. println ( square. getName ( ) + "的周长为:" + square. getPerimeter ( ) ) ;
}
}
定义一个名字为Cuboid的长方体类,继承Rectangle类,包含double型的成员变量height 定义一个含长宽高的构造方法,再定义一个求体积的方法
class Rectangle {
public double length ;
public double width ;
public Rectangle ( double length, double width) {
this . length = length ;
this . width = width ;
}
}
public class Cuboid extends Rectangle {
private double height ;
public Cuboid ( double length, double width, double height) {
super ( length, width) ;
this . height = height ;
}
public double getVolume ( ) {
return length * width * height ;
}
public static void main ( String[ ] args) {
Cuboid cuboid = new Cuboid ( 10 , 5 , 2 ) ;
System. out. println ( "长方体的体积为:" + cuboid. getVolume ( ) ) ;
}
}