类的继承与多态性
1.1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长
(2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。
该类包含有成员变量:
radius:public 修饰的double类型radius,表示圆的半径。
x:private修饰的double型变量x,表示圆心的横坐标。
y:protected修饰的double型变量y,表示圆心的纵坐标。
包含的方法有:
Circle(double radius) 有参构造方法。以形参表中的参数初始化半径,圆心为坐标原点。 double getRadius():获取半径为方法的返回值。void setCenter(double x, double y):利用形参表中的参数设置类Circle的圆心坐标。void setRadius(double radius):利用形参表中的参数设置类Circle的radius域。
//接口里只能写常量和抽象方法 public interface ShapePara { int getArea(); int getCircumference(); }
public class Circle implements ShapePara { public double radius; private double x; protected double y; public Circle(double radius) { this.radius=radius; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public void setCenter(double x,double y) { System.out.println("圆心为:"+"("+x+","+y+")"); } @Override public int getArea() { // TODO 自动生成的方法存根 double area =Math.PI*Math.pow(radius, 2); return (int)area; } @Override public int getCircumference() { // TODO 自动生成的方法存根 return 0; } }
public class TestCircle { public static void main(String[] args) { // TODO 自动生成的方法存根 Circle c = new Circle(3); System.out.println("圆的面积是:"+c.getArea()); } }
2.定义图形类Shape,该类中有获得面积的方法getArea();定义长方形类Rect,该类是Shape的子类,类中有矩形长和宽的变量double a,double b,设置长和宽的方法setWidth()、setHeight(),使用getArea()求矩形面积;利用getArea方法实现题1中圆面积的求解。
public class Rect extends Shape { private double a; private double b; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public void setHeight(double a) { this.a =a; } public void setWidth(double b) { this.b =b; } public double getArea() { return a*b; } }
public class Test { public static void main(String[] args) { // TODO 自动生成的方法存根 Rect r = new Rect(); r.setHeight(30); r.setWidth(40); System.out.println("矩形的面积是:"+r.getArea()); } }
3. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。
public class Animal { private String name; private int legs; int count; //获得腿数量的方法 public int getLegs() { return legs; } public void setLegs(int legs) { this.legs = legs; } //获取动物名称的方法 public String getKind() { return name; } public void setKind(String name) { this.name = name; } //获取动物数量的方法 public int getCount() { return count; } public void setCount(int count) { this.count = count; }
public class Fish extends Animal { }
public class Tiger extends Animal { }
public class SouthEastTiger extends Tiger { }
public class TestAnimal { public static void main(String[] args) { // TODO 自动生成的方法存根 //向上转型 Animal a = new Fish(); //统计数量 a.count=10; //获取数量 System.out.println("鱼的数量"+a.getCount()); Animal b =new Tiger(); b.count=30; System.out.println("老虎的数量"+b.getCount()); Animal c =new SouthEastTiger(); c.count=20; System.out.println("东北虎的数量"+c.getCount()); } }