| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2954 人关注过本帖
标题:为什么这里错误
只看楼主 加入收藏
Q1992
Rank: 2
等 级:论坛游民
帖 子:63
专家分:24
注 册:2016-6-26
结帖率:56.25%
收藏
已结贴  问题点数:10 回复次数:7 
为什么这里错误
package java1;

public abstract class Shape {
    {
        System.out.println("执行Shaps的初始会");
    }
    private String color;
    public abstract double calPerimeter();
    public abstract String getType();
    public Shape(){}
    public Shape(String color)
    {
System.out.println("执行Shaps的初始构造器。。。");
this.color=color;
    }
    public    class Triangle extends Shape
    {
        private double a;
        private double b;
        private double c;
     public Triangle(String color ,double a , double b , double c)
     {
         if(a>=b+c||b>=a+c||c>=a+b)
         {
             System.out.println("三角型两边之和必须大于第三班");
             return;
         }
         this.a=a;
         this.b=b;
         this.c=c;
     }
     public double calPerimeter()
     {
         return a+b+c;
         
     }
     public String getType()
     {
         return "三角形";
     }
    }
    public static  class Circle extends Shape
    {
        private static final Shape sl = null;
        private double radius;
        public Circle (String color,double radius )
        {
            super(color);
            this.radius =radius;
        }
        public void setRandius(double radius)
        {
            this.radius =radius;
        }
        public double calPerimete()
        {
            return 2  *  Math.PI  *radius;
            
        }
        public String getType()
        {
            return  getColor()  +"圆形";
        }
        private String getColor() {
            // TODO 自动生成的方法存根
            return null;
        }
        @SuppressWarnings("null")
        public static void mani(String[] args)
        {
            Shape s1 = new Triangle("黑色",3,4,5);
                      ------------------------->报错
            Shape  s2 = new  Circle("黄色",3);
            
            System.out.println(sl.getType());
            
        }
        @Override
        public double calPerimeter() {
            // TODO 自动生成的方法存根
            return 0;
        }
    }
   
}    // TODO 自动生成的方法存根
   
图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: abstract private package public double abstract private package public double 
2017-01-16 15:10
GrayJerry
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:75
专家分:310
注 册:2015-10-20
收藏
得分:4 
你把类Triangle、Circle定义为Shape类的内部类,又把Shape定义为抽象类,所以初始化会出问题,实例化Shape类中的内部类Triangle,需要先创建一个Shape类的对象s,使用s.new Triangle()进行初始化Triangle类
2017-01-16 16:56
纵横阳仔
Rank: 7Rank: 7Rank: 7
来 自:自学成才的火星人儿
等 级:贵宾
威 望:26
帖 子:147
专家分:698
注 册:2016-10-20
收藏
得分:4 
回复 2楼 GrayJerry
但是抽象类是不可以实例化对象的。
2017-01-16 17:43
Q1992
Rank: 2
等 级:论坛游民
帖 子:63
专家分:24
注 册:2016-6-26
收藏
得分:0 
回复 2楼 GrayJerry
麻烦大神演示怎么改错误代码演示一下不胜感激
2017-01-16 19:56
GrayJerry
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:75
专家分:310
注 册:2015-10-20
收藏
得分:0 
可以把Triangle定义为静态内部类,也可以把Triangle拿出来,在Shape类外定义,代码如下:
静态内部类:
package java1;

public abstract class Shape {
    {
        System.out.println("执行Shaps的初始会");
    }
    private String color;

    public abstract double calPerimeter();

    public abstract String getType();

    public Shape() {
    }

    public Shape(String color) {
        System.out.println("执行Shaps的初始构造器。。。");
        this.color = color;
    }
   
    public static class Triangle extends Shape {
        private double a;
        private double b;
        private double c;

        public Triangle(String color, double a, double b, double c) {
            if (a >= b + c || b >= a + c || c >= a + b) {
                System.out.println("三角型两边之和必须大于第三班");
                return;
            }
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public double calPerimeter() {
            return a + b + c;

        }

        public String getType() {
            return "三角形";
        }
    }

    public static class Circle extends Shape {
        private static final Shape sl = null;
        private double radius;

        public Circle(String color, double radius) {
            super(color);
            this.radius = radius;
        }

        public void setRandius(double radius) {
            this.radius = radius;
        }

        public double calPerimete() {
            return 2 * Math.PI * radius;

        }

        public String getType() {
            return getColor() + "圆形";
        }

        private String getColor() {
            return null;
        }

        public static void main(String[] args) {
            Shape s1 = new Triangle("黑色", 3, 4, 5);
            Shape s2 = new Circle("黄色", 3);
            System.out.println(sl.getType());
        }

        public double calPerimeter() {
            return 0;
        }
    }
}

拿出来:
package java1;

public abstract class Shape {
    {
        System.out.println("执行Shaps的初始会");
    }
    private String color;

    public abstract double calPerimeter();

    public abstract String getType();

    public Shape() {
    }

    public Shape(String color) {
        System.out.println("执行Shaps的初始构造器。。。");
        this.color = color;
    }
   
    public static class Circle extends Shape {
        private static final Shape sl = null;
        private double radius;

        public Circle(String color, double radius) {
            super(color);
            this.radius = radius;
        }

        public void setRandius(double radius) {
            this.radius = radius;
        }

        public double calPerimete() {
            return 2 * Math.PI * radius;

        }

        public String getType() {
            return getColor() + "圆形";
        }

        private String getColor() {
            return null;
        }

        public static void main(String[] args) {
            Shape s1 = new Triangle("黑色", 3, 4, 5);
            Shape s2 = new Circle("黄色", 3);
            System.out.println(sl.getType());
        }

        public double calPerimeter() {
            return 0;
        }
    }
}

class Triangle extends Shape {
    private double a;
    private double b;
    private double c;

    public Triangle(String color, double a, double b, double c) {
        if (a >= b + c || b >= a + c || c >= a + b) {
            System.out.println("三角型两边之和必须大于第三班");
            return;
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double calPerimeter() {
        return a + b + c;

    }

    public String getType() {
        return "三角形";
    }
}
2017-01-17 09:12
GrayJerry
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:75
专家分:310
注 册:2015-10-20
收藏
得分:0 
回复 3楼 纵横阳仔
所以这样写进了死胡同
2017-01-17 09:14
Q1992
Rank: 2
等 级:论坛游民
帖 子:63
专家分:24
注 册:2016-6-26
收藏
得分:0 
回复 4楼 Q1992
package java1;

public abstract class Shape {
    {
        System.out.println("执行Shaps的初始会");
    }
    private String color;

    public abstract double calPerimeter();

    public abstract String getType();

    public Shape() {
    }

    public Shape(String color) {
        System.out.println("执行Shaps的初始构造器。。。");
        this.color = color;
    }
   
    public static class Triangle extends Shape {
        private double a;
        private double b;
        private double c;

        public Triangle(String color, double a, double b, double c) {
            if (a >= b + c || b >= a + c || c >= a + b) {
                System.out.println("三角型两边之和必须大于第三班");
                return;
            }
            this.a = a;
            this.b = b;
            this.c = c;
        }

        public double calPerimeter() {
            return a + b + c;

        }

        public String getType() {
            return "三角形";
        }
    }

    public static class Circle extends Shape {
        private static final Shape sl = null;
        private double radius;

        public Circle(String color, double radius) {
            super(color);
            this.radius = radius;
        }

        public void setRandius(double radius) {
            this.radius = radius;
        }

        public double calPerimete() {
            return 2 * Math.PI * radius;

        }

        public String getType() {
            return getColor() + "圆形";
        }

        private String getColor() {
            return null;
        }

        public static void main(String[] args) {
            Shape s1 = new Triangle("黑色", 3, 4, 5);
            Shape s2 = new Circle("黄色", 3);
            System.out.println(sl.getType());
        }

        public double calPerimeter() {
            return 0;
        }
    }

拿出来:

package java1
————————》
标记语法上有语法错误,删除这些标记
public abstract class Shape
                      ————》
                      嵌套类型 Shape  无法隐藏外层类型
   {
    {
        System.out.println("执行Shaps的初始会");
    }
    private String color;

    public abstract double calPerimeter();

    public abstract String getType();

    public Shape() {
    }

    public Shape(String color) {
        System.out.println("执行Shaps的初始构造器。。。");
        this.color = color;
    }
   
    public static class Circle extends Shape {
        private static final Shape sl = null;
        private double radius;

        public Circle(String color, double radius) {
            super(color);
            this.radius = radius;
        }

        public void setRandius(double radius) {
            this.radius = radius;
        }

        public double calPerimete() {
            return 2 * Math.PI * radius;

        }

        public String getType() {
            return getColor() + "圆形";
        }

        private String getColor() {
            return null;
        }

        public static void main(String[] args) {
            Shape s1 = new Triangle("黑色", 3, 4, 5);
            Shape s2 = new Circle("黄色", 3);
            System.out.println(sl.getType());
        }

        public double calPerimeter() {
            return 0;
        }
    }
}

class Triangle extends Shape {
    private double a;
    private double b;
    private double c;

    public Triangle(String color, double a, double b, double c)
          ——————》报错
        嵌套类型Triangle重复

  {
        if (a >= b + c || b >= a + c || c >= a + b) {
            System.out.println("三角型两边之和必须大于第三班");
            return;
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double calPerimeter() {
        return a + b + c;

    }

    public String getType() {
        return "三角形";
    }
}
}
2017-01-18 18:26
GrayJerry
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:75
专家分:310
注 册:2015-10-20
收藏
得分:0 
回复 7楼 Q1992
没看懂你什么意思,贴下代码吧
2017-01-19 09:20
快速回复:为什么这里错误
数据加载中...
 
   



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

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