*** 17信2班 1720501234
实验目
1掌握接口定义实现
2掌握类态性
二预容
1什接口接口中包含容实现接口
接口方法特征集合
包含方法常量
类中实现接口关键字implements基格式:
[修饰符] class <类名> [extends 父类名] [implements 接口列表]{
}
2 填相关容
三实验容
1定义ShapeIF接口包括常量PI(圆周率)求面积方法s()求周长方法l()建立圆类(C)矩形类(R)实现ShapeIF接口建立运行类ShapeRun类运行CR
提示代码
接口ShapeIF
public interface ShapeIF {
double PI31416
public double S()
public double L()
}
形状类Shape
public class Shape {
private double x
private double y
public Shape()
{ x0y0}
public Shape(double xdouble y)
{ thisxxthisyy }
public double getX()
{ return x }
public void setX(double x)
{ thisxx }
public double getY()
{ return y }
public void setY(double y)
{ thisyy }
public double distance(double xdouble y)
{ return Mathsqrt((thisxx)*(thisxx)+(thisyy)*(thisyy)) }
}
圆类C
public class C extends Shape implements ShapeIF {
private double r
public C(){ super() r0 }
public C(double r){super() thisrr}
public C(double xdouble ydouble r){super(xy) thisrr}
public double S(){
return PI*r*r
}l
Public double L(){
return 2* ShapeIF PI*r
}
矩形类R
public class R extends Shape implements ShapeIF {
private double a长
private double b宽
构造方法
R()
{ super()a0b0 }
R(double newAdouble newB)
{ super()anewA bnewB }
R(double newXdouble newYdouble newAdouble newB)
{ super(newXnewY)anewA bnewB }
成员方法
Public double S(){
return a*b
}
Public double L(){
return 2*a+2*b
}
运行类ShapeRun
public class ShapeRun {
public static void main(String args[])
{
R r1
C c1
r1new R(22810)
Systemoutprintln(r1S())
Systemoutprintln(r1L())
c1new C(125)
Systemoutprintln(c1S())
Systemoutprintln(c1L())
}
}
2请设计接口ShapeIF(题)DistanceIFDistanceIF包括两方法方法D(double xdouble y)求点(xy)该Shape中心距离方法DD(double xdouble y)求点(xy)图形位置关系建立圆类(CC)实现ShapeIF接口DistanceIF接口建立运行类ShapeRun1类运行CC
提示代码
接口ShapeIF(题)
父类Shape(题)
接口DistanceIF
Public interface DistanceIF {
double D(double xdouble y)
double DD(double xdouble y)
}
圆类CC
public class CC extends Shape implements ShapeIF DistanceIF {
private double r
public CC(){ super() r0 }
public CC(double r){super() thisrr}
public CC(double xdouble ydouble r){super(xy) thisrr}
public double S(){
return ShapeIF PI *r*r
}
public double L(){
return PI*2*r
}
public double D(double xdouble y){
return Mathsqrt((ShapeIFxx)*( ShapeIFxx)+( ShapeIFyy)*( ShapeIFyy))
}
public double DD(double xdouble y){
if(D(xy)
else return 2 返回2表示圆外
}
}
运行类ShapeRun1
public class ShapeRun1 {
public static void main(String args[])
{
String str
CC c1new CC(125)果句改ShapeIF c1new CC(125)
者改DistanceIF c1new CC(125)
第结果调DDD方法第二结果调SL方法
接口CC类中已实现ShapeIFDistanceIF接口中方法CC类中没接口定义方法访问 程序会出现什情况请讲明原
Systemoutprintln(c1S())
Systemoutprintln(c1L())
Systemoutprintln(c1D(3 4))
if(c1DD(34)0)str 圆
else if(c1DD(34)1)str 圆
else str圆外
Systemoutprintln(str)
}
}
3面三类(PointColorPointMapPoint)中PointColorPoint父类ColorPointMapPoint父类运行类UseAllPointPointColorPointMapPoint中相方法public void draw()请家根方法理解态性完成程序中 注释部分
提示代码
类Point
public class Point {
protected int x
protected int y
protected Point(int x int y)
{ thisxx
thisyy
}
public void draw() 父类draw()方法
{ Systemoutprintln(Pointa point(+thisx++thisy+))
}
}
类ColorPoint
public class ColorPoint extends Point
*类ColorPoint类Point直接子类类Point类ColorPoint
直接父类类ObjectColorPoint缺省父类直接
父类*
{ protected int color
protected ColorPoint(int xint yint color)
{super(xy)
thiscolorcolor}
public void draw() 覆盖父类draw()类
{
Systemoutprintln(ColorPoint a color point (+thisx++thisy++thiscolor+))
}
}
类MapPoint
public class MapPoint extends ColorPoint
*类MapPoint 类ColorPoint直接子类 类ColorPoint类MapPoint
直接父类类ObjectPointMapPoint直接父类
*
{ String name 图点名字城市名
protected MapPoint(int xint yint colorString name)
{
super(xycolor)
thisnamename
}
public void draw() 覆盖父类draw()类
{
Systemoutprintln(MapPoint a map color point (+thisx++thisy++thiscolor++thisname+))
}
}
类UseAllPoint
public class UseAllPoint {
public static void main(String args[])
{
Point p1new Point(12) 实例化Point类象
p1draw() 调pointdraw方法
p1new ColorPoint(123)
p1draw() 父类引指ColorPoint子类象先调象方法
p1new MapPoint(123123)
p1draw() 父类引指MapPoint子类象 先调象方法
}
}
4创建名称Vehicle接口接口中添加两带参数方法start()stop()两名称分BikeBus类中实现Vehicle接口创建名称interfaceDemo类interfaceDemomain()方法中创建BikeBus象访问start()stop()方法
public interface ShapeIF {
public double start(double x)
public double stop(double x)
}
public class Bike implements ShapeIF {
@Override
public double start(double x) {
TODO 动生成方法存根
return 1
}
@Override
public double stop(double x) {
TODO 动生成方法存根
return 1
}
}
public class Bus implements ShapeIF{
@Override
public double start(double x) {
TODO 动生成方法存根
return 2
}
@Override
public double stop(double x) {
TODO 动生成方法存根
return 0
}
}
public class Bus implements ShapeIF{
@Override
public double start(double x) {
TODO 动生成方法存根
return 2
}
@Override
public double stop(double x) {
TODO 动生成方法存根
return 0
}
}
5创建名称CCCircle类表示面坐标系圆形求圆心点CCCircle部类Point表示CCCircle类中包含求点圆心距离方法public double distance(Piont p)main方法中实例化圆c((1010)2)点p(55)求pc间关系
四思考题
1.简述接口抽象类区
接口抽象类概念样接口动作抽象抽象类根源抽象
2.方法态性实现手段什
1虚方法 2抽象类 3接口
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档