| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1341 人关注过本帖
标题:java的父类和子类,这是矛盾...
只看楼主 加入收藏
ZYHLouis
Rank: 1
等 级:新手上路
帖 子:17
专家分:3
注 册:2011-9-23
收藏
 问题点数:0 回复次数:12 
java的父类和子类,这是矛盾...

public class P134 {//父类
    /*圆的半径*/
    private double radius;
    /**
     * 第一个构造方法
     * 构造一个单位圆
     */
    public P134(){
        this(1.0);
    }
    /**
     * 第二个构造方法
     * 圆的半径
     */
    public P134(double radius){
        setRadius(radius);
    }
    /**
     * 半径更改器
     * 圆的半径
     */
    public void setRadius(double radius){
        /*在方法中加入参数检查,确保不会传入负值*/
        if(radius<0){
            System.out.println("圆的半径不能为负值");
            this.radius=1.0;
        }else{
            this.radius=radius;
        }
    }
    /**
     * 半径获取器
     * 返回值的半径
     */
    public double getRadius(){
        return radius;
    }
    /**
     * 计算圆的面积
     * 返回圆的面积
     */
    public double computerArea(){
        return Math.PI*radius*radius;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建一个Cylinder对象,显示它的属性
        Cylinder myCylinder=new Cylinder(10.0, 5.0);
        
        System.out.println("The height is "+myCylinder.getHeight());
        System.out.println("The radius is "+myCylinder.getRadius());
        System.out.println("The volume of the cylinder is "
                +());
        System.out.println("The area of the circle is "+());
    }

}



public class Cylinder extends P134{//子类
    private double height;
    /**
     * 默认构造方法
     * 构造一个底面圆为单位圆,高度等于1.0圆柱体
     */
    public Cylinder(){
        super();
        height=1.0;
    }
    /**
     * 第二个构造方法
     * 底面圆的半径,圆柱体的高度
     */
    public Cylinder(double radius,double height){
        super(radius);
        setHeight(height);
    }
    /**
     * 高度更改器
     * 圆柱体的高度
     */
    public void setHeight(double height){
        /*方法加入了参数检查,确保不会传入负值*/
        if(height<0){
            System.out.println("圆柱体的高度不能为负值.");
            this.height=1.0;
        }else{
            this.height=height;
        }
    }
    /**
     * 高度获取器
     * 圆柱体的高度
     */
    public double getHeight(){
        return height;
    }
    /**
     * 计算圆柱体体积
     */
    public double computerVolume(){
        return computerArea()*height;
    }
}
怎么上面子类和父类是在同个java文档却用两个class隔开
public class P134 {
    /*圆的半径*/
    private double radius;
    /**
     * 第一个构造方法
     * 构造一个单位圆
     */
    public P134(){
        this(1.0);
    }
    /**
     * 第二个构造方法
     * 圆的半径
     */
    public P134(double radius){
        setRadius(radius);
    }
    /**
     * 半径更改器
     * 圆的半径
     */
    public void setRadius(double radius){
        /*在方法中加入参数检查,确保不会传入负值*/
        if(radius<0){
            System.out.println("圆的半径不能为负值");
            this.radius=1.0;
        }else{
            this.radius=radius;
        }
    }
    /**
     * 半径获取器
     * 返回值的半径
     */
    public double getRadius(){
        return radius;
    }
    /**
     * 计算圆的面积
     * 返回圆的面积
     */public class Cylinder extends P134{
    private double height;
    /**
     * 默认构造方法
     * 构造一个底面圆为单位圆,高度等于1.0圆柱体
     */
    public Cylinder(){
        super();
        height=1.0;
    }
    /**
     * 第二个构造方法
     * 底面圆的半径,圆柱体的高度
     */
    public Cylinder(double radius,double height){
        super(radius);
        setHeight(height);
    }
    /**
     * 高度更改器
     * 圆柱体的高度
     */
    public void setHeight(double height){
        /*方法加入了参数检查,确保不会传入负值*/
        if(height<0){
            System.out.println("圆柱体的高度不能为负值.");
            this.height=1.0;
        }else{
            this.height=height;
        }
    }
    /**
     * 高度获取器
     * 圆柱体的高度
     */
    public double getHeight(){
        return height;
    }
    /**
     * 计算圆柱体体积
     */
    public double computerVolume(){
        return computerArea()*height;
    }
}

    public double computerArea(){
        return Math.PI*radius*radius;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建一个Cylinder对象,显示它的属性
        Cylinder myCylinder=new Cylinder(10.0, 5.0);
        
        System.out.println("The height is "+myCylinder.getHeight());
        System.out.println("The radius is "+myCylinder.getRadius());
        System.out.println("The volume of the cylinder is "
                +());
        System.out.println("The area of the circle is "+());
    }

}
这样怎么不行?
搜索更多相关主题的帖子: java 检查 private public double 
2011-09-23 22:37
ZYHLouis
Rank: 1
等 级:新手上路
帖 子:17
专家分:3
注 册:2011-9-23
收藏
得分:0 
求救啊
2011-09-23 23:25
让我们飞
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:40
专家分:119
注 册:2011-1-9
收藏
得分:0 
你要问的是什么?
2011-09-24 12:33
ZYHLouis
Rank: 1
等 级:新手上路
帖 子:17
专家分:3
注 册:2011-9-23
收藏
得分:0 
为什么这个父类和子类不能放在同一个class文件中
2011-09-25 18:19
风兮飞扬
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2011-09-25 20:46
让我们飞
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:40
专家分:119
注 册:2011-1-9
收藏
得分:0 
同上
这个看着很头疼

2011-09-26 10:40
ZYHLouis
Rank: 1
等 级:新手上路
帖 子:17
专家分:3
注 册:2011-9-23
收藏
得分:0 
public class P134 {//父类
     /*圆的半径*/
     private double radius;
        public P134(){
         this(1.0);
     }
     public P134(double radius){
         setRadius(radius);
     }
     public void setRadius(double radius){
          if(radius<0){
             System.out.println("圆的半径不能为负值");
             this.radius=1.0;
         }else{
             this.radius=radius;
         }
     }
     /**
      * 半径获取器
      * 返回值的半径
      */
     public double getRadius(){
         return radius;
     }
     public double computerArea(){
         return Math.PI*radius*radius;
     }
     public static void main(String[] args) {
         // TODO Auto-generated method stub
         //创建一个Cylinder对象,显示它的属性
         Cylinder myCylinder=new Cylinder(10.0, 5.0);
         
         System.out.println("The height is "+myCylinder.getHeight());
         System.out.println("The radius is "+myCylinder.getRadius());
         System.out.println("The volume of the cylinder is "
                 +());
         System.out.println("The area of the circle is "+());
     }
 
}
 这个是父类,下面我还有个子类
2011-09-28 11:09
ZYHLouis
Rank: 1
等 级:新手上路
帖 子:17
专家分:3
注 册:2011-9-23
收藏
得分:0 
这就是子类,他们有关系,为什么它和父类不能放在同一个class的文档中          我用的是这款软件MyEclipse 8.5
public class Cylinder extends P134{
     private double height;
      public Cylinder(){
         super();
         height=1.0;
     }
     public Cylinder(double radius,double height){
         super(radius);
         setHeight(height);
     }
     public void setHeight(double height){
         if(height<0){
             System.out.println("圆柱体的高度不能为负值.");
             this.height=1.0;
         }else{
             this.height=height;
         }
     }
     public double getHeight(){
         return height;
     }
     public double computerVolume(){
         return computerArea()*height;
     }
 }
 
2011-09-28 11:13
哈狄斯
Rank: 2
等 级:论坛游民
帖 子:45
专家分:16
注 册:2006-3-14
收藏
得分:0 
你都用public修饰了,当然不行了,去掉一个应该就可以了~~~
2011-09-28 15:44
ZYHLouis
Rank: 1
等 级:新手上路
帖 子:17
专家分:3
注 册:2011-9-23
收藏
得分:0 
谢谢.
2011-09-29 09:11
快速回复:java的父类和子类,这是矛盾...
数据加载中...
 
   



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

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