| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 495 人关注过本帖
标题:接口多态性
取消只看楼主 加入收藏
小情歌
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-3-18
结帖率:100%
收藏
 问题点数:0 回复次数:0 
接口多态性
interface Scaleable
{
    double area();
   void scale(double proportion);
}

class Circle implements Scaleable
{
    private double radius;
   
    public Circle(double radius)
    {
        this.radius=radius;
    }
   
    public void setRadius(double radius)
    {
        this.radius=radius;
    }
   
    public double getRadius()
    {
        return radius;
    }
   
    public double area()
    {
        return Math.PI*radius*radius;
    }

     public void scale(double proportion)    //    放缩圆的方法
    {
        setRadius(proportion*getRadius());
    }
   
}


public class Test11
{
  public static void main(String args[])
  {
      Scaleable s=new Circle(10);
      s.scale(2);
      s.getRadius();     //   为什么不能用接口引用访问Circle的getRadius()方法呢???
     System.out.println(s.area()+"放缩后的半径是:"+s.getRadius()); // 通过接口引用访问对象所在类的方法,scale(),area()都可以被访问,为什么getRadius()就不能被访问呢
   }
  
}

搜索更多相关主题的帖子: 多态 接口 
2010-04-20 10:17
快速回复:接口多态性
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021693 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved