标题:为什么这里错误
只看楼主
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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-473633-1-1.html




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

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